loop-form-render.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { FormRenderProps, FlowNodeJSON, Field } from '@flowgram.ai/free-layout-editor';
  2. import { SubCanvasRender } from '@flowgram.ai/free-container-plugin';
  3. import { BatchVariableSelector, IFlowRefValue } from '@flowgram.ai/form-materials';
  4. import { useIsSidebar, useNodeRenderContext } from '../../hooks';
  5. import { FormHeader, FormContent, FormOutputs, FormItem, Feedback } from '../../form-components';
  6. interface LoopNodeJSON extends FlowNodeJSON {
  7. data: {
  8. batchFor: IFlowRefValue;
  9. };
  10. }
  11. export const LoopFormRender = ({ form }: FormRenderProps<LoopNodeJSON>) => {
  12. const isSidebar = useIsSidebar();
  13. const { readonly } = useNodeRenderContext();
  14. const formHeight = 85;
  15. const batchFor = (
  16. <Field<IFlowRefValue> name={`batchFor`}>
  17. {({ field, fieldState }) => (
  18. <FormItem name={'batchFor'} type={'array'} required>
  19. <BatchVariableSelector
  20. style={{ width: '100%' }}
  21. value={field.value?.content}
  22. onChange={(val) => field.onChange({ type: 'ref', content: val })}
  23. readonly={readonly}
  24. hasError={Object.keys(fieldState?.errors || {}).length > 0}
  25. />
  26. <Feedback errors={fieldState?.errors} />
  27. </FormItem>
  28. )}
  29. </Field>
  30. );
  31. if (isSidebar) {
  32. return (
  33. <>
  34. <FormHeader />
  35. <FormContent>
  36. {batchFor}
  37. <FormOutputs />
  38. </FormContent>
  39. </>
  40. );
  41. }
  42. return (
  43. <>
  44. <FormHeader />
  45. <FormContent>
  46. {batchFor}
  47. <SubCanvasRender offsetY={-formHeight} />
  48. <FormOutputs />
  49. </FormContent>
  50. </>
  51. );
  52. };