switch-form-field.tsx


Overview

switch-form-field.tsx defines a reusable React functional component, SwitchFormField, that integrates a toggle switch UI element into a form managed by react-hook-form. This component provides a labeled switch input with optional vertical layout and tooltip support, designed for consistent form styling and accessibility using the project's UI primitives.

The file’s primary purpose is to encapsulate the logic and rendering of a form switch field, simplifying form building by handling connection to form state, layout, and UI composition in one place.


Component: SwitchFormField

Description

SwitchFormField is a React component that renders a labeled switch input integrated with react-hook-form. It uses the following UI components from the project:

The component supports vertical or horizontal layout and can display a tooltip on the label.


Props

Prop

Type

Required

Default

Description

name

string

Yes

The form field name, used to register and bind the switch value in react-hook-form.

label

ReactNode

Yes

The label displayed alongside the switch.

vertical

boolean

No

true

When true, the label and switch are stacked vertically; otherwise, arranged horizontally.

tooltip

ReactNode

No

Optional tooltip content shown on the label for additional information.


Internal Behavior and Implementation Details


Usage Example

import { useForm, FormProvider } from 'react-hook-form';
import { SwitchFormField } from './switch-form-field';

function MyForm() {
  const methods = useForm({
    defaultValues: {
      notifications: false,
    },
  });

  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(data => console.log(data))}>
        <SwitchFormField
          name="notifications"
          label="Enable Notifications"
          tooltip="Toggle to receive email notifications"
          vertical={false}
        />
        <button type="submit">Submit</button>
      </form>
    </FormProvider>
  );
}

Interaction with Other Parts of the System

This component is designed to be used within a form that is wrapped by react-hook-form's FormProvider or equivalent context provider to work correctly.


Mermaid Component Diagram

componentDiagram
    component SwitchFormField {
        +name: string
        +label: ReactNode
        +vertical?: boolean
        +tooltip?: ReactNode
        render()
    }
    component FormField
    component FormItem
    component FormLabel
    component FormControl
    component Switch
    component useFormContext

    SwitchFormField --> useFormContext : use hook to get form control
    SwitchFormField --> FormField : wraps switch with form control
    SwitchFormField --> FormItem : layout container
    SwitchFormField --> FormLabel : displays label and tooltip
    SwitchFormField --> FormControl : wraps switch for styling
    SwitchFormField --> Switch : toggle input component

Summary

The switch-form-field.tsx file encapsulates a form switch component that:

This component is essential for building forms with toggle switches that seamlessly integrate into the app's form handling architecture.