labels.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Field from './_field';
  2. import { Text } from 'app/design/typography'
  3. import { View, Row } from 'app/design/view'
  4. import { useState, useEffect , useCallback } from 'react';
  5. import { Modal, NeoButton } from "app/design/controls";
  6. import ChkList from 'app/ui/molecules/form-controls/checkbox-list';
  7. import emitter, { EVENTS } from 'app/context/emitter';
  8. import { LabelButton, wellButtonProps } from 'app/lib/form/form-helpers'
  9. import FieldWell from './_well'
  10. import { useFormField } from 'app/lib/form/use-form-field';
  11. import { useTranslation } from 'react-i18next'
  12. export default function FormFieldLabels(props) {
  13. const { t } = useTranslation();
  14. const { name, field } = useFormField(props);
  15. const isEmbedded = !!props.noPadding;
  16. const variant = props.variant || (isEmbedded ? 'text' : 'default');
  17. const size = props.size || 'sm';
  18. const [isModal, setIsModal] = useState(!!props.isShow);
  19. const [value2, setValue2] = useState(() => Array.isArray(field.value) ? field.value : []);
  20. useEffect(() => {
  21. const subscription = emitter.addListener(EVENTS.fieldLabels(name), (data) => {
  22. if (data.action == 'add') {
  23. setIsModal(true)
  24. }
  25. })
  26. return () => {
  27. subscription.remove()
  28. }
  29. }, [])
  30. const setFormValue = useCallback(
  31. (value) => {
  32. if (value){
  33. const filteredValue = value.filter(item => item);
  34. field.onChange(filteredValue);
  35. }
  36. setIsModal(false);
  37. },
  38. [field]
  39. );
  40. const removeValue = useCallback(
  41. (valueToRemove) => {
  42. return () => {
  43. const newValue = field.value.filter(item => item !== valueToRemove);
  44. field.onChange(newValue);
  45. setValue2(newValue);
  46. };
  47. },
  48. [field, setValue2]
  49. );
  50. const dataFlat = [...props.values.system, ...props.values.context].flatMap(item =>
  51. item.subitems ? [item, ...item.subitems] : item
  52. );
  53. const styles = props.align === 'right' ? 'justify-end' : 'justify-start';
  54. const buttonProps = wellButtonProps({ variant, size });
  55. const addButton = !props.hide_button && (
  56. <LabelButton {...buttonProps} title={t('Tags')} icon="Plus" field_name={name} />
  57. );
  58. const content = (
  59. <>
  60. {props.align != 'right' && addButton}
  61. {!!field.value && field.value.map((item, index) => (
  62. <NeoButton
  63. key={`lbl-${index}`}
  64. {...buttonProps}
  65. label={item}
  66. image="X"
  67. imagePlacement="trailing"
  68. onPress={removeValue(item)}
  69. />
  70. ))}
  71. {props.align == 'right' && addButton}
  72. </>
  73. );
  74. const header = <Row className=' w-full justify-between items-center'>
  75. <View><NeoButton style="glass" controlSize="regular" borderShape="circle" image="ArrowLeft" accessibilityLabel={t('Back')} onPress={() => { setIsModal(null) }} /></View>
  76. <View className='w-full flex-auto items-center justify-center'><Text className="text-secondary-foreground text-xl font-bold">{'Choose ' + props.caption}</Text></View>
  77. <View >
  78. <NeoButton
  79. style="glassProminent"
  80. controlSize="regular"
  81. borderShape="circle"
  82. image="Check"
  83. accessibilityLabel={t('Done')}
  84. onPress={() => setFormValue(value2)}
  85. />
  86. </View>
  87. </Row>
  88. return (
  89. <>
  90. <Modal
  91. title={header}
  92. onVisible={isModal}
  93. transparent={true}
  94. headerBorder={true}
  95. scrollable={true}
  96. >
  97. <ChkList values={dataFlat} selectedValue={value2} setValue={setValue2} showApply={false} />
  98. </Modal>
  99. <Field {...props}>
  100. {isEmbedded
  101. ? <View className={`${styles} w-full flex-auto items-center flex-row flex-wrap gap-1`}>{content}</View>
  102. : <FieldWell className={styles}>{content}</FieldWell>}
  103. </Field>
  104. </>
  105. );
  106. }