Skip to content

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.

Only used about your booking.

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.

Nothing is sent.

'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 id and 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 sets aria-invalid, which turns the border to --critical.
  • Put only one control in each field. Two would share the same id. Use Checkbox on 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.

Field props
PropTypeDefaultDescription
labelReactNoderequiredThe visible label.
childrenReactNoderequiredOne Input, Select or Textarea.
errorstring—Replaces the hint and marks the control as invalid. An empty string means no error.
hintstring—Help shown under the control.
quietbooleanfalseMutes the label.
classNamestring—Extra classes, for spacing or width. Utility classes override the component’s own styles.

Tokens

Tokens
TokenDefaultSets
--label-color--foregroundLabel colour (a quiet label uses --muted)
--label-weight500Label weight (a quiet label uses 400)
--field-font-size0.875remText size of the field and its control