How it works
One file exports Form, FormField and useFormApi. FormField is a field: label above, a required asterisk, and a 20px message line that help text and errors share, so nothing below moves. Form owns values and rules (required, min, max, pattern, async validator): a field is checked on blur, re-checked on each change while it shows an error, and on submit every mounted field is checked, focus goes to the first invalid one and the count is announced.
Use it for
- Forms that get submitted: adding a product, editing a profile, checkout.
- Only the field shell (label on top, required asterisk, message slot): use FormField alone, without Form.
- A native input, select or textarea that should match the standard input look: add ds-form__control.
- Errors only the server can know (name taken, wrong code): attach them with form.setError after submit.
Look elsewhere when
- A single input that applies as you type (search box, inline table edit): use the input alone.
- Filters: they apply on change and need no validation or submit button.
- Single controls such as a code input, slider or rating: use that component, and put it inside a FormField function child when it joins a form.
Usage
import { Form, FormField } from './FormField.jsx'
const rules = {
name: [{ required: true, message: 'Enter a product name' }, { max: 16, message: 'At most 16 characters' }],
price: [{ required: true }, { pattern: /^\d+(\.\d{1,2})?$/, message: 'Digits only, up to two decimals' }],
}
<Form
defaultValues={{ name: '', price: '' }}
rules={rules}
onSubmit={async (values, form) => {
const res = await createProduct(values)
if (res.nameTaken) form.setError('name', 'This name is already listed', true)
}}
>
{(form) => (
<>
<FormField name="name" label="Product name" help="The name shoppers see">
<input className="ds-form__control" />
</FormField>
<FormField name="price" label="Price">
<input className="ds-form__control" inputMode="decimal" />
</FormField>
<div className="ds-form__actions">
<button type="submit" className="ds-form__btn ds-form__btn--primary" aria-busy={form.submitting || undefined}>
Add product
</button>
</div>
</>
)}
</Form>
// Connect another control: write children as a function
<FormField name="quantity" label="Quantity">
{(field) => <Stepper id={field.id} value={field.value} onChange={field.onChange} onBlur={field.onBlur} />}
</FormField>
// Without Form: only the field shell
<FormField label="Note" help="Optional" error={tooLong ? 'At most 50 characters' : undefined}>
<textarea className="ds-form__control" rows={2} />
</FormField>Props
| Prop | Type | Default | What it does |
|---|---|---|---|
Form.values | object | — | Form values (controlled). If omitted, the form keeps its own. |
Form.defaultValues | object | — | Initial values when uncontrolled; also what reset returns to. |
Form.onValuesChange | (values, changed) => void | — | Called when any value changes; changed holds only the field that just changed. |
Form.rules | { [name]: Rule | Rule[] } | — | Rules per field: { required, min, max, pattern, validator, message }. Checked in order; the first failure is reported. |
rule.validator | (value, values) => string | boolean | void | Promise | — | Custom check: a returned string is the error, false uses the rule's message, anything else passes. May return a Promise. |
Form.onSubmit | (values, form) => void | Promise | — | Called only when everything passes. While a returned Promise is pending, submitting is true and resubmits are ignored. |
Form.disabled | boolean | false | Disables the whole form: controls get disabled and it cannot be submitted. |
Form.size | 'md' | 'lg' | 'md' | md: 44px controls with 15px text. lg: 14px labels, 52px controls with 16px text, for mobile. |
Form.labels | object | — | Replaces built-in validation messages (required, requiredWithLabel, minLength, maxLength, invalidFormat, validationFailed, invalidCount). Shallow-merged over the English defaults. |
Form.children | ReactNode | (form) => ReactNode | — | A function receives { values, errors, submitting, setValue, setError, validate, reset }, for example to draw a loading submit button. |
form.setError | (name, message?, focus?) => void | — | Attaches an error to a field by hand, such as one returned by the server. focus moves focus there and announces it. It clears when that field's value changes. |
form.validate / form.reset | (name?) => Promise<boolean> / (values?) => void | — | Validate one field or all (without moving focus); return to the initial values and clear all errors. |
useFormApi | () => form | — | Returns the same form object inside any component under <Form>, such as a standalone submit button. |
FormField.name | string | — | The key in values and rules. Without it the field is only a shell with a label and message slot. |
FormField.label | ReactNode | — | Label shown above the control. |
FormField.required | boolean | — | Adds a danger-coloured asterisk and required on the control. Set automatically when the rules include required. |
FormField.help | ReactNode | — | Help text in the message slot under the control. An error replaces it. |
FormField.error | ReactNode | — | Sets the error text directly: without a Form, or to override the form's result. |
FormField.size / disabled | 'md' | 'lg' / boolean | — | Size and disabled for this field only. Otherwise it follows the Form. |
FormField.children | ReactElement | (control) => ReactNode | — | A native input, select or textarea (id, value and aria attributes are wired automatically), or a function receiving { id, value, onChange, onBlur, invalid, describedBy, required, disabled, validating }. |
ds-form__control | CSS class | — | Put on a native input, select or textarea for the standard filled look: --ds-surface-2 with a faint hairline, white with an accent line and halo on focus, a danger hairline and tint when aria-invalid. 44px high; add --sm (36) or --lg (52) for other sizes. |
ds-form__row, ds-form__actions, ds-form__btn | CSS classes | — | A row of side-by-side fields; the bottom button row (right-aligned, 8px gap); 44px buttons with 12px corners (--primary ink, --accent accent fill, --secondary soft grey, --text; --lg is 52px; spinner on aria-busy). |
...rest | FormHTMLAttributes / HTMLAttributes | — | On Form they go to the <form>; on FormField to the field's root element. |
Accessibility
- The label is a real <label> pointing at the control, so clicking it focuses the input.
- Required fields get required on the control (the asterisk is not read); an invalid control has aria-invalid="true" and aria-describedby pointing at the error (or the help text when there is none).
- A blur error is announced; a failed submit announces how many fields need attention and moves focus to the first one, scrolled into view.
- Errors are not only a red hairline: the reason is written below with an icon.
- Browser validation bubbles are off (the form has novalidate), so errors look the same everywhere.
- With reduced motion, error text does not fade in and scrolling to the first error is not smooth.
Motion
Error text fades in over 220ms ease-out and drops 4px into place, and its icon pops in with a 320ms spring (opacity and transform only: the line is reserved, so nothing else moves). On focus a control turns white in 160ms and its accent line and 4px halo grow in over 220ms. Buttons press to 0.98. The spinner turns once per 0.8s (1.6s with reduced motion, when the text and icon also appear without motion).
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
FormField.jsxform-field.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





