oakoss/ui
Components

Field

Label, description, error and input parts for any React Aria field.

SourceReact Aria
Press Escape to clear.

Installation

npx shadcn@latest add @oakoss/field

This 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.

Choose a longer username. That username is taken.

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.

Shipping address

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.

Billed per month.

Separator

FieldSeparator divides fields, with optional text between two lines.

Or

Accessibility

React Aria links the parts through context:

  • Name and description: FieldLabel names the input, and FieldDescription and FieldError describe it, with no ids to wire.
  • Errors: FieldError renders 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: FieldSet is a native <fieldset>, and FieldLegend its <legend>, which screen readers announce on entering the group.
  • Layout parts: Field and FieldContent are plain <div>s with no role, so they add nothing for screen readers to announce. A FieldSeparator with text reads as one separator, then its text.
  • Other controls: a FieldLabel with htmlFor names 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

AttributePresent when
data-slotAlways field-error

Input

Extends React Aria's Input.

Props

Prop

Type

Also accepts all React Aria Input props except HTML's size.

Data attributes

AttributePresent when
data-slotAlways input
data-sizeAlways; the height
data-hoveredA pointer hovers it
data-focusedIt has focus
data-focus-visibleIt has keyboard focus
data-disabledIts field is disabled
data-invalidIts field is invalid

FieldLegend

A <legend>.

Props

Prop

Type

Data attributes

AttributePresent when
data-slotAlways field-legend
data-variantAlways; 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

AttributePresent when
data-slotAlways field
data-orientationAlways; vertical, horizontal or responsive

FieldSeparator

A <div> around a line, or two lines with text between them. Accepts div props.

AttributePresent when
data-slotAlways field-separator; field-separator-content on the text
data-contentAlways; true when it has text

Other parts

These add no props of their own, and each sets a data-slot:

PartExtendsdata-slot
FieldLabelReact Aria's Labelfield-label
FieldDescriptionReact Aria's Text with slot="description"field-description
FieldSet<fieldset>field-set
FieldGroup<div>, a container for responsive fieldsfield-group
FieldContent<div>field-content