Field
Label, description, error and input parts for any React Aria field.
Installation
npx shadcn@latest add @oakoss/fieldThis also installs Input, which lives in its own file.
Usage
FieldLabel, FieldDescription and FieldError find their field through
React Aria's context. Inside any React Aria field (TextField, SearchField,
NumberField, Select, ComboBox, DatePicker and others) they link to the
input for screen readers with no ids to wire.
import { SearchField } from 'react-aria-components';
import { FieldDescription, FieldLabel } from '#/components/ui/inputs/field';
import { Input } from '#/components/ui/inputs/input';
<SearchField>
<FieldLabel>Search docs</FieldLabel>
<Input />
<FieldDescription>Press / to focus.</FieldDescription>
</SearchField>;Examples
Errors
errors on FieldError takes a form library's errors (strings, or objects
with a message); duplicates, blank entries and anything else are dropped, and
several errors render one per line. Children win over errors unless empty
(cond && 'Message'). With neither, it shows React Aria's validation messages.
It renders only while its field is invalid.
Input sizes
size on Input (sm, md, lg) sets the control height, which matches
Button's. It replaces HTML's size attribute (width in characters).
Layout
FieldSet and FieldLegend group related fields under a caption that screen
readers announce, and FieldGroup stacks fields with consistent spacing.
Orientation
orientation on Field lays out a label beside its control. Put Field
inside the React Aria field, so the field still wires its label and
description. horizontal keeps them in a row, and a FieldContent column
next to a control lines the row up at its top. responsive is a row once the
nearest FieldGroup is 28rem wide and stacks below that; outside a
FieldGroup it always stacks. A stacked Field stretches every child to its
width, a Button included.
Separator
FieldSeparator divides fields, with optional text between two lines.
Or
Accessibility
React Aria links the parts through context:
- Name and description:
FieldLabelnames the input, andFieldDescriptionandFieldErrordescribe it, with no ids to wire. - Errors:
FieldErrorrenders only while the field is invalid, so screen readers never read a stale message. Several errors are read in one pass, with a space between them so their words don't run together. - Groups:
FieldSetis a native<fieldset>, andFieldLegendits<legend>, which screen readers announce on entering the group. - Layout parts:
FieldandFieldContentare plain<div>s with no role, so they add nothing for screen readers to announce. AFieldSeparatorwith text reads as one separator, then its text. - Other controls: a
FieldLabelwithhtmlFornames that control only, even inside a React Aria field.
Input's border reaches 3:1 against the page at rest and when invalid.
The input keeps the browser's own keyboard behavior; React Aria adds no keys.
API
FieldError
Extends React Aria's FieldError.
Props
Prop
Type
Also accepts all React Aria FieldError props.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always field-error |
Input
Extends React Aria's Input.
Props
Prop
Type
Also accepts all React Aria Input props except HTML's size.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always input |
data-size | Always; the height |
data-hovered | A pointer hovers it |
data-focused | It has focus |
data-focus-visible | It has keyboard focus |
data-disabled | Its field is disabled |
data-invalid | Its field is invalid |
FieldLegend
A <legend>.
Props
Prop
Type
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always field-legend |
data-variant | Always; legend or label text size |
Field
A <div> that lays out a label, description and control.
Props
Prop
Type
Also accepts div props.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always field |
data-orientation | Always; vertical, horizontal or responsive |
FieldSeparator
A <div> around a line, or two lines with text between them. Accepts div
props.
| Attribute | Present when |
|---|---|
data-slot | Always field-separator; field-separator-content on the text |
data-content | Always; true when it has text |
Other parts
These add no props of their own, and each sets a data-slot:
| Part | Extends | data-slot |
|---|---|---|
FieldLabel | React Aria's Label | field-label |
FieldDescription | React Aria's Text with slot="description" | field-description |
FieldSet | <fieldset> | field-set |
FieldGroup | <div>, a container for responsive fields | field-group |
FieldContent | <div> | field-content |