API: components
Field
The layer that labels, describes and validates a form control, a native input or any of the form primitives, with async validation and server errors.
The layer that turns a form control into a labelled, described and validated field: a label
linked to the control, a description, error messages shown when the value is invalid, and the
field's interaction states (dirty, touched, filled, focused) for styling. It wraps a native
input (Field.Control) or one of the form primitives: a Checkbox, a
CheckboxGroup, a Switch, a
RadioGroup, a Select, a
Slider, a Combobox, a
DatePicker or a DateRangePicker.
Fieldset groups fields under a legend and disables them together;
Form validates every field on submission and shows server errors. The APG has
no pattern for it: it follows Base UI's Field (the behaviour rule).
The forms guide walks through them together.
Behaviour
- Wiring: the Label's
forand the control'saria-labelledbypoint at each other; the control'saria-describedbylists the Description and every shown Error, in document order. - Validation: the control's native validity (
required,type,pattern, ...) plus avalidate(value)function, which returns a message, a list of messages, ornull; or a promise of one of these. Its messages are also set as the control's custom validity, so a native form never submits a rejected value. The custom validity is kept current on every change, whatever the mode: the mode only decides when the result is shown. - When errors show (
validationMode):onSubmiton a submission attempt, then on every change;onBlurwhen the control loses focus;onChangeon every change. The default is the enclosing Form'svalidationMode, elseonSubmit. Focusing and leaving an empty required field shows nothing until the value changed or a submission was attempted. A change on a field that shows an error, in a mode that does not validate on every change, hides the error at once, even while an asyncvalidateruns, unless it leaves a required value missing. - Async validation: the native validity is checked at once; a native error is shown without waiting for the promise. While the latest run is pending, the field keeps what it shows. The latest run wins: an answer for an older value is dropped, even when it arrives last. A value the last resolved run already validated is not validated again: its messages are reused at once, so a submission with an unchanged value is blocked by that answer. A submission does not wait for a pending run. A rejected promise counts as no message.
- Server errors: inside a Form, the field's
namekeys the Form'serrors. While the Form has errors for it, the field is invalid and its Error with nomatchshows them; the first change of the value clears them (the Form'sonClearErrors). - Disabled:
disabled, or a disabled Fieldset around the field, disables the control. A disabled field shows no error and has neitherdata-invalidnordata-valid. - A blocked submission focuses the first invalid control: the integrated control's own element (a Checkbox, a Switch, a Select's or a picker's Trigger, a Combobox's Input, a RadioGroup's checked or first enabled Radio), or, in a Form, the first invalid field's focus target (a Slider's first Thumb, a group's first enabled Checkbox).
- Outside a field, a control behaves exactly as without it.
Anatomy
| part | element | data-primitives-part | role |
|---|---|---|---|
| Root | div | field-root | holds the field's state; renders no role |
| Label | label | field-label | names the control |
| Control | input | field-control | a native input; or one of the integrated controls instead |
| Description | p | field-description | describes the control |
| Error | div | field-error | rendered only while it is shown |
A field has one control: a Field.Control, a Checkbox.Root, a CheckboxGroup, a
Switch.Root, a RadioGroup, a Select.Root, a Slider.Root, a Combobox.Root, a DatePicker.Root or a
DateRangePicker.Root.
Usage
import { Field } from "@fragiola/primitives-react/field";
async function usernameFree(value: unknown) {
const response = await fetch(`/api/usernames/${encodeURIComponent(String(value))}`);
const { taken } = (await response.json()) as { taken: boolean };
return taken ? "This username is taken." : null;
}
<form>
<Field.Root name="email">
<Field.Label>Email</Field.Label>
<Field.Control type="email" required />
<Field.Description>We never share it.</Field.Description>
<Field.Error match="valueMissing">Enter your email.</Field.Error>
<Field.Error match="typeMismatch">Enter a valid email.</Field.Error>
</Field.Root>
<Field.Root name="username" validate={usernameFree} validationMode="onChange">
<Field.Label>Username</Field.Label>
<Field.Control required />
<Field.Error />
</Field.Root>
<button type="submit">Sign up</button>
</form>;Around a form control
The integrated controls take no name of their own: the field gives it. The Label names the
control; a click on it focuses the Control, or clicks a labelable control (it toggles a
Checkbox or a Switch). A picker's Trigger is only focused: a click would open its popup.
<Field.Root name="arrival">
<Field.Label>Arrival</Field.Label>
<DatePicker.Root required locale="en-US">
<DatePicker.Trigger>{/* the chosen date, or "none" */}</DatePicker.Trigger>
{/* its Portal, Positioner and Popup */}
</DatePicker.Root>
<Field.Description>The day you arrive.</Field.Description>
<Field.Error>Choose a date.</Field.Error>
</Field.Root>| control | labelled element | value given to validate | focused when invalid | validated element |
|---|---|---|---|---|
| Field.Control | the input | its text | the input | the input |
| Checkbox | Checkbox.Root | checked | Checkbox.Root | its hidden input |
| CheckboxGroup | the group (aria-labelledby) | the array of checked values | its first enabled Checkbox | none: validate decides |
| Switch | Switch.Root | checked | Switch.Root | its hidden checkbox input |
| RadioGroup | the group (aria-labelledby) | the value, or null | its checked Radio, else its first enabled one | its first enabled Radio's hidden radio input |
| Select | Select.Trigger | the value, or null | Select.Trigger | its hidden input |
| Slider | the Root and its Thumbs (aria-labelledby) | the number, or numbers | its first Thumb | none: validate decides |
| Combobox | Combobox.Input | the item, null, or the items with multiple | Combobox.Input | its hidden input |
| DatePicker | DatePicker.Trigger | the date, or null | DatePicker.Trigger | its hidden input |
| DateRangePicker | DateRangePicker.Trigger | the range { start, end }, or null | DateRangePicker.Trigger | its hidden input |
A picker's Trigger is named by the label, then its own content. Its aria-labelledby lists
the Label, then the Trigger itself, so the name reads "Arrival 2026-11-24" and the visible text
stays in it (WCAG 2.5.3). A Select's Trigger is a combobox, named by the label alone.
A Slider and a CheckboxGroup have no element the browser validates: their validate still runs
and shows its messages, but only a Form blocks a submission with them.
A RadioGroup has one: its first enabled radio's hidden input, since radios of one name share
their validity, so a required group blocks a native submission too.
Props
Every part also takes what the primitive contract gives:
render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which
it forwards. The names below are React's; Vue binds them the same way (:validate,
validation-mode), and Angular as inputs ([validate], validationMode).
Root
| prop | type | default | description |
|---|---|---|---|
name | string | the control's form name; it wins over the control's own, and keys the field's server errors in a Form | |
disabled | boolean | false | disables the control; no error is shown. A disabled Fieldset around it does too |
invalid | boolean | false | forces the invalid state, for an error the app found |
validate | (value) => FieldValidateResult | PromiseLike<FieldValidateResult> | a message, a list of messages, or null / undefined; or a promise of one. The value is the control's (the table above) | |
validationMode | "onSubmit" | "onBlur" | "onChange" | the Form's, else "onSubmit" | when the result is shown |
Label
| prop | type | default | description |
|---|---|---|---|
id | string | generated | the id the control's aria-labelledby points at |
Control
| prop | type | default | description |
|---|---|---|---|
value | string | the text, controlled | |
defaultValue | string | "" | the initial text, uncontrolled |
onValueChange | (value: string, details: { reason }) => void | every change (reason none) | |
id | string | generated | the input's id, the Label's for |
name | string | the input's own name (the field's wins) | |
disabled | boolean | false | disables the input |
Every other native input attribute (type, required, pattern, minlength, ...) is
forwarded.
Description
| prop | type | default | description |
|---|---|---|---|
id | string | generated | the id in the control's aria-describedby |
Error
| prop | type | default | description |
|---|---|---|---|
match | a ValidityState key, (validity: FieldValidity) => boolean, or true | without it, shown while the field is invalid; with a key ("valueMissing", "typeMismatch", ..., "customError" for validate's messages), while that key is set; with a function, while it returns true (it receives every key, and valid: null before the first validation); true: always (Angular: *fieldError="'valueMissing'") | |
id | string | generated | the id in the control's aria-describedby while shown (Angular: fieldErrorId) |
With no children, an Error shows the current messages: one as text, several as a list. These are
the field's server errors when it has some, else validate's messages, else the browser's
validationMessage. Server errors are shown only by an Error with no match (or true): a key
or a function matches the validity, which server errors are not part of.
State and part attributes
Every part, and the integrated control's main element (Checkbox.Root, the CheckboxGroup, Switch.Root, the RadioGroup, Select.Trigger, Slider.Root, Combobox.Input, a picker's Trigger), carries:
| attribute | when |
|---|---|
data-invalid | the field shows an error, invalid is set, or its Form has errors for it |
data-valid | the field validated and is valid |
data-dirty | the value differs from the value at mount |
data-touched | the control has lost focus once |
data-filled | the value is not empty (a slider is always filled) |
data-focused | the control has focus (a slider: one of its thumbs) |
data-disabled | the field is disabled, or a Fieldset around it is |
Before the first validation, neither data-invalid nor data-valid is present. The control also
gets aria-invalid="true" while invalid, and aria-describedby.
Keyboard
Nothing of its own: the control's keyboard applies. A click on the Label focuses the Control, or clicks a labelable control (it toggles a Checkbox or a Switch); a picker's Trigger is only focused.
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/field | ChangeDetails, ChangeReason, Field, FieldControlProps, FieldDescriptionProps, FieldErrorMatch, FieldErrorProps, FieldLabelProps, FieldRootProps, FieldState, FieldValidate, FieldValidationMode, RenderProp |
@fragiola/primitives-vue/field | ChangeDetails, ChangeReason, Field, FieldControlEmits, FieldControlProps, FieldDescriptionProps, FieldErrorMatch, FieldErrorProps, FieldLabelProps, FieldRootProps, FieldState, FieldValidate, FieldValidateResult, FieldValidationMode, FieldValidity, RenderSlotProps |
@fragiola/primitives-angular/field | ChangeDetails, ChangeReason, FIELDSET_CONTEXT, FORM_CONTEXT, FieldControl, FieldControlBinding, FieldControlData, FieldControlEntry, FieldControlEvents, FieldControlOptions, FieldDescription, FieldError, FieldErrorContext, FieldErrorMatch, FieldLabel, FieldRoot, FieldState, FieldValidate, FieldValidateResult, FieldValidationMode, FieldValidity, FieldsetContext, FormContext, FormErrors, FormField, ValidityKey, ValueChangeEvent, fieldControl, injectField, writeFieldAria |