index.tsx
Overview
This file defines a React functional component, TuShareForm, which renders a form interface for users to input and configure parameters related to the TuShare API, a financial data service. The form includes inputs for API token, data source selection, date ranges, and keywords. A custom DateTimePicker component is embedded in the form for selecting date and time with proper formatting and value handling.
The primary purpose of this file is to provide a reusable, localized, and well-structured form UI component that integrates with Ant Design (antd), handles date-time inputs consistently using dayjs, and supports dynamic input variables through the imported DynamicInputVariable component.
Components and Functions
1. DateTimePicker
Description
A wrapper component around Ant Design's DatePicker that supports both date and time selection. It handles the conversion between the external numeric timestamp value and the internal dayjs object required by the DatePicker.
Props
Prop | Type | Description |
|---|---|---|
| [(val: number \ | undefined) => void](/projects/311/71659) (optional) |
| [number \ | undefined](/projects/311/74002) (optional) |
Behavior & Implementation Details
Uses
useCallbackfor memoizing thehandleChangefunction to avoid unnecessary re-renders.Converts the selected
dayjsdate object to a formatted stringYYYY-MM-DD HH:mm:ssbefore invokingonChange.Uses
useMemoto convert the incomingvalue(timestamp) to adayjsobject for proper display inside theDatePicker.The
valueprop is expected to be a timestamp (number), but internally converted to/from a formatted string.The component renders an
antdDatePickerwithshowTimeenabled and a fixed date-time format.
Usage Example
<DateTimePicker
value={someTimestamp}
onChange={(val) => console.log('Selected datetime:', val)}
/>
2. TuShareForm
Description
A form component built with Ant Design's Form component that collects parameters for queries to the TuShare financial data API. It supports internationalization through the useTranslate hook and dynamic input variables via the DynamicInputVariable component.
Props (from IOperatorForm interface)
Prop | Type | Description |
|---|---|---|
|
| Callback triggered when any form field changes. |
|
| The Ant Design form instance to be managed externally. |
|
| Passed to |
Form Fields
Label (i18n key) | Name | Input Type | Notes |
|---|---|---|---|
|
| Text Input | API token, tooltip points to https://tushare.pro/ |
|
| Select | Data source from predefined options ( |
|
| DateTimePicker | Start date/time for query |
|
| DateTimePicker | End date/time for query |
|
| Text Input | Keyword filter |
Implementation Details
Uses
useTranslate('flow')for localization of labels and options.The
tuShareSrcOptionsarray is memoized usinguseMemoto map constant source values to localized labels.Renders the
DynamicInputVariablecomponent at the top of the form, passing down thenodeprop.Uses Ant Design's vertical form layout and disables autocomplete.
The form is controlled externally via the passed
forminstance.On any form field change, the
onValuesChangecallback is fired with changed and all values.
Usage Example
import { Form } from 'antd';
import TuShareForm from './index';
const [form] = Form.useForm();
const handleValuesChange = (changedValues, allValues) => {
console.log('Form changed:', changedValues);
console.log('All values:', allValues);
};
<TuShareForm form={form} onValuesChange={handleValuesChange} node={someNode} />;
Important Implementation Details & Algorithms
Date Handling: The
DateTimePickercomponent manages date/time values by converting betweendayjsobjects (used internally by theantdDatePicker) and formatted string timestamps (YYYY-MM-DD HH:mm:ss) expected by the backend or parent components.Localization: The form labels and options use a translation hook
useTranslatescoped to'flow', allowing dynamic translation of UI text based on the user's locale.Dynamic Inputs: The inclusion of
DynamicInputVariablesuggests support for variable placeholders or expressions within form inputs, enhancing flexibility for user inputs.Memoization: Both
handleChangeand option list computation are memoized to optimize rendering and performance.
Interaction With Other Parts of the System
Hooks: Imports
useTranslatefrom a common hooks module, indicating a centralized localization strategy.Constants: Uses
TuShareSrcOptionsfrom a constants module, defining available data source options for the form's select input.Interfaces: Conforms to the
IOperatorForminterface imported from an interface module, ensuring consistent prop typing across the application.Components: Embeds
DynamicInputVariablefrom a sibling components directory, facilitating dynamic variable input inside the form.UI Library: Relies on Ant Design components (
Form,Input,Select,DatePicker) for consistent UI and behavior.Date Library: Uses
dayjsfor date parsing and formatting consistency.
The form component (TuShareForm) is likely used within a larger workflow or node-based editor UI where operators or nodes represent different data sources or transformations. The node prop hints at contextual data passed down for dynamic input rendering.
Visual Diagram
componentDiagram
component "TuShareForm" {
+form: FormInstance
+onValuesChange(changedValues, allValues)
+node: any
+DynamicInputVariable(node)
+Form.Item(token: Input)
+Form.Item(src: Select)
+Form.Item(start_date: DateTimePicker)
+Form.Item(end_date: DateTimePicker)
+Form.Item(keyword: Input)
}
component "DateTimePicker" {
+value: number | undefined
+onChange(val: number | undefined)
+DatePicker(showTime, format, onChange, value)
}
TuShareForm --> DateTimePicker : uses (for start_date, end_date)
TuShareForm --> DynamicInputVariable : embeds
Summary
This file provides a localized, extensible form component for TuShare API parameter input in a React application. It leverages Ant Design UI components, supports dynamic variables, and handles date/time inputs robustly with dayjs. The modular design and typed props ensure seamless integration into larger workflows or node-based UI systems. The DateTimePicker subcomponent abstracts date/time formatting and conversion complexities, allowing consistent handling of date inputs throughout the app.