@payfit/unity-components
Version:
47 lines (46 loc) • 1.79 kB
TypeScript
import { ToggleSwitch as CoreToggleSwitch } from '../../../../core/components/toggle-switch/ToggleSwitch.js';
type BaseToggleSwitchProps = React.ComponentProps<typeof CoreToggleSwitch>;
export type ToggleSwitchProps = Omit<BaseToggleSwitchProps, 'isSelected' | 'defaultSelected'>;
/**
* TanStack‑controlled toggle switch.
*
* `ToggleSwitch` is the Unity `ToggleSwitch` wired to the TanStack Form
* field context. Its selected state (`isSelected`) is fully controlled by the
* form and updates through `field.handleChange` on user interaction.
*
* Accessibility:
* - Automatically links to the enclosing `FormField` feedback text via
* `aria-describedby`.
* - Use a plain string `label` (or `isLabelSrOnly` for visually hidden labels)
* to keep the control accessible.
*
* Usage:
* - Must be rendered inside `<form.AppField name="…">` from
* `useUnityForm`.
* - Do not pass `isSelected`/`defaultSelected`; use form default values instead.
* @example
* ```tsx
* import { useUnityForm } from '../../hooks/use-form.js'
* import { ToggleSwitch } from '@/components/toggle-switch/ToggleSwitch'
* import * as z from 'zod'
*
* const schema = z.object({ newsletter: z.boolean().optional() })
*
* export function Example() {
* const form = useUnityForm({ validators: { onBlur: schema } })
* return (
* <form.AppForm>
* <form.Form>
* <form.AppField name="newsletter">
* {() => (
* <ToggleSwitch label="Subscribe to newsletter" />
* )}
* </form.AppField>
* </form.Form>
* </form.AppForm>
* )
* }
* ```
*/
declare const ToggleSwitch: import('react').ForwardRefExoticComponent<Omit<ToggleSwitchProps, "ref"> & import('react').RefAttributes<HTMLLabelElement>>;
export { ToggleSwitch };