Text Field
A text input with a label, description and validation message.
Installation
npx shadcn@latest add @oakoss/text-fieldThis also installs Field, whose parts TextField renders.
Usage
import { TextField } from '#/components/ui/inputs/text-field';
<TextField
description="We never share your address."
label="Email"
type="email"
/>;For multiline text, TextareaField takes the same props with a growing
Textarea in place of the input.
Examples
Sizes
size (sm, md, lg) matches Button's heights, so an input and a button of
the same size line up.
Decorations
start and end put text or an icon inside the border, in an
Input Group. Text, such as a unit, is added to
the input's description after the field's own, so screen readers hear it; name
the unit in the label too. An element is decoration: mark an icon
aria-hidden, or name it. A button passed as end isn't disabled with the
field, so compose an Input Group in children for one.
On TextareaField, start and end sit above and below the textarea.
Validation
errors takes a form library's errors: strings, or objects with a message,
as TanStack Form and React Hook Form produce. Several errors render one per
line. Derive isInvalid from the same state, so the field clears when the
errors do. Type a .com address to clear them:
With TanStack Form, that's
errors={field.state.meta.errors} isInvalid={!field.state.meta.isValid}. The error shows only while the field is invalid, so errors without
isInvalid renders nothing, and errorMessage wins over errors. Without
either, React Aria's own validation (isRequired, validate, type="email")
supplies the message. For a whole form, see
TanStack Form.
Custom layout
children replace the default layout. Compose the
Field parts, which wire up the label, description and
error the same way:
Children and the layout props (label, description, errorMessage,
errors, placeholder, size, start, end) can't be mixed; set those on the parts
instead.
Accessibility
React Aria renders a native <input> and links it to its parts:
- Name and description: the label names the input, and the description and
error describe it (
aria-labelledby,aria-describedby), with no ids to wire. - Validation: an invalid field sets
aria-invalid, andisRequiredsets the nativerequiredattribute (aria-requiredwithvalidationBehavior="aria"). The required asterisk is hidden from screen readers, which already announce the field as required.
The border reaches 3:1 against the page at rest and when invalid, and the focus outline shows for any focus, mouse included.
The input keeps the browser's own keyboard behavior; React Aria adds no keys.
API
TextField
Extends React Aria's TextField.
With children, it renders only them; the parts are on the
Field page.
Props
Prop
Type
Also accepts all React Aria TextField props.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always text-field |
data-disabled | isDisabled is set |
data-invalid | The field is invalid |
data-readonly | isReadOnly is set |
data-required | isRequired is set |