Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dynamic-input-variable.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
  2. import { Button, Collapse, Flex, Form, Input, Select } from 'antd';
  3. import { PropsWithChildren, useCallback } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { Node } from 'reactflow';
  6. import { useBuildComponentIdSelectOptions } from '../../hooks/use-get-begin-query';
  7. import { NodeData } from '../../interface';
  8. import styles from './index.less';
  9. interface IProps {
  10. node?: Node<NodeData>;
  11. }
  12. enum VariableType {
  13. Reference = 'reference',
  14. Input = 'input',
  15. }
  16. const getVariableName = (type: string) =>
  17. type === VariableType.Reference ? 'component_id' : 'value';
  18. const DynamicVariableForm = ({ node }: IProps) => {
  19. const { t } = useTranslation();
  20. const valueOptions = useBuildComponentIdSelectOptions(
  21. node?.id,
  22. node?.parentId,
  23. );
  24. const form = Form.useFormInstance();
  25. const options = [
  26. { value: VariableType.Reference, label: t('flow.reference') },
  27. { value: VariableType.Input, label: t('flow.input') },
  28. ];
  29. const handleTypeChange = useCallback(
  30. (name: number) => () => {
  31. setTimeout(() => {
  32. form.setFieldValue(['query', name, 'component_id'], undefined);
  33. form.setFieldValue(['query', name, 'value'], undefined);
  34. }, 0);
  35. },
  36. [form],
  37. );
  38. return (
  39. <Form.List name="query">
  40. {(fields, { add, remove }) => (
  41. <>
  42. {fields.map(({ key, name, ...restField }) => (
  43. <Flex key={key} gap={10} align={'baseline'}>
  44. <Form.Item
  45. {...restField}
  46. name={[name, 'type']}
  47. className={styles.variableType}
  48. >
  49. <Select
  50. options={options}
  51. onChange={handleTypeChange(name)}
  52. ></Select>
  53. </Form.Item>
  54. <Form.Item noStyle dependencies={[name, 'type']}>
  55. {({ getFieldValue }) => {
  56. const type = getFieldValue(['query', name, 'type']);
  57. return (
  58. <Form.Item
  59. {...restField}
  60. name={[name, getVariableName(type)]}
  61. className={styles.variableValue}
  62. >
  63. {type === VariableType.Reference ? (
  64. <Select
  65. placeholder={t('common.pleaseSelect')}
  66. options={valueOptions}
  67. ></Select>
  68. ) : (
  69. <Input placeholder={t('common.pleaseInput')} />
  70. )}
  71. </Form.Item>
  72. );
  73. }}
  74. </Form.Item>
  75. <MinusCircleOutlined onClick={() => remove(name)} />
  76. </Flex>
  77. ))}
  78. <Form.Item>
  79. <Button
  80. type="dashed"
  81. onClick={() => add({ type: VariableType.Reference })}
  82. block
  83. icon={<PlusOutlined />}
  84. className={styles.addButton}
  85. >
  86. {t('flow.addVariable')}
  87. </Button>
  88. </Form.Item>
  89. </>
  90. )}
  91. </Form.List>
  92. );
  93. };
  94. export function FormCollapse({
  95. children,
  96. title,
  97. }: PropsWithChildren<{ title: string }>) {
  98. return (
  99. <Collapse
  100. className={styles.dynamicInputVariable}
  101. defaultActiveKey={['1']}
  102. items={[
  103. {
  104. key: '1',
  105. label: <span className={styles.title}>{title}</span>,
  106. children,
  107. },
  108. ]}
  109. />
  110. );
  111. }
  112. const DynamicInputVariable = ({ node }: IProps) => {
  113. const { t } = useTranslation();
  114. return (
  115. <FormCollapse title={t('flow.input')}>
  116. <DynamicVariableForm node={node}></DynamicVariableForm>
  117. </FormCollapse>
  118. );
  119. };
  120. export default DynamicInputVariable;