| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /**
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
- * SPDX-License-Identifier: MIT
- */
- import { FormRenderProps, FlowNodeJSON, Field, FormMeta } from '@flowgram.ai/free-layout-editor';
- import { SubCanvasRender } from '@flowgram.ai/free-container-plugin';
- import {
- BatchOutputs,
- BatchVariableSelector,
- createBatchOutputsFormPlugin,
- DisplayOutputs,
- IFlowRefValue,
- IJsonSchema,
- provideBatchInputEffect,
- } from '@flowgram.ai/form-materials';
- import { defaultFormMeta } from '../default-form-meta';
- import { useIsSidebar, useNodeRenderContext } from '../../hooks';
- import { FormHeader, FormContent, FormItem, Feedback } from '../../form-components';
- interface LoopNodeJSON extends FlowNodeJSON {
- data: {
- loopFor: IFlowRefValue;
- };
- }
- export const LoopFormRender = ({ form }: FormRenderProps<LoopNodeJSON>) => {
- const isSidebar = useIsSidebar();
- const { readonly } = useNodeRenderContext();
- const formHeight = 85;
- const loopFor = (
- <Field<IFlowRefValue> name={`loopFor`}>
- {({ field, fieldState }) => (
- <FormItem name={'loopFor'} type={'array'} required>
- <BatchVariableSelector
- style={{ width: '100%' }}
- value={field.value?.content}
- onChange={(val) => field.onChange({ type: 'ref', content: val })}
- readonly={readonly}
- hasError={Object.keys(fieldState?.errors || {}).length > 0}
- />
- <Feedback errors={fieldState?.errors} />
- </FormItem>
- )}
- </Field>
- );
- const loopOutputs = (
- <Field<Record<string, IFlowRefValue | undefined> | undefined> name={`loopOutputs`}>
- {({ field, fieldState }) => (
- <FormItem name="loopOutputs" type="object" vertical>
- <BatchOutputs
- style={{ width: '100%' }}
- value={field.value}
- onChange={(val) => field.onChange(val)}
- readonly={readonly}
- hasError={Object.keys(fieldState?.errors || {}).length > 0}
- />
- <Feedback errors={fieldState?.errors} />
- </FormItem>
- )}
- </Field>
- );
- if (isSidebar) {
- return (
- <>
- <FormHeader />
- <FormContent>
- {loopFor}
- {loopOutputs}
- </FormContent>
- </>
- );
- }
- return (
- <>
- <FormHeader />
- <FormContent>
- {loopFor}
- <SubCanvasRender offsetY={-formHeight} />
- <DisplayOutputs displayFromScope />
- </FormContent>
- </>
- );
- };
- export const formMeta: FormMeta = {
- ...defaultFormMeta,
- render: LoopFormRender,
- effect: {
- loopFor: provideBatchInputEffect,
- },
- plugins: [createBatchOutputsFormPlugin({ outputKey: 'loopOutputs' })],
- };
|