dynamic-output-variable.tsx


Overview

dynamic-output-variable.tsx is a React functional component file designed to render a dynamic form interface for managing output variables in a user-friendly way. It leverages Ant Design (antd) components and icons, combined with internationalization support (react-i18next) and a custom collapsible section component FormCollapse, to allow users to add, remove, and configure multiple output variables dynamically.

Each output variable consists of two parts:

This component is typically used in scenarios such as workflow builders, form generators, or any UI where users need to define multiple output variables with specific types.


Detailed Explanation

Imports


Types and Constants

type DynamicOutputVariableProps = {
  name?: string;
};
const options = [
  'String',
  'Number',
  'Boolean',
  'Array[String]',
  'Array[Number]',
  'Object',
].map((x) => ({ label: x, value: x }));

Component: DynamicOutputVariable

export const DynamicOutputVariable = ({
  name = 'output',
}: DynamicOutputVariableProps) => {
  const { t } = useTranslation();

  return (
    <FormCollapse title={t('flow.output')}>
      <Form.List name={name}>
        {(fields, { add, remove }) => (
          <>
            {fields.map(({ key, name, ...restField }) => (
              <div key={key} className="flex items-center gap-2 pb-4">
                <Form.Item {...restField} name={[name, 'first']} className="m-0 flex-1">
                  <Input />
                </Form.Item>
                <Form.Item {...restField} name={[name, 'last']} className="m-0 flex-1">
                  <Select placeholder={t('common.pleaseSelect')} options={options} />
                </Form.Item>
                <MinusCircleOutlined onClick={() => remove(name)} />
              </div>
            ))}
            <Form.Item>
              <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
                {t('flow.addVariable')}
              </Button>
            </Form.Item>
          </>
        )}
      </Form.List>
    </FormCollapse>
  );
};

Parameters

Return Value

Usage Example

import { Form } from 'antd';
import { DynamicOutputVariable } from './dynamic-output-variable';

const MyFormComponent = () => (
  <Form>
    <DynamicOutputVariable name="myOutputVars" />
  </Form>
);

This will render a form section titled according to the translation key 'flow.output', allowing dynamic addition/removal of output variables stored under the form path myOutputVars.


Implementation Details and Algorithms


Interaction with Other Parts of the System


Component Structure Diagram

componentDiagram
    component DynamicOutputVariable {
        +props: { name?: string }
        FormCollapse "Collapsible section"
        Form.List "Manages dynamic array of variables"
        Fields (name input + type select)
        PlusOutlined Button "Add variable"
        MinusCircleOutlined Icon "Remove variable"
    }

    DynamicOutputVariable --> FormCollapse
    DynamicOutputVariable --> Form.List
    Form.List --> Fields
    Fields --> Input
    Fields --> Select
    Fields --> MinusCircleOutlined
    DynamicOutputVariable --> Button : Add new field

Summary

This component is a reusable building block in UI forms for dynamic variable configuration and integrates cleanly with form state management and i18n infrastructure.