Skip to main content

ToggleField

Description

A toggle switch field component that provides a visual switch interface for boolean or binary (0/1) values. Features customizable size, variant colors, and label text for on/off states.

Import

import { ToggleField } from '@components/common/form/ToggleField';

Usage

import { Form } from '@components/common/form/Form';
import { ToggleField } from '@components/common/form/ToggleField';

function SettingsForm() {
return (
<Form action="/api/settings">
<ToggleField
name="emailNotifications"
label="Email Notifications"
defaultValue={true}
/>
</Form>
);
}

Props

NameTypeDefaultDescription
nameFieldPath<T>-Field name (required)
labelstring-Label text displayed above the toggle
trueValueboolean | 1trueValue when toggle is on
falseValueboolean | 0falseValue when toggle is off
trueLabelstring'Yes'Label text when toggle is on
falseLabelstring'No'Label text when toggle is off
size'sm' | 'md' | 'lg''md'Size of the toggle switch
variant'default' | 'success' | 'warning' | 'danger''default'Color variant for the toggle
defaultValueboolean | 0 | 1falseDefault toggle state
disabledbooleanfalseDisables the toggle
requiredbooleanfalseMakes the field required
errorstring-Custom error message
helperTextstring-Helper text shown in a tooltip
validationRegisterOptions<T>-React Hook Form validation rules
onChange(value: boolean | 0 | 1) => void-Callback fired when the value changes
wrapperClassNamestring-CSS class for the wrapper div

Example: Boolean Values

import { Form } from '@components/common/form/Form';
import { ToggleField } from '@components/common/form/ToggleField';

function NotificationSettings() {
return (
<Form action="/api/notifications">
<ToggleField
name="emailEnabled"
label="Email Notifications"
trueLabel="Enabled"
falseLabel="Disabled"
defaultValue={true}
/>

<ToggleField
name="smsEnabled"
label="SMS Notifications"
trueLabel="On"
falseLabel="Off"
/>
</Form>
);
}

Example: Binary Values (0/1)

import { Form } from '@components/common/form/Form';
import { ToggleField } from '@components/common/form/ToggleField';

function ProductForm() {
return (
<Form action="/api/products">
<ToggleField
name="isPublished"
label="Product Status"
trueValue={1}
falseValue={0}
trueLabel="Published"
falseLabel="Draft"
defaultValue={0}
/>
</Form>
);
}

Example: Different Sizes

import { Form } from '@components/common/form/Form';
import { ToggleField } from '@components/common/form/ToggleField';

function SizeDemo() {
return (
<Form action="/api/settings">
<ToggleField
name="setting1"
label="Small Toggle"
size="sm"
/>

<ToggleField
name="setting2"
label="Medium Toggle"
size="md"
/>

<ToggleField
name="setting3"
label="Large Toggle"
size="lg"
/>
</Form>
);
}

Example: Color Variants

import { Form } from '@components/common/form/Form';
import { ToggleField } from '@components/common/form/ToggleField';

function VariantDemo() {
return (
<Form action="/api/settings">
<ToggleField
name="default"
label="Default"
variant="default"
defaultValue={true}
/>

<ToggleField
name="success"
label="Success"
variant="success"
defaultValue={true}
/>

<ToggleField
name="warning"
label="Warning"
variant="warning"
defaultValue={true}
/>

<ToggleField
name="danger"
label="Danger"
variant="danger"
defaultValue={true}
/>
</Form>
);
}

Example: With Callback

import { Form } from '@components/common/form/Form';
import { ToggleField } from '@components/common/form/ToggleField';
import { useState } from 'react';

function FeatureToggle() {
const [isEnabled, setIsEnabled] = useState(false);

return (
<Form action="/api/features">
<ToggleField
name="feature"
label="Advanced Features"
onChange={(value) => {
setIsEnabled(Boolean(value));
console.log('Feature toggled:', value);
}}
helperText={isEnabled ? 'Advanced features are active' : 'Enable to access more features'}
/>
</Form>
);
}

Size Options

  • sm: Small toggle (h-5 w-9)
  • md: Medium toggle (h-6 w-11) - default
  • lg: Large toggle (h-7 w-14)

Variant Colors

  • default: Blue (default)
  • success: Green
  • warning: Yellow
  • danger: Red

Features

  • Visual Feedback: Animated switch with smooth transitions
  • Flexible Values: Support for boolean or binary (0/1) values
  • Custom Labels: Different labels for on/off states
  • Size Control: Three size options
  • Color Variants: Four color schemes
  • Controller Integration: Uses React Hook Form Controller
  • Accessibility: Includes ARIA attributes and screen reader support
  • TypeScript Support: Full type safety with generics

Styling

The toggle uses Tailwind CSS classes and includes:

  • Focus ring for keyboard navigation
  • Smooth color transitions
  • Disabled state styling
  • Error state styling with red ring