Theme
Customize appearance
Components
Form
Connect React Hook Form state with accessible labels and validation messages.
View component source ↗Preview
A minimal example to get started.
Installation
Install this component and its dependencies in a project initialized with shadcn.
pnpm dlx shadcn@latest add https://ui.hyunjoon.net/r/form.jsonUsage
This is the basic example. Use the Code tab above for the selected example. Adjust import aliases to match your project.
import { useState } from "react";
import { useForm } from "react-hook-form";
import { Button } from "@/components/ui/button";
import {
Form,
FormField,
FormItem,
FormLabel,
FormControl,
FormDescription,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
export default function Example() {
const form = useForm({ defaultValues: { name: "" } });
const [saved, setSaved] = useState(false);
return (
<Form {...form}>
<form
className="grid w-full max-w-sm gap-4"
onSubmit={form.handleSubmit(() => setSaved(true))}
>
<FormField
control={form.control}
name="name"
rules={{ required: "Please enter your name." }}
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl render={<Input {...field} />} />
<FormDescription>This is shown to your teammates.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Save</Button>
{saved && (
<p role="status" className="text-sm">
Profile saved for this preview.
</p>
)}
</form>
</Form>
);
}
API reference
Derived from the installed component types. Native DOM props are also supported where applicable. A dash means no explicit default is documented. Some props depend on the selected mode.
Form
| Prop | Type | Default | Description |
|---|---|---|---|
childrenRequired | React.ReactNode | React.ReactNode[]React.ReactNode | React.ReactNode[] | — | Content rendered inside this component. |
watchRequired | UseFormWatch<TFieldValues>UseFormWatch<TFieldValues> | — | Configures watch; see the accepted type. |
getValuesRequired | UseFormGetValues<TFieldValues>UseFormGetValues<TFieldValues> | — | Configures get values; see the accepted type. |
getErrorsRequired | UseFormGetErrors<TFieldValues>UseFormGetErrors<TFieldValues> | — | Configures get errors; see the accepted type. |
getFieldStateRequired | UseFormGetFieldState<TFieldValues>UseFormGetFieldState<TFieldValues> | — | Configures get field state; see the accepted type. |
setErrorRequired | UseFormSetError<TFieldValues>UseFormSetError<TFieldValues> | — | Configures set error; see the accepted type. |
clearErrorsRequired | UseFormClearErrors<TFieldValues>UseFormClearErrors<TFieldValues> | — | Configures clear errors; see the accepted type. |
setValueRequired | UseFormSetValue<TFieldValues>UseFormSetValue<TFieldValues> | — | Configures set value; see the accepted type. |
setValuesRequired | UseFormSetValues<TFieldValues>UseFormSetValues<TFieldValues> | — | Configures set values; see the accepted type. |
triggerRequired | UseFormTrigger<TFieldValues>UseFormTrigger<TFieldValues> | — | Configures trigger; see the accepted type. |
formStateRequired | FormState<TFieldValues>FormState<TFieldValues> | — | Configures form state; see the accepted type. |
resetFieldRequired | UseFormResetField<TFieldValues>UseFormResetField<TFieldValues> | — | Configures reset field; see the accepted type. |
resetRequired | UseFormReset<TFieldValues>UseFormReset<TFieldValues> | — | Configures reset; see the accepted type. |
resetDefaultValuesRequired | UseFormResetDefaultValues<TFieldValues>UseFormResetDefaultValues<TFieldValues> | — | Configures reset default values; see the accepted type. |
handleSubmitRequired | UseFormHandleSubmit<TFieldValues, TTransformedValues>UseFormHandleSubmit<TFieldValues, TTransformedValues> | — | Configures handle submit; see the accepted type. |
unregisterRequired | UseFormUnregister<TFieldValues>UseFormUnregister<TFieldValues> | — | Configures unregister; see the accepted type. |
controlRequired | Control<TFieldValues, TContext, TTransformedValues>Control<TFieldValues, TContext, TTransformedValues> | — | React Hook Form control object for the field. |
registerRequired | UseFormRegister<TFieldValues>UseFormRegister<TFieldValues> | — | Configures register; see the accepted type. |
setFocusRequired | UseFormSetFocus<TFieldValues>UseFormSetFocus<TFieldValues> | — | Configures set focus; see the accepted type. |
subscribeRequired | UseFormSubscribe<TFieldValues>UseFormSubscribe<TFieldValues> | — | Configures subscribe; see the accepted type. |
FormField
| Prop | Type | Default | Description |
|---|---|---|---|
renderRequired | ({ field, fieldState, formState, }: { field: ControllerRenderProps<TValues, TName>; fieldS…({ field, fieldState, formState, }: { field: ControllerRenderProps<TValues, TName>; fieldState: ControllerFieldState; formState: UseFormStateReturn<TValues>; }) => React.ReactElement | — | Element or render function used to compose a custom root element. |
nameRequired | TNameTName | — | Field name used in form data. |
rules | Omit<RegisterOptions<TValues, TName>, "disabled" | "valueAsNumber" | "valueAsDate" | "setV…Omit<RegisterOptions<TValues, TName>, "disabled" | "valueAsNumber" | "valueAsDate" | "setValueAs"> | undefined | — | Validation rules applied to this field. |
shouldUnregister | boolean | undefinedboolean | undefined | — | Configures should unregister; see the accepted type. |
defaultValue | PathValueImpl<TValues, TName> | undefinedPathValueImpl<TValues, TName> | undefined | — | Initial value for an uncontrolled component. |
control | Control<TValues, any, TValues> | undefinedControl<TValues, any, TValues> | undefined | — | React Hook Form control object for the field. |
disabled | boolean | undefinedboolean | undefined | — | Prevents user interaction. |
exact | boolean | undefinedboolean | undefined | — | Configures exact; see the accepted type. |
FormItem
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLDivElement> | undefinedReact.Ref<HTMLDivElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLDivElement, Element> | undefinedReact.ChangeEventHandler<HTMLDivElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLDivElement> | undefinedReact.KeyboardEventHandler<HTMLDivElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLDivElement> | undefinedReact.MouseEventHandler<HTMLDivElement> | undefined | — | Called when the element is activated. |
FormLabel
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLLabelElement> | undefinedReact.Ref<HTMLLabelElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
htmlFor | string | undefinedstring | undefined | — | ID of the associated form control. |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLLabelElement> | undefinedReact.FocusEventHandler<HTMLLabelElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLLabelElement> | undefinedReact.FocusEventHandler<HTMLLabelElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLLabelElement, Element> | undefinedReact.ChangeEventHandler<HTMLLabelElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLLabelElement> | undefinedReact.KeyboardEventHandler<HTMLLabelElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLLabelElement> | undefinedReact.MouseEventHandler<HTMLLabelElement> | undefined | — | Called when the element is activated. |
FormControl
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLInputElement> | undefinedReact.Ref<HTMLInputElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
alt | string | undefinedstring | undefined | — | Accessible text alternative for the image. |
checked | boolean | undefinedboolean | undefined | — | Current checked state when controlled. |
disabled | boolean | undefinedboolean | undefined | — | Prevents user interaction. |
max | string | number | undefinedstring | number | undefined | — | Maximum allowed value. |
maxLength | number | undefinednumber | undefined | — | Maximum number of input characters. |
min | string | number | undefinedstring | number | undefined | — | Minimum allowed value. |
multiple | boolean | undefinedboolean | undefined | — | Allows more than one value to be selected. |
name | string | undefinedstring | undefined | — | Field name used in form data. |
placeholder | string | undefinedstring | undefined | — | Hint shown when the control has no value. |
required | boolean | undefinedboolean | undefined | — | Whether the user must provide a value before form submission. |
src | string | undefinedstring | undefined | — | URL of the displayed resource. |
step | string | number | undefinedstring | number | undefined | — | Increment between valid values. |
type | React.HTMLInputTypeAttribute | undefinedReact.HTMLInputTypeAttribute | undefined | — | The native input or button behavior. |
value | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | The current value when controlled by the application. |
onChange | React.ChangeEventHandler<HTMLInputElement, HTMLInputElement> | undefinedReact.ChangeEventHandler<HTMLInputElement, HTMLInputElement> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLInputElement> | undefinedReact.FocusEventHandler<HTMLInputElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLInputElement> | undefinedReact.FocusEventHandler<HTMLInputElement> | undefined | — | Called when the control loses focus. |
onKeyDown | React.KeyboardEventHandler<HTMLInputElement> | undefinedReact.KeyboardEventHandler<HTMLInputElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLInputElement> | undefinedReact.MouseEventHandler<HTMLInputElement> | undefined | — | Called when the element is activated. |
render | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn…React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, {}> | undefined | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |
FormDescription
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLParagraphElement> | undefinedReact.Ref<HTMLParagraphElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLParagraphElement> | undefinedReact.FocusEventHandler<HTMLParagraphElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLParagraphElement> | undefinedReact.FocusEventHandler<HTMLParagraphElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLParagraphElement, Element> | undefinedReact.ChangeEventHandler<HTMLParagraphElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLParagraphElement> | undefinedReact.KeyboardEventHandler<HTMLParagraphElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLParagraphElement> | undefinedReact.MouseEventHandler<HTMLParagraphElement> | undefined | — | Called when the element is activated. |
FormMessage
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLParagraphElement> | undefinedReact.Ref<HTMLParagraphElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLParagraphElement> | undefinedReact.FocusEventHandler<HTMLParagraphElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLParagraphElement> | undefinedReact.FocusEventHandler<HTMLParagraphElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLParagraphElement, Element> | undefinedReact.ChangeEventHandler<HTMLParagraphElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLParagraphElement> | undefinedReact.KeyboardEventHandler<HTMLParagraphElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLParagraphElement> | undefinedReact.MouseEventHandler<HTMLParagraphElement> | undefined | — | Called when the element is activated. |
Usage notes
Wrap controls in FormField and FormItem. Compose FormControl with render, and pass validation rules to FormField.