Installation
The Form component is built on top of React Hook Form and Zod for schema validation.Usage
Create a form schema
Define your form schema using Zod:Define a form
Use theuseForm hook from React Hook Form to create a form:
Build your form
Compose form fields using the Form components:Component API
Form
The root form provider built on React Hook Form’sFormProvider.
Props:
- All props from React Hook Form’s
FormProvider
FormField
A controlled form field. Props:control- Form control fromuseFormname- Field namerender- Render function that receives field props
FormItem
Container for a form field.FormLabel
Label for a form field. Props:- Automatically shows error state
FormControl
Wrapper for the form control element. Props:- Automatically handles
id,aria-describedby, andaria-invalid
FormDescription
Description text for a form field.FormMessage
Displays form field errors. Props:- Automatically displays the error message for the field
useFormField
A custom hook to access form field context. Returns:id- Field IDname- Field nameformItemId- Form item IDformDescriptionId- Description IDformMessageId- Message IDerror- Field errorinvalid- Whether field is invalidisDirty- Whether field is dirtyisTouched- Whether field is touched
Examples
Input
A simple form with an input field:Textarea
A form with a textarea:Checkbox
A form with a checkbox:Select
A form with a select:Radio Group
A form with a radio group:Switch
A form with a switch:Accessibility
- Built on React Hook Form for performance
- Automatic error handling and display
- Proper ARIA attributes
- Label associations
- Error announcements for screen readers