Field

A label over one control, with a hint or an error under it.

Overview

Every text, select or file control: it gets a visible label.

Label

Do

Shown on your reviews.

import { Field, Input } from "@hackcove/ui";

<Field label="Display name" htmlFor="do-label-name" hint="Shown on your reviews.">
  <Input id="do-label-name" />
</Field>
Every control sits in a Field: the label is tied to the control by htmlFor, and the hint sits under it.
Don't
import { Input } from "@hackcove/ui";

<Input placeholder="Display name" />
A placeholder is not a label: it vanishes as soon as the person types, and it names nothing for a screen reader.

Error

Do
import { Field, Input } from "@hackcove/ui";

<Field
  label="Display name"
  htmlFor="do-error-name"
  hint="Shown on your reviews."
  error="Pick a shorter name."
>
  <Input id="do-error-name" defaultValue="A very long display name indeed" />
</Field>
error replaces the hint, in red, and is announced as an alert.
Don't

Pick a shorter name.

import { Field, Input } from "@hackcove/ui";

<Field label="Display name" htmlFor="dont-error-name" hint="Pick a shorter name.">
  <Input id="dont-error-name" defaultValue="A very long display name indeed" />
</Field>
An error passed as the hint looks like help text: it is grey and is not announced.

Usage

  • Wrap every Input, Textarea, Select and FileDrop in a Field; the label is always visible, and htmlFor is the control's id.
  • A hint is the one line of help under the control.
  • A validation message is error, not hint: it replaces the hint, in red.
  • Inside a FormGrid a Field takes one column; span="full" runs it across both.
  • A Checkbox and a DatePicker draw their own label; do not wrap them in a Field.

Accessibility

  • The label is a real <label> tied to the control by htmlFor, so clicking the text focuses the control and a screen reader reads it as the control's name.
  • An error is a <p role="alert">, so it is announced when it appears. The hint is plain text.
  • Field does not add aria-describedby or aria-invalid to the control.

API

Field

A label above a control, with a hint or an error line under it. An error replaces the hint. Inside a FormGrid, span="full" runs the field across both columns.


error

Replaces the hint when set.

Type
ReactNode

hint
Type
ReactNode

htmlFor required

The id of the control inside; the label's htmlFor.

Type
string

label required
Type
ReactNode

span

full spans every column of a FormGrid; unset takes one. It does nothing outside a FormGrid.

Type
"full"