Field
Puts a label, a control, a description and an error together. The label sits 8px above the control and the description 8px below it, all in 16px text.
Installation
npx shadcn@latest add @sevenone/fieldUsage
import {
Field,
FieldContent,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet,
} from "@/components/ui/field"Examples
Default
We will never share your email.
<Field>
<FieldLabel htmlFor="email" required>Email</FieldLabel>
<Input id="email" type="email" placeholder="m@example.com" aria-describedby="email-description" />
<FieldDescription id="email-description">We will never share your email.</FieldDescription>
</Field>Invalid
Set data-invalid on the Field and aria-invalid on the control. FieldError is announced right away.
Enter a valid email address.
<Field data-invalid>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" type="email" aria-invalid aria-describedby="email-error" />
<FieldError id="email-error">Enter a valid email address.</FieldError>
</Field>Checkbox
orientation="horizontal" puts the control next to its label.
One short email a month. Unsubscribe any time.
<Field orientation="horizontal">
<Checkbox id="news" defaultChecked aria-describedby="news-description" />
<FieldContent>
<FieldLabel htmlFor="news">Email me about new components</FieldLabel>
<FieldDescription id="news-description">One short email a month. Unsubscribe any time.</FieldDescription>
</FieldContent>
</Field>Field set
FieldSet and FieldLegend group related fields, and screen readers read the legend with each one.
<FieldSet>
<FieldLegend>Shipping address</FieldLegend>
<FieldDescription>Where we send your order.</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel htmlFor="street">Street</FieldLabel>
<Input id="street" autoComplete="street-address" />
</Field>
<div className="grid grid-cols-2 gap-4">
<Field>
<FieldLabel htmlFor="city">City</FieldLabel>
<Input id="city" autoComplete="address-level2" />
</Field>
<Field>
<FieldLabel htmlFor="zip">Postal code</FieldLabel>
<Input id="zip" autoComplete="postal-code" />
</Field>
</div>
<FieldSeparator />
<Field orientation="horizontal">
<Checkbox id="billing" defaultChecked />
<FieldLabel htmlFor="billing">Same as billing address</FieldLabel>
</Field>
</FieldGroup>
</FieldSet>Accessibility
- FieldLabel is a real label: connect it to the control with htmlFor and id.
- Connect FieldDescription and FieldError to the control with aria-describedby.
- FieldError has role=alert and the destructive text color (7:1); the message is text, never color alone.
- Use FieldSet and FieldLegend for groups of checkboxes, radios or related fields.