Field
Form field layout: label, control, description and error, in vertical, horizontal and responsive orientations.
Label and description
Two to five letters. It starts every task number in the project.
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
export default function FieldLabelAndDescription() {
return (
<Field className="max-w-xs">
<FieldLabel htmlFor="field-project-key">Project key</FieldLabel>
<Input id="field-project-key" defaultValue="ATL" aria-describedby="field-project-key-description" />
<FieldDescription id="field-project-key-description">
Two to five letters. It starts every task number in the project.
</FieldDescription>
</Field>
)
}
With an error
Enter an address with a domain, such as accounts@example.com.
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
export default function FieldWithAnError() {
return (
<Field data-invalid="true" className="max-w-xs">
<FieldLabel htmlFor="field-invoice-email">Send invoices to</FieldLabel>
<Input
id="field-invoice-email"
type="email"
defaultValue="accounts@example"
aria-invalid
aria-describedby="field-invoice-email-error"
/>
<FieldError id="field-invoice-email-error">Enter an address with a domain, such as accounts@example.com.</FieldError>
</Field>
)
}
Several errors
- Use at least 12 characters.
- Add a number or a symbol.
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
const errors = [{ message: "Use at least 12 characters." }, { message: "Add a number or a symbol." }]
export default function FieldSeveralErrors() {
return (
<Field data-invalid="true" className="max-w-xs">
<FieldLabel htmlFor="field-password">New password</FieldLabel>
<Input
id="field-password"
type="password"
autoComplete="new-password"
defaultValue="release"
aria-invalid
aria-describedby="field-password-errors"
/>
<FieldError id="field-password-errors" errors={errors} />
</Field>
)
}
Horizontal
Email the customer three days before an invoice is due.
Add 2% for each month an invoice stays unpaid.
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function FieldHorizontal() {
return (
<FieldGroup className="max-w-sm">
<Field orientation="horizontal">
<FieldContent>
<FieldLabel htmlFor="field-reminders">Payment reminders</FieldLabel>
<FieldDescription id="field-reminders-description">
Email the customer three days before an invoice is due.
</FieldDescription>
</FieldContent>
<Switch id="field-reminders" defaultChecked aria-describedby="field-reminders-description" />
</Field>
<Field orientation="horizontal">
<FieldContent>
<FieldLabel htmlFor="field-late-fees">Late fees</FieldLabel>
<FieldDescription id="field-late-fees-description">
Add 2% for each month an invoice stays unpaid.
</FieldDescription>
</FieldContent>
<Switch id="field-late-fees" aria-describedby="field-late-fees-description" />
</Field>
</FieldGroup>
)
}
Responsive
Shown on invoices and in emails.
Where customers answer an invoice email.
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
export default function FieldResponsive() {
return (
<FieldGroup className="max-w-xl">
<Field orientation="responsive">
<FieldContent>
<FieldLabel htmlFor="field-workspace-name">Workspace name</FieldLabel>
<FieldDescription id="field-workspace-name-description">Shown on invoices and in emails.</FieldDescription>
</FieldContent>
<Input
id="field-workspace-name"
defaultValue="Atlas"
aria-describedby="field-workspace-name-description"
className="@md/field-group:max-w-56"
/>
</Field>
<Field orientation="responsive">
<FieldContent>
<FieldLabel htmlFor="field-reply-to">Reply-to address</FieldLabel>
<FieldDescription id="field-reply-to-description">Where customers answer an invoice email.</FieldDescription>
</FieldContent>
<Input
id="field-reply-to"
type="email"
defaultValue="billing@example.com"
aria-describedby="field-reply-to-description"
className="@md/field-group:max-w-56"
/>
</Field>
</FieldGroup>
)
}
Grouped
import {
Field,
FieldDescription,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
export default function FieldGrouped() {
return (
<FieldGroup className="max-w-sm">
<FieldSet>
<FieldLegend>Billing contact</FieldLegend>
<FieldDescription>Who receives invoices and payment receipts.</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel htmlFor="field-contact-team">Team</FieldLabel>
<Input id="field-contact-team" defaultValue="Finance" />
</Field>
<Field>
<FieldLabel htmlFor="field-contact-email">Email</FieldLabel>
<Input id="field-contact-email" type="email" defaultValue="billing@example.com" />
</Field>
</FieldGroup>
</FieldSet>
<FieldSeparator />
<FieldSet>
<FieldLegend>Invoice footer</FieldLegend>
<FieldGroup>
<Field>
<FieldLabel htmlFor="field-footer-note">Note</FieldLabel>
<Textarea id="field-footer-note" defaultValue="Payment is due 30 days after the invoice date." />
</Field>
</FieldGroup>
</FieldSet>
</FieldGroup>
)
}
Choice cards
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,
FieldContent,
FieldDescription,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSet,
FieldTitle,
} from "@/components/ui/field"
const exports = [
{ id: "tasks", title: "Tasks", description: "Every task with its status, owner and due date.", checked: true },
{ id: "invoices", title: "Invoices", description: "Sent and draft invoices with their line items.", checked: false },
]
export default function FieldChoiceCards() {
return (
<FieldSet className="w-full max-w-sm">
<FieldLegend variant="label">Include in the export</FieldLegend>
<FieldGroup className="gap-2">
{exports.map((option) => (
<FieldLabel key={option.id} htmlFor={`field-export-${option.id}`}>
<Field orientation="horizontal">
<FieldContent>
<FieldTitle>{option.title}</FieldTitle>
<FieldDescription>{option.description}</FieldDescription>
</FieldContent>
<Checkbox id={`field-export-${option.id}`} defaultChecked={option.checked} />
</Field>
</FieldLabel>
))}
</FieldGroup>
</FieldSet>
)
}
Disabled
Only the workspace owner can change it.
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
export default function FieldDisabled() {
return (
<Field data-disabled="true" className="max-w-xs">
<FieldLabel htmlFor="field-tax-number">Tax number</FieldLabel>
<Input
id="field-tax-number"
defaultValue="GB 123 4567 89"
disabled
aria-describedby="field-tax-number-description"
/>
<FieldDescription id="field-tax-number-description">Only the workspace owner can change it.</FieldDescription>
</Field>
)
}
Installation
npx shadcn@latest add @khaeli/fieldbunx --bun shadcn@latest add @khaeli/fieldInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- class-variance-authority ^0.7.1
Accessible name
FieldLabelis a @khaeli/label: itshtmlForis theidof the control.FieldDescriptionandFieldErrorare not tied to the control for you. Give each anidand list it in the control'saria-describedby.- For an error, set
data-invalid="true"onField(it colours the label and the message) andaria-invalidon the control (it tells assistive technology).FieldErrorhasrole="alert", so a message that appears is announced. FieldSetwith aFieldLegendnames a group of fields. Use it for a set of checkboxes and for a section of a form.- For a disabled field, set
data-disabled="true"onFieldanddisabledon the control.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/field.json