cross-language-item.tsx
Overview
cross-language-item.tsx defines a reusable React functional component named CrossLanguageItem that renders a multi-select dropdown for choosing multiple languages. It leverages Ant Design's Select and Form components and integrates internationalization support using the react-i18next library.
This component is designed to be used within forms where users need to select one or more languages from a predefined list. It supports form binding via the Ant Design Form.Item's name property, making it easily integrable into form data management and validation flows.
Detailed Explanation
Constants
Languages
An array of strings representing the available languages for selection:[ 'English', 'Chinese', 'Spanish', 'French', 'German', 'Japanese', 'Korean', 'Vietnamese', ]options
Transforms theLanguagesarray into an array of objects withlabelandvaluekeys, which is the required format for Ant Design'sSelectoptions:[ { label: 'English', value: 'English' }, { label: 'Chinese', value: 'Chinese' }, ... ]
Types
CrossLanguageItemProps
Defines the props object accepted by the CrossLanguageItem component:
Property | Type | Default | Description |
|---|---|---|---|
|
|
|
|
Component: CrossLanguageItem
export const CrossLanguageItem = ({
name = ['prompt_config', 'cross_languages'],
}: CrossLanguageItemProps) => {
const { t } = useTranslation();
return (
<Form.Item
label={t('chat.crossLanguage')}
name={name}
tooltip={t('chat.crossLanguageTip')}
>
<AntSelect
options={options}
allowClear
placeholder={t('common.languagePlaceholder')}
mode="multiple"
/>
</Form.Item>
);
};
Purpose
Renders a form item containing a multiple selection dropdown allowing users to choose one or more languages. It supports internationalization for labels, tooltips, and placeholders.
Parameters
props(CrossLanguageItemProps):name(optional): The key or path used by Ant Design's Form system to identify and store the selected value(s). Can be a string or an array of strings for nested form values.
Return Value
Returns a JSX element representing a labeled form item with a multi-select control.
Usage Example
import { Form } from 'antd';
import { CrossLanguageItem } from './cross-language-item';
const ExampleForm = () => {
return (
<Form onFinish={(values) => console.log(values)}>
<CrossLanguageItem name="userLanguages" />
<Form.Item>
<button type="submit">Submit</button>
</Form.Item>
</Form>
);
};
In this example, the selected languages will be stored under the key userLanguages in the form values.
Important Implementation Details
Internationalization (
useTranslationhook):
All user-facing text (label, tooltip, placeholder) is fetched dynamically via translation keys:Label:
'chat.crossLanguage'Tooltip:
'chat.crossLanguageTip'Placeholder:
'common.languagePlaceholder'
This allows the component to adapt to different languages without code changes.
Multi-select mode:
The Ant DesignSelectcomponent is set tomode="multiple", enabling users to select multiple options simultaneously.Form Integration:
The component wraps the select control insideForm.Item, binding the selected values to the form data under the specifiedname. This makes it easy to integrate with Ant Design's validation and form submission mechanisms.Options Generation:
The component statically defines a list of supported languages and maps them to the expected option format on every render. Since the list is static and small, this is efficient and straightforward.
Interaction with Other Parts of the System
Ant Design (
antd):
UtilizesFormandSelectcomponents from the Ant Design UI library, relying on its form management and UI styles.i18n (react-i18next):
Uses theuseTranslationhook for rendering localized strings, making the component's UI adapt to different locales based on app settings.Forms and Data Flow:
Designed to be embedded in larger forms that handle user input, validation, and submission. Thenameprop connects it to the form's data model.
Visual Diagram
classDiagram
class CrossLanguageItem {
+name: string | string[]
+() => JSX.Element
}
CrossLanguageItem ..> Form.Item : uses
CrossLanguageItem ..> AntSelect : uses
CrossLanguageItem ..> useTranslation : uses
Summary
cross-language-item.tsx provides a localized, form-integrated, multi-language selection UI component. It is simple, declarative, and designed for easy reuse in forms requiring users to select multiple languages from a fixed list. The component abstracts away the complexities of internationalization and form binding, allowing developers to focus on higher-level form logic and UI composition.