excel-to-html-form-field.tsx


Overview

The excel-to-html-form-field.tsx file defines a React functional component named ExcelToHtmlFormField. This component integrates with a form managed by react-hook-form and renders a labeled toggle switch. The switch enables users to set or toggle a boolean configuration option called parser_config.html4excel within the form state.

This component is primarily used for configuring whether a particular parser should treat HTML content as generated by Excel. The toggle switch is accompanied by a localized label and tooltip text to provide contextual help.


Detailed Documentation

ExcelToHtmlFormField Component

Description

ExcelToHtmlFormField is a form field component that connects to a form context via react-hook-form, exposing a toggle switch to control the parser_config.html4excel boolean value. It provides:

Usage

This component is designed to be used inside a form wrapped with react-hook-form's FormProvider or otherwise providing form context.

Example usage inside a form:

import { useForm, FormProvider } from 'react-hook-form';
import { ExcelToHtmlFormField } from './excel-to-html-form-field';

function MyForm() {
  const form = useForm();

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

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

Implementation Details

Props

The component takes no props directly, as it relies entirely on the form context and translations.

Rendered JSX Structure

<FormField control={form.control} name="parser_config.html4excel" render={({ field }) => {
  // Initialize default if undefined
  if (typeof field.value === 'undefined') {
    form.setValue('parser_config.html4excel', false);
  }

  return (
    <FormItem defaultChecked={false} className="items-center space-y-0">
      <div className="flex items-center gap-1">
        <FormLabel tooltip={t('html4excelTip')} className="text-sm text-muted-foreground whitespace-break-spaces w-1/4">
          {t('html4excel')}
        </FormLabel>
        <div className="w-3/4">
          <FormControl>
            <Switch checked={field.value} onCheckedChange={field.onChange} />
          </FormControl>
        </div>
      </div>
      <div className="flex pt-1">
        <div className="w-1/4"></div>
        <FormMessage />
      </div>
    </FormItem>
  );
}} />

Interactions with Other Parts of the System


Summary

Feature

Description

Purpose

Toggles the parser configuration option for HTML Excel parsing via a switch field.

Form Integration

Uses react-hook-form's useFormContext for form state.

Localization

Provides label and tooltip text localized via useTranslate.

UI Components

Uses custom form and switch UI components for consistent look and feel.

Default Initialization

Sets field value to false if undefined on initial render.


Visual Diagram

componentDiagram
    ExcelToHtmlFormField --> FormField
    ExcelToHtmlFormField --> useFormContext
    ExcelToHtmlFormField --> useTranslate
    ExcelToHtmlFormField --> FormItem
    ExcelToHtmlFormField --> FormLabel
    ExcelToHtmlFormField --> FormControl
    ExcelToHtmlFormField --> Switch
    ExcelToHtmlFormField --> FormMessage

Explanation:
This component diagram shows ExcelToHtmlFormField as the central component that interacts with form-related components (FormField, FormItem, FormLabel, FormControl, FormMessage) and the controlled input component Switch. It also depends on hooks useFormContext and useTranslate to manage form state and localization respectively.


End of Documentation for excel-to-html-form-field.tsx