antlitz
Menu

Field

Wires one control to its label, description and error message. It generates the ids and the aria-describedby that connect them, which is the part most easily got wrong by hand.

import { Field } from "antlitz";

API

PropTypeDefault
childrenReactElement—
controlIdstring—
descriptionReactNode—
errorReactNode—
labelReactNode—
orientation"vertical" | "horizontal"vertical

Field takes exactly one child and clones it to add an id, aria-describedby, and aria-invalid. It reads required off that child, so the label's asterisk stays in step with what the browser enforces.

The generated ids derive from the control's id if it has one, then controlId, then a generated fallback. Pass controlId when the id must be stable.

Usage

Shown next to your comments.

export function Basic() {
  return (
    <>
      <Field label="Email">
        <Input type="email" placeholder="you@example.com" />
      </Field>
      <Field label="Display name" description="Shown next to your comments.">
        <Input defaultValue="alihan" />
      </Field>
    </>
  );
}

Validation

Enter a valid email address.

A short introduction.

Must be under 200 characters.

export function Validation() {
  return (
    <>
      <Field label="Email" error="Enter a valid email address.">
        <Input type="email" defaultValue="not-an-email" />
      </Field>
      <Field
        label="Bio"
        description="A short introduction."
        error="Must be under 200 characters."
      >
        <Textarea defaultValue="…" />
      </Field>
    </>
  );
}

Passing error sets aria-invalid on the control and appends the message's id to aria-describedby, so a screen reader announces the field as invalid and reads the reason. Any existing aria-describedby is preserved.

Required

As it appears on your ID.

Optional.

export function Required() {
  return (
    <>
      <Field label="Full name" description="As it appears on your ID.">
        <Input required defaultValue="Alihan" />
      </Field>
      <Field label="Nickname" description="Optional.">
        <Input />
      </Field>
    </>
  );
}

Orientation

Anyone can see your activity.

export function Orientation() {
  return (
    <>
      <Field label="Timezone">
        <Select defaultValue="Europe/Berlin">
          <option>Europe/Berlin</option>
          <option>Asia/Tokyo</option>
        </Select>
      </Field>
      <Field orientation="horizontal" label="Subscribe to updates">
        <Checkbox defaultChecked />
      </Field>
      <Field
        orientation="horizontal"
        label="Public profile"
        description="Anyone can see your activity."
      >
        <Switch />
      </Field>
    </>
  );
}

vertical stacks the label above the control. horizontal places the control first and the label beside it, which is the correct order for checkboxes and switches — and there label becomes required, since a horizontal field with no label has nothing to sit beside.

For several related controls sharing one label and one error, use Fieldset.