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.

dynamic-input-variable.tsx 3.7KB

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