Components / Feedback and display
Field
Adds a label, help text and an error message to any control and wires them together for assistive tech. Your control is rendered through a render prop that receives the ids and state. Checkbox and Switch carry their own label, so they don't need Field.
When to useLink to "When to use"
- Around a text input, select or any custom control that needs a visible label.
- To show validation errors consistently.
- With
groupto label a set of controls such as a radio group.
Use something else when:
UsageLink to "Usage"
import { Field, InputBase } from "@arshad-shah/cynosure";
<Field label="Email" description="We never share it." error={error} required>
{(f) => <InputBase id={f.id} aria-describedby={f.describedBy} invalid={f.invalid} required />}
</Field>The render prop gives you id, describedBy and invalid. Pass them to the control.
Errors and requiredLink to "Errors and required"
error is shown below the control, announced with role="alert", linked to the control and marks it invalid. required draws the marker as a shape, not a typed asterisk; also pass required to the control itself.
Settings rowsLink to "Settings rows"
layout="split" puts the label and description at the start and the control at the end of one row, centred on the text, as in a settings page. When the field is too narrow for both, the control moves under the text. width gives the control one of three widths (or the whole field), so a column of settings lines up whatever the labels. Labels, descriptions and errors stay linked to the control. Every control built on Field takes both props too (FieldLayoutProps): TextField, Select, Combobox, TagInput, PathField, NumberField, DateField, TimeField, ColorField, TextArea and HotkeyField.
<Field layout="split" width="md" label="Font" description="Used in the query editor.">
{(f) => <InputBase id={f.id} aria-describedby={f.describedBy} />}
</Field>
<Select layout="split" width="md" label="Theme" description="Light, dark or the system's." options={themes} />GroupsLink to "Groups"
With group, Field renders a fieldset and the label names the whole group instead of one control.
PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
labelRequired | Visible label; it names the control. | ReactNode | |
childrenRequired | Renders the control from the ids and state in field. | (field: FieldRender) => ReactNode | |
description | Help text shown below the control and read out with it. | ReactNode | |
error | Shown below the control, announced, and marks the control invalid. | ReactNode | |
required | Draws the required marker; pass required to the control too. | boolean | |
size | Size of the label text, matching the control's size (an xs control has the sm label). Default "md". | DenseSize | "md" |
labelHidden | Keep the label for assistive tech but take no space (toolbars, dense rows). Prefer visible labels. | boolean | |
group | Label a group of controls (radio groups) instead of one control. | boolean | |
id | Id for the control; the label, description and error ids are built from it. Default: a generated id. | string | a generated id |
className | Added to the root element, for layout. | string | |
style | Layout only. | CSSProperties | |
layout | stack puts the label above the control and the description and error below it. split puts the label and description at the start and the control at the end of one row (a settings row); when the field is too narrow for both, the control moves under the text. Default "stack". | "stack" | "split" | "stack" |
width | Width of the control, so the controls of a column of fields line up: "sm", "md" and "lg" are the --cy-field-control-width-* tokens (never wider than the field), "full" is the whole field. Default: the control's own width (the whole field in the stacked layout for TextField and the other fields). | FieldWidth |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
Behaviour and accessibilityLink to "Behaviour and accessibility"
- Label: a
labellinked to the control, or the group label whengroupis set. - Hidden label:
labelHiddenkeeps the label for assistive tech but takes no space. Prefer visible labels. - Description and error: linked through
aria-describedby. The error also announces itself. - Size:
sm,md(default) orlg. - Split layout: the order in the page stays label, description, control, then error, so screen readers and the Tab key meet them as in the stacked layout. Right-to-left text mirrors the row.
ThemingLink to "Theming"
Tokens: --cy-field-gap, --cy-field-label-weight, --cy-field-label-color, --cy-field-description-color, --cy-field-error-color and --cy-field-required-color. Control widths: --cy-field-control-width-sm, -md and -lg (160, 256 and 384px). The split layout keeps the text at least --cy-field-split-text-min (192px) wide before the control moves under it.
The field skin: --cy-field-control-border, --cy-field-control-bg and --cy-field-control-radius style the box of every field at once (TextField and the fields built on it, and HotkeyField), and --cy-field-control-sunken-bg the fill of a box that cannot be edited (a read-only TextField, a disabled HotkeyField). Each control's own tokens (--cy-text-field-bg…) still win over them.
createTheme({ components: { field: { gap: "8px", labelWeight: "600" } } });className and style are for layout only.
RelatedLink to "Related"
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.