cross-language-form-field.tsx


Overview

cross-language-form-field.tsx defines a reusable React form component named CrossLanguageFormField that provides a multi-select dropdown for users to choose multiple languages. It is designed to be integrated into forms managed by react-hook-form and supports internationalization with react-i18next. The component displays a labeled field with optional vertical or horizontal layout and includes user-friendly features such as tooltips and placeholder text.

This component is typically used in settings or configuration forms where users specify cross-lingual preferences, such as selecting multiple languages for translation, content filtering, or language-specific processing.


Detailed Explanation

Constants

Languages

const Languages = [
  'English',
  'Chinese',
  'Spanish',
  'French',
  'German',
  'Japanese',
  'Korean',
  'Vietnamese',
];

options

const options = Languages.map((x) => ({
  label: t('language.' + toLower(x)),
  value: x,
}));

Types

CrossLanguageItemProps

type CrossLanguageItemProps = {
  name?: string;
  vertical?: boolean;
};

Component: CrossLanguageFormField

export const CrossLanguageFormField = ({
  name = 'prompt_config.cross_languages',
  vertical = true,
}: CrossLanguageItemProps) => {
  const { t } = useTranslation();
  const form = useFormContext();

  return (
    <FormField
      control={form.control}
      name={name}
      render={({ field }) => (
        <FormItem
          className={cn('flex', {
            'gap-2': vertical,
            'flex-col': vertical,
            'justify-between': !vertical,
            'items-center': !vertical,
          })}
        >
          <FormLabel tooltip={t('chat.crossLanguageTip')}>
            {t('chat.crossLanguage')}
          </FormLabel>
          <FormControl>
            <MultiSelect
              options={options}
              placeholder={t('fileManager.pleaseSelect')}
              maxCount={100}
              {...field}
              onValueChange={field.onChange}
              defaultValue={field.value}
              modalPopover
            />
          </FormControl>
        </FormItem>
      )}
    />
  );
};

Description

Parameters

Prop

Type

Default

Description

name

string?

'prompt_config.cross_languages'

Form field name in react-hook-form context.

vertical

boolean?

true

Determines vertical (true) or horizontal (false) layout.

Return Value

Usage Example

import { useForm, FormProvider } from 'react-hook-form';
import { CrossLanguageFormField } from '@/components/forms/cross-language-form-field';

const MyForm = () => {
  const methods = useForm({
    defaultValues: {
      prompt_config: {
        cross_languages: ['English', 'Spanish'],
      },
    },
  });

  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(onSubmit)}>
        <CrossLanguageFormField vertical={false} />
        <button type="submit">Submit</button>
      </form>
    </FormProvider>
  );
};

Implementation Details and Algorithms


Interaction with Other Parts of the System

This component is designed to be embedded inside larger forms where language selection is necessary, such as user preferences, content filtering, or multilingual prompt configurations.


Visual Diagram

classDiagram
    CrossLanguageFormField {
        +name: string
        +vertical: boolean
        +render()
    }
    CrossLanguageFormField ..> FormField : uses
    CrossLanguageFormField ..> FormItem : uses
    CrossLanguageFormField ..> FormLabel : uses
    CrossLanguageFormField ..> FormControl : uses
    CrossLanguageFormField ..> MultiSelect : uses
    CrossLanguageFormField ..> useFormContext : hooks into
    CrossLanguageFormField ..> useTranslation : hooks into

Summary

cross-language-form-field.tsx exports a flexible, localized multi-select form field component for choosing multiple languages. Leveraging React Hook Form for state management and i18next for internationalization, it provides a user-friendly interface with tooltips and layout options suitable for multilingual applications. The component's dependencies on UI primitives and form utilities make it a modular part of a larger form system.