Form field components for building accessible forms with TanStack Form.
"use client"
import { useForm } from "@tanstack/react-form"Installation
pnpmnpmyarnbun
Usage
import { useForm } from "@tanstack/react-form"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormDescription,
FormField,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"const form = useForm({
defaultValues: { email: "" },
onSubmit: async ({ value }) => {
console.log(value)
},
})
return (
<Form
onSubmit={(e) => {
e.preventDefault()
form.handleSubmit()
}}
>
<form.Field name="email">
{(field) => (
<FormField
name={field.name}
errors={field.state.meta.errors}
isTouched={field.state.meta.isTouched}
>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
value={field.state.value}
onBlur={field.handleBlur}
onChange={(e) => field.handleChange(e.target.value)}
/>
</FormControl>
<FormMessage />
</FormField>
)}
</form.Field>
<Button type="submit">Submit</Button>
</Form>
)Examples
With Zod Validation
Use Zod schemas for form-level validation.
"use client"
import { useForm } from "@tanstack/react-form"All Field Types
Examples with Input, Textarea, Select, RadioGroup, Checkbox, and Switch.
"use client"
import { useForm } from "@tanstack/react-form"Dynamic Array Fields
Add and remove fields dynamically with array mode.
"use client"
import { useForm } from "@tanstack/react-form"API Reference
Form
A styled <form> wrapper with consistent spacing.
FormField
Context provider for field state. Wrap each form field with this component.
Error Normalization: The errors prop accepts any error format and automatically extracts the message. This handles Zod validation errors, string errors, and objects with a message property.
Horizontal Layout: Use orientation="horizontal" for inline layouts like checkboxes and switches:
<FormField orientation="horizontal" {...props}>
<FormControl>
<Checkbox checked={checked} onCheckedChange={onChange} />
</FormControl>
<div className="space-y-1 leading-none">
<FormLabel>Accept terms</FormLabel>
<FormDescription>You agree to our terms of service.</FormDescription>
</div>
</FormField>FormLabel
Label component with automatic error styling.
FormControl
Slot component that passes id, aria-invalid, and aria-describedby to its child.
FormDescription
Help text displayed below the input.
FormMessage
Displays validation error messages when field is touched.
Accessibility
- Labels are automatically associated with inputs via
htmlFor/id - Error states use
aria-invalidattribute - Descriptions and errors use
aria-describedby - Error messages have
role="alert"for screen reader announcements