index.tsx
Overview
index.tsx defines a React functional component named SwitchForm that implements a dynamic, nested conditional form interface for building switch-case style logic flows within a UI. The form allows users to configure multiple "cases," each containing one or more logical conditions, and specify the next step (or branch) to take in a workflow based on those conditions. An ELSE branch can also be configured to handle conditions not matched by any case.
This component leverages Ant Design UI components (Form, Select, Input, Card, Button) and integrates translation support via react-i18next. It uses custom hooks to build dynamic select options based on the current node context, and supports adding/removing cases and conditions dynamically.
Detailed Explanation
Component: SwitchForm
A React functional component that renders a configurable switch-case form.
Props
interface IOperatorForm {
onValuesChange: (changedValues: any, allValues: any) => void;
node: {
id: string;
parentId?: string;
// other node properties
};
form: FormInstance; // Ant Design form instance
}
onValuesChange: Callback invoked whenever any form field values change.node: The current node context containing at least anidand optionally aparentId.form: Ant DesignForminstance to control and manage form state.
Return
Returns JSX rendering a dynamic form that supports multiple cases and nested conditions.
Key Internal Constants and Hooks
t: Translation function fromuseTranslationto localize labels and options.buildCategorizeToOptions: Function fromuseBuildFormSelectOptionshook to generate next-step options dynamically for "to" selects.getSelectedConditionTos: Utility function to extract all "to" values currently selected in conditions for exclusion from options.switchOperatorOptions: Memoized list of operator options localized for condition operators.switchLogicOperatorOptions: Memoized list of logical operator options localized for combining multiple conditions.componentIdOptions: Options for selecting component IDs from the node context, built usinguseBuildComponentIdSelectOptions.
Form Structure and Behavior
Form Container:
name="dynamic_form_complex",autoComplete="off", vertical layout.Initializes with one empty condition in
conditions.Calls
onValuesChangeon any value change.
Form.List "conditions":
Represents multiple "cases" (switch cases).
Each case is rendered inside an Ant Design
Cardwith a close icon to remove the case.Case title:
"Case X"(1-based index).Inside each case:
Optionally renders a logical operator select if there are multiple conditions.
Select for
to(next step) with options excluding already selected "to" values elsewhere.Nested
Form.Listfor "items" (individual conditions).Each condition is a card with fields:
componentId(select)operator(select)value(input)
Each condition can be removed with a close icon.
Button to add new conditions, which also sets a default logical operator and operator.
Button to add new cases.
ELSE Case:
Separate select outside the cases to choose the "ELSE" branch.
Options exclude already selected "to" values in cases.
Usage Example
import React from 'react';
import { Form } from 'antd';
import SwitchForm from './index';
const MySwitchFormWrapper = () => {
const [form] = Form.useForm();
const onFormChange = (changedValues, allValues) => {
console.log('Form changed:', allValues);
};
const node = { id: 'node-1', parentId: 'parent-1' };
return (
<SwitchForm onValuesChange={onFormChange} node={node} form={form} />
);
};
export default MySwitchFormWrapper;
Important Implementation Details
Dynamic Field Arrays: Uses Ant Design's
Form.Listto handle dynamic addition/removal of cases and conditions.Field Dependencies: Uses
Form.Itemdependenciesprop to conditionally render the logical operator select only if more than one condition exists in a case.Exclusion Logic in Options: The
buildCategorizeToOptionsfunction is called with an array of already selected "to" values to exclude them from next-step options, preventing circular or duplicate references.Localization: All user-facing labels and option texts are localized using the
tfunction fromreact-i18next.CSS Modules: Styles are imported from
index.lessand applied via class names.
Interaction with Other Parts of the System
Constants and Interfaces: Utilizes constants like
Operator.Switch,SwitchElseTo, and interfaceISwitchFormfrom the system's shared constants and interfaces.Custom Hooks: Depends on custom hooks
useBuildFormSelectOptionsanduseBuildComponentIdSelectOptionsto retrieve dynamic select options, likely querying or caching data based on the current node context.Utilities: Calls
getOtherFieldValuesutility to get values of other fields for option exclusion logic.Translation Namespace: Appears to use the
flownamespace for i18n keys (e.g.,flow.switchOperatorOptions,flow.logicalOperator).
This component likely serves as part of a UI builder or workflow editor where users visually configure conditional branching logic.
Visual Diagram
componentDiagram
component SwitchForm {
+onValuesChange
+node
+form
-- Hooks & State --
useTranslation()
useBuildFormSelectOptions()
useBuildComponentIdSelectOptions()
useMemo() for options
-- Form Structure --
Form.List "conditions" (Cases)
├─ Card per Case
│ ├─ Logical Operator Select (conditional)
│ ├─ Next Step ("to") Select
│ └─ Form.List "items" (Conditions)
│ ├─ Card per Condition
│ │ ├─ ComponentId Select
│ │ ├─ Operator Select
│ │ └─ Value Input
│ └─ Add Condition Button
├─ Add Case Button
ELSE Select (outside cases)
}
Summary
index.tsx exports a sophisticated form component to visually build switch-case logic with nested conditions and branching options. It carefully manages dynamic form fields, localized UI, and contextual select options to enable users to create complex logical flows in an intuitive interface. This component integrates tightly with the system's workflow node context and data sources via custom hooks and utilities.