You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { CloseOutlined } from '@ant-design/icons';
  2. import { Button, Card, Divider, Form, Input, Select } from 'antd';
  3. import { useMemo } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import {
  6. Operator,
  7. SwitchElseTo,
  8. SwitchLogicOperatorOptions,
  9. SwitchOperatorOptions,
  10. } from '../../constant';
  11. import { useBuildFormSelectOptions } from '../../form-hooks';
  12. import { useBuildComponentIdSelectOptions } from '../../hooks/use-get-begin-query';
  13. import { IOperatorForm, ISwitchForm } from '../../interface';
  14. import { getOtherFieldValues } from '../../utils';
  15. import styles from './index.less';
  16. const SwitchForm = ({ onValuesChange, node, form }: IOperatorForm) => {
  17. const { t } = useTranslation();
  18. const buildCategorizeToOptions = useBuildFormSelectOptions(
  19. Operator.Switch,
  20. node?.id,
  21. );
  22. const getSelectedConditionTos = () => {
  23. const conditions: ISwitchForm['conditions'] =
  24. form?.getFieldValue('conditions');
  25. return conditions?.filter((x) => !!x).map((x) => x?.to) ?? [];
  26. };
  27. const switchOperatorOptions = useMemo(() => {
  28. return SwitchOperatorOptions.map((x) => ({
  29. value: x.value,
  30. label: t(`flow.switchOperatorOptions.${x.label}`),
  31. }));
  32. }, [t]);
  33. const switchLogicOperatorOptions = useMemo(() => {
  34. return SwitchLogicOperatorOptions.map((x) => ({
  35. value: x,
  36. label: t(`flow.switchLogicOperatorOptions.${x}`),
  37. }));
  38. }, [t]);
  39. const componentIdOptions = useBuildComponentIdSelectOptions(
  40. node?.id,
  41. node?.parentId,
  42. );
  43. return (
  44. <Form
  45. form={form}
  46. name="dynamic_form_complex"
  47. autoComplete="off"
  48. initialValues={{ conditions: [{}] }}
  49. onValuesChange={onValuesChange}
  50. layout={'vertical'}
  51. >
  52. <Form.List name="conditions">
  53. {(fields, { add, remove }) => (
  54. <div style={{ display: 'flex', rowGap: 16, flexDirection: 'column' }}>
  55. {fields.map((field) => {
  56. return (
  57. <Card
  58. size="small"
  59. title={`Case ${field.name + 1}`}
  60. key={field.key}
  61. className={styles.caseCard}
  62. extra={
  63. <CloseOutlined
  64. onClick={() => {
  65. remove(field.name);
  66. }}
  67. />
  68. }
  69. >
  70. <Form.Item noStyle dependencies={[field.name, 'items']}>
  71. {({ getFieldValue }) =>
  72. getFieldValue(['conditions', field.name, 'items'])
  73. ?.length > 1 && (
  74. <Form.Item
  75. label={t('flow.logicalOperator')}
  76. name={[field.name, 'logical_operator']}
  77. >
  78. <Select options={switchLogicOperatorOptions} />
  79. </Form.Item>
  80. )
  81. }
  82. </Form.Item>
  83. <Form.Item label={t('flow.to')} name={[field.name, 'to']}>
  84. <Select
  85. allowClear
  86. options={buildCategorizeToOptions([
  87. form?.getFieldValue(SwitchElseTo),
  88. ...getOtherFieldValues(
  89. form!,
  90. 'conditions',
  91. field,
  92. 'to',
  93. ),
  94. ])}
  95. />
  96. </Form.Item>
  97. <Form.Item label="Condition">
  98. <Form.List name={[field.name, 'items']}>
  99. {(subFields, subOpt) => (
  100. <div
  101. style={{
  102. display: 'flex',
  103. flexDirection: 'column',
  104. rowGap: 16,
  105. }}
  106. >
  107. {subFields.map((subField) => (
  108. <Card
  109. key={subField.key}
  110. title={null}
  111. size="small"
  112. className={styles.conditionCard}
  113. bordered
  114. extra={
  115. <CloseOutlined
  116. onClick={() => {
  117. subOpt.remove(subField.name);
  118. }}
  119. />
  120. }
  121. >
  122. <Form.Item
  123. label={t('flow.componentId')}
  124. name={[subField.name, 'cpn_id']}
  125. >
  126. <Select
  127. placeholder={t('flow.componentId')}
  128. options={componentIdOptions}
  129. />
  130. </Form.Item>
  131. <Form.Item
  132. label={t('flow.operator')}
  133. name={[subField.name, 'operator']}
  134. >
  135. <Select
  136. placeholder={t('flow.operator')}
  137. options={switchOperatorOptions}
  138. />
  139. </Form.Item>
  140. <Form.Item
  141. label={t('flow.value')}
  142. name={[subField.name, 'value']}
  143. >
  144. <Input placeholder={t('flow.value')} />
  145. </Form.Item>
  146. </Card>
  147. ))}
  148. <Button
  149. onClick={() => {
  150. form?.setFieldValue(
  151. ['conditions', field.name, 'logical_operator'],
  152. SwitchLogicOperatorOptions[0],
  153. );
  154. subOpt.add({
  155. operator: SwitchOperatorOptions[0].value,
  156. });
  157. }}
  158. block
  159. className={styles.addButton}
  160. >
  161. + Add Condition
  162. </Button>
  163. </div>
  164. )}
  165. </Form.List>
  166. </Form.Item>
  167. </Card>
  168. );
  169. })}
  170. <Button onClick={() => add()} block className={styles.addButton}>
  171. + Add Case
  172. </Button>
  173. </div>
  174. )}
  175. </Form.List>
  176. <Divider />
  177. <Form.Item
  178. label={'ELSE'}
  179. name={[SwitchElseTo]}
  180. className={styles.elseCase}
  181. >
  182. <Select
  183. allowClear
  184. options={buildCategorizeToOptions(getSelectedConditionTos())}
  185. />
  186. </Form.Item>
  187. </Form>
  188. );
  189. };
  190. export default SwitchForm;