Field
A label, hint and error message around one control, connected for screen readers.
import { Field, Input } from '@/design-system';Examples
Labels and hints
quiet suits filters and toolbars.
import { Field, Input } from '@/design-system';
export default function FieldBasic() {
return (
<div className="grid gap-4 sm:grid-cols-2">
<Field label="Name">
<Input name="name" />
</Field>
<Field label="Phone" hint="Only used about your booking.">
<Input name="phone" type="tel" />
</Field>
<Field label="Search" quiet>
<Input type="search" placeholder="Company or email" />
</Field>
</div>
);
}Validation
Field shows the message you give it. Deciding when a value is wrong is up to your form.
'use client';
import { useState } from 'react';
import { Button, Field, Input } from '@/design-system';
export default function FieldValidation() {
const [email, setEmail] = useState('');
const [error, setError] = useState('');
return (
<form
noValidate
className="space-y-4"
onSubmit={event => {
event.preventDefault();
setError(
email.includes('@') ? '' : 'Enter an email address with an @.',
);
}}
>
<Field label="Email" hint="Nothing is sent." error={error}>
<Input
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
/>
</Field>
<Button type="submit">Check</Button>
</form>
);
}How it works
- Field makes a unique
idand shares it with the control inside, so clicking the label focuses the control. - The hint or error is linked with
aria-describedby. An error also setsaria-invalid, which turns the border to--critical. - Put only one control in each field. Two would share the same
id. UseCheckboxon its own, since it has its own label. - The error is not announced as it appears. On submit, move focus to the first invalid control.
Props
Field takes only these props.
| Prop | Type | Default | Description |
|---|---|---|---|
label | ReactNode | required | The visible label. |
children | ReactNode | required | One Input, Select or Textarea. |
error | string | — | Replaces the hint and marks the control as invalid. An empty string means no error. |
hint | string | — | Help shown under the control. |
quiet | boolean | false | Mutes the label. |
className | string | — | Extra classes, for spacing or width. Utility classes override the component’s own styles. |
Tokens
| Token | Default | Sets |
|---|---|---|
--label-color | --foreground | Label colour (a quiet label uses --muted) |
--label-weight | 500 | Label weight (a quiet label uses 400) |
--field-font-size | 0.875rem | Text size of the field and its control |