index.tsx
Overview
index.tsx defines a React functional component named Jin10Form that renders a dynamic form using Ant Design components. This form is designed to configure different types of Jin10 data sources or operators within a workflow or flow-based application.
The form adapts its fields dynamically based on the selected "type" (flash, calendar, symbols, or news), presenting relevant options and inputs accordingly. It also supports localization through a custom useTranslate hook, ensuring labels and options are translated based on context.
Key features include:
Dynamic rendering of form fields depending on the chosen Jin10 data type.
Translation-ready UI labels and option texts.
Integration with Ant Design's
Formand input components.Usage of memoized option lists to optimize rendering performance.
Incorporation of a custom
DynamicInputVariablecomponent, likely for variable input binding or complex input handling.Controlled form values and change handlers via props.
Detailed Explanation
Component: Jin10Form
const Jin10Form = ({ onValuesChange, form, node }: IOperatorForm) => { ... }
Description
Jin10Form is a React component that renders a form for configuring Jin10 operator parameters. It uses the Ant Design Form API integrated with a controlled form instance passed via props, allowing external control and state management.
Props (IOperatorForm interface)
onValuesChange: (changedValues, allValues) => void
Callback function triggered whenever any form value changes. It receives the changed values and all current form values.form: FormInstance
An instance of Ant Design's Form object for controlling form state and validation externally.node: any
Data or metadata related to the current node/operator in a workflow. Passed down toDynamicInputVariablecomponent.
Internal variables and hooks
t = useTranslate('flow')
Localization function scoped to the'flow'namespace, used to translate UI text and option labels.useMemohooks for generating option arrays with localized labels, corresponding to the different Jin10 option categories:jin10TypeOptionsjin10FlashTypeOptionsjin10CalendarTypeOptionsjin10CalendarDatashapeOptionsjin10SymbolsTypeOptionsjin10SymbolsDatatypeOptions
Each memoized option array maps raw option strings to objects of shape { value: string, label: string } with the label translated for UI display.
Rendered JSX Structure
<Form>
The main container form with vertical layout, controlled by the passedforminstance, and listens to value changes viaonValuesChange.<DynamicInputVariable node={node}>
A custom component injected at the top of the form, presumably to handle dynamic variables or inputs related to the node.<Form.Item>for selecting the Jin10 type (type) with options rendered fromjin10TypeOptions. Default value:'flash'.<Form.Item>for entering asecret_key(string input).<Form.Item noStyle dependencies={['type']}>
A dependent form item that conditionally renders additional fields depending on the selectedtypevalue:When
type === 'flash':flash_type(select)contain(input)filter(input)
When
type === 'calendar':calendar_type(select)calendar_datashape(select)
When
type === 'symbols':symbols_type(select)symbols_datatype(select)
When
type === 'news':contain(input)filter(input)
Default: renders nothing.
Each label is localized via the t function.
Usage Example
import { Form } from 'antd';
import Jin10Form from './path/to/index';
const MyComponent = () => {
const [form] = Form.useForm();
const handleValuesChange = (changedValues, allValues) => {
console.log('Form values changed:', allValues);
};
return (
<Jin10Form
form={form}
onValuesChange={handleValuesChange}
node={{ id: 'node_123' }} // example node data
/>
);
};
Important Implementation Details
Localization: All option labels and form item labels use the
useTranslatehook with keys matching the option constants. This ensures the form supports internationalization.Dynamic Form Rendering Based on Type: The conditional rendering inside the
Form.Itemwithdependencies={['type']}leverages Ant Design's form dependency mechanism to re-render appropriate input fields when thetypechanges, keeping the UI responsive and relevant.Memoization of Options: Using
useMemofor option arrays avoids unnecessary recalculations and re-renders, improving performance especially when translations or options might be expensive to compute.Loose Coupling: The form component is designed to be generic and reusable, accepting external form instance and change handlers. It does not manage its own state internally, facilitating integration into larger forms or workflows.
DynamicInputVariable Integration: This component is included at the top of the form and takes the
nodeprop. Although its implementation is not shown here, it likely provides advanced input features such as variable substitution or dynamic expressions relevant to the workflow node.
Interaction with Other Parts of the System
Hooks: Imports and uses a custom
useTranslatehook from@/hooks/common-hooksfor translation functionality.Options: Imports several option arrays from
../../optionsthat define possible values for different Jin10 data types and categories.Interface: Relies on the
IOperatorForminterface from../../interfacefor typing props, ensuring consistency across operator forms in the system.DynamicInputVariable Component: Uses a sibling component from
../components/dynamic-input-variable, which likely handles more complex input scenarios tied to the workflow's node context.Form Handling: The form's controlled nature (external form instance and onValuesChange) suggests this component is part of a larger flow editor or workflow builder where each operator's configuration is managed centrally.
Mermaid Component Diagram
componentDiagram
component Jin10Form {
+onValuesChange(changedValues, allValues)
+form: FormInstance
+node: object
}
component DynamicInputVariable
component useTranslate
component AntdForm {
+Form
+Form.Item
+Input
+Select
}
component OptionsData
Jin10Form --> AntdForm : uses
Jin10Form --> DynamicInputVariable : renders
Jin10Form --> useTranslate : calls
Jin10Form --> OptionsData : imports option constants
Summary
The index.tsx file defines the Jin10Form React component, a flexible and localized form for configuring Jin10 data source/operator parameters within a flow-based system. It dynamically adjusts its inputs based on the selected data type, supports internationalization, and integrates tightly with Ant Design's form components and a custom dynamic input system. This modular approach facilitates easy extension and integration within complex workflow editors or operator configuration UIs.