Form and FormField
When to use
- You need to prevent a repeated form submission through
pending. - Ordinary controls need an associated label, a hint and a consistent way to show errors.
When not to use
- Do not look for a global
validationErrorsprop: there is no such public API. - Do not expect
FormFieldto replace thelabelof components built on React Aria:NumberInputstill requires its ownlabel.
Minimal example
<Form pending={saving} onSubmit={submit}>
<FormField label="Name" htmlFor="name" required error={errors.name} hint="Up to 80 characters">
<TextControl id="name" value={name} onChange={(e) => setName(e.target.value)} required />
</FormField>
<Button type="submit">Save</Button>
</Form>Public API
Form.onSubmitis required; the component prevents native submit and does not call the callback whilepending.pendingdisables the children through a fieldset, including fields added later. ARichTextEditorinside<fieldset disabled>(including one set by the consumer, not onlyForm pending) is disabled the same way as a native field. ACheckbox,Radio,ToggleandSliderinside such a fieldset are dimmed the same way as with their owndisabled.FormsetsnoValidate(validationBehavior="aria"): the browser's native validation does not work in it, and a requiredRichTextEditor, like a nativerequired, does not block submission — the consumer shows the error throughFormField error.FormField:labelis required,htmlForties it to the control'sid,errorreplaceshint,requiredrenders only a visual asterisk.hintPersistentkeeps a rule hint ("8–64 characters, a digit and a letter") under the field even with an error. The error goes under it, and both describe the field; an explanatory hint ("how should we address you") is still replaced by the error (ADR-0065).erroris carried all the way to the field: kit fields read the invalid flag from context and setaria-invalidthemselves, so the border turns red too, not only the text under the field. An explicitaria-invalidon the field wins — the consumer knows more about their field than the form does.
The layout axis on FormField
layout="stack" (the default) — the label above the field, as it always was;
layout="side" — the label in the left column, the field, the note and the error on the right.
The axis lives on the field, not on the layout: FormField renders the label,
and the layout cannot move it into the left column — it sits inside
its own flex. The analysis is in
ADR-0025.
Without the prop, the field takes the axis from the nearest FormLayout; without
a layout it is stack, and the markup does not change by a single node (this is
checked by a test on the whole markup). An explicit prop wins over context.
The width of the label column is the --form-label-w variable with a default of
12rem; subgrid is not used, because it requires an outer grid with a known
number of tracks, and the field must also work on its own:
<div style={{ "--form-label-w": "8rem" }}>
<FormField label="Narrow column" htmlFor="x" layout="side">
<TextControl id="x" />
</FormField>
</div>A long label wraps inside its column and does not move the field.
In the side layout, the field, the note and the error sit in one column as a whole, not one cell per node. A grid places children into cells, not by meaning, and a second child (a "from" and "to" range, a field with a button next to it) would otherwise land in the next row under the label.
Accessibility and forms
- Set both
htmlForandid; a placeholder does not replace a label. - The FormField error is announced with
role="alert"; the field itself must also be marked as required, throughrequiredoraria-required. - The hint and error are tied to the field through
aria-describedby.FormFieldgenerates theid, and the kit fields pick it up —TextControland everything built on it (MaskedInput,PhoneInput,PasswordInput,SearchControl),NumberInput,Select,Combobox,DatePicker,TagInputand others. This does not override the field's own description: a description passed as a prop is extended, not replaced. - The role and the description share the duties:
role="alert"announces the error the moment it appears (it appears after submit, when focus has already moved away), while the description is read on returning to the field. The analysis is in ADR-0026. - The value format is a hint, not a placeholder: a placeholder disappears at the first character, that is, exactly when it is needed to check what was typed.
- The label publishes its own
idthrough context.<label for>names only the nativeinput,textarea,select; a field that is not among them (thecontenteditableofRichTextEditor) refers to the label througharia-labelledby. SoFormFieldputs a stableid(fromuseId) on the<label>and hands it to the fields the same way as the description and the invalid state. OnlyRichTextEditorreads it; the consumer does not need to know the label'sid, andhtmlForis still set — native fields need it. TheFormFieldmarkup changes by one attribute — the label'sid. - Sources:
src/components/inputs/Form.tsx,src/components/inputs/FormField.tsx; testsform.test.tsx,form-field.test.tsx.
TypeScript types
FormProps
Prop
Type
FormFieldProps
Prop
Type
Plus DOM attributes, such as onChange, onClick, className — 278 in total; the table does not repeat them.