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>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" />
Error
Do
Pick a shorter name.
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>hint looks like help text: it is grey and is not announced.Usage
- Wrap every
Input,Textarea,SelectandFileDropin aField; thelabelis always visible, andhtmlForis the control'sid. - A
hintis the one line of help under the control. - A validation message is
error, nothint: it replaces the hint, in red. - Inside a
FormGridaFieldtakes one column;span="full"runs it across both. - A
Checkboxand aDatePickerdraw their ownlabel; do not wrap them in aField.
Accessibility
- The label is a real
<label>tied to the control byhtmlFor, so clicking the text focuses the control and a screen reader reads it as the control's name. - An
erroris a<p role="alert">, so it is announced when it appears. The hint is plain text. Fielddoes not addaria-describedbyoraria-invalidto 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.
errorReplaces the hint when set.
- Type
ReactNode
hint- Type
ReactNode
htmlFor requiredThe id of the control inside; the label's htmlFor.
- Type
string
label required- Type
ReactNode
spanfull spans every column of a FormGrid; unset takes one. It does nothing outside a FormGrid.
- Type
"full"