Theme
Customize appearance
Components
Field
Lay out a label, control, description, and validation feedback.
View component source ↗Preview
A minimal example to get started.
We’ll only use this for account updates.
Installation
Install this component and its dependencies in a project initialized with shadcn.
pnpm dlx shadcn@latest add https://ui.hyunjoon.net/r/field.jsonUsage
This is the basic example. Use the Code tab above for the selected example. Adjust import aliases to match your project.
import { Field, FieldLabel, FieldDescription } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export default function Example() {
return (
<Field className="max-w-sm">
<FieldLabel htmlFor="example-email">Email address</FieldLabel>
<Input id="example-email" type="email" placeholder="you@example.com" />
<FieldDescription>We’ll only use this for account updates.</FieldDescription>
</Field>
);
}
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.
Field
| 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. |
orientation | "horizontal" | "vertical" | "responsive" | null | undefined"horizontal" | "vertical" | "responsive" | null | undefined | "vertical" | Layout axis used by the component. |
FieldLabel
| 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. |
FieldDescription
| 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. |
FieldError
| 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. |
errors | ({ message?: string; } | undefined)[] | undefined({ message?: string; } | undefined)[] | undefined | — | Validation errors to present near the field. |
FieldGroup
| 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. |
FieldLegend
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLLegendElement> | undefinedReact.Ref<HTMLLegendElement> | 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<HTMLLegendElement> | undefinedReact.FocusEventHandler<HTMLLegendElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLLegendElement> | undefinedReact.FocusEventHandler<HTMLLegendElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLLegendElement, Element> | undefinedReact.ChangeEventHandler<HTMLLegendElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLLegendElement> | undefinedReact.KeyboardEventHandler<HTMLLegendElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLLegendElement> | undefinedReact.MouseEventHandler<HTMLLegendElement> | undefined | — | Called when the element is activated. |
variant | "label" | "legend" | undefined"label" | "legend" | undefined | "legend" | Visual treatment of the component. |
FieldSeparator
| 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. |
FieldSet
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLFieldSetElement> | undefinedReact.Ref<HTMLFieldSetElement> | 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). |
disabled | boolean | undefinedboolean | undefined | — | Prevents user interaction. |
name | string | undefinedstring | undefined | — | Field name used in form data. |
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<HTMLFieldSetElement> | undefinedReact.FocusEventHandler<HTMLFieldSetElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLFieldSetElement> | undefinedReact.FocusEventHandler<HTMLFieldSetElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLFieldSetElement, Element> | undefinedReact.ChangeEventHandler<HTMLFieldSetElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLFieldSetElement> | undefinedReact.KeyboardEventHandler<HTMLFieldSetElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLFieldSetElement> | undefinedReact.MouseEventHandler<HTMLFieldSetElement> | undefined | — | Called when the element is activated. |
FieldContent
| 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. |
FieldTitle
| 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. |
Usage notes
Connect the label to the control with htmlFor and id. Field supplies layout; your form library supplies validation and state.