'use client'; import * as React from 'react'; import * as Slot from 'radix-ui/slot'; import { Controller, FormProvider, useFormContext, useFormState, type ControllerProps, type FieldPath, type FieldValues, } from 'react-hook-form'; import { cn } from '@/lib/utils'; import { Label } from '@/components/label/label'; /** * react-hook-form bindings. * * `react-hook-form` is an **optional peer dependency** — install it in your app * if you import from this subpath. Everything else in the kit works without it. * * The value here is the wiring, not the styling: `FormControl` links the input * to its label, description and error message via `aria-describedby` and sets * `aria-invalid`, so validation state is announced instead of only being shown. * * ```tsx * const form = useForm({ resolver: zodResolver(schema) }); * *
* * ( * * Email * * We never share it. * * * )} * /> * * * ``` */ const Form = FormProvider; type FormFieldContextValue< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, > = { name: TName; }; const FormFieldContext = React.createContext(null); type FormItemContextValue = { id: string; }; const FormItemContext = React.createContext(null); const FormField = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, >( props: ControllerProps ) => { return ( ); }; /** * Reads the ids and validation state of the enclosing field. Throws with a * useful message rather than failing silently when used out of context. */ const useFormField = () => { const fieldContext = React.useContext(FormFieldContext); const itemContext = React.useContext(FormItemContext); const { getFieldState } = useFormContext(); if (!fieldContext) { throw new Error('useFormField must be used within a '); } if (!itemContext) { throw new Error('useFormField must be used within a '); } const formState = useFormState({ name: fieldContext.name }); const fieldState = getFieldState(fieldContext.name, formState); const { id } = itemContext; return { id, name: fieldContext.name, formItemId: `${id}-form-item`, formDescriptionId: `${id}-form-item-description`, formMessageId: `${id}-form-item-message`, ...fieldState, }; }; function FormItem({ className, ...props }: React.ComponentProps<'div'>) { const id = React.useId(); return (
); } function FormLabel({ className, ...props }: React.ComponentProps) { const { error, formItemId } = useFormField(); return (