Field
Label, control, helper text and errors composed into one accessible form field. Vertical, horizontal or responsive.
Installation
pnpm dlx nachui add fieldAnatomy
Field generates an id and shares it with its parts. Label points at it, Description and Error register their ids, and Control clones the element inside it with id, aria-describedby, aria-invalid and disabled. Anything that renders a native control works: Input, Textarea, Select, Switch, Checkbox, Radio or a plain element. For a component that cannot take a child, call useField() and spread the result.Composition
Group spaces fields in a form. Set and Legend wrap related controls in a real fieldset. Content keeps a label and its description together when the control sits next to them.Variants
Default
A vertical form: label, control, description.
Horizontal
orientation="horizontal" lines the parts up on one row. Put the label and description in Content so they stack and the control stays at the end.Post to the team channel when a production deploy finishes.
Page the on-call engineer when an alert fires.
A summary of usage and spend every Monday.
Fieldset
Radio or checkbox options grouped under a legend. Each option is its own
Field so the label clicks the control.With error
Pass
invalid to the root and the errors to Error. Strings, objects with a message and empty values are all accepted, so a form library's error array works as is. One message renders as text, several as a list.Responsive
orientation="responsive" is vertical by default and becomes horizontal when the enclosing Field.Group is at least 28rem wide. It uses a container query, so it follows the form, not the viewport.Hook
useField returns id, aria-describedby, aria-invalid and disabled for the nearest Field.API Reference
Field
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | "vertical" | "horizontal" | "responsive" | "vertical" | Layout of the parts |
invalid | boolean | false | Marks the field and its control invalid |
disabled | boolean | false | Dims the field and disables the control |
id | string | generated | Id given to the control |
name | string | - | Stored as data-name for styling and testing |
className | string | - | Additional CSS classes |
Field.Control
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactElement | - | The single element that receives the props |
Field.Error
| Prop | Type | Default | Description |
|---|---|---|---|
errors | Array<string | { message?: string } | null> | - | Messages to show. Duplicates are dropped |
children | ReactNode | - | Custom content instead of errors |
Field.Legend
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "legend" | "label" | "legend" | Larger heading or label sized |
Field.Label, Field.Description, Field.Content, Field.Title, Field.Set, Field.Group, Field.Separator
Presentational parts. Each accepts
className and the attributes of the element it renders. Separator takes optional children rendered in the middle of the line.Found something to improve?
Notice a bug, typo, or missing detail on this page? Help us make the documentation better by opening a GitHub issue.