Fragiola

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.

Sign-up email
Gallery

Behaviour

  • Wiring: the Label's for and the control's aria-labelledby point at each other; the control's aria-describedby lists the Description and every shown Error, in document order.
  • Validation: the control's native validity (required, type, pattern, ...) plus a validate(value) function, which returns a message, a list of messages, or null; 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): onSubmit on a submission attempt, then on every change; onBlur when the control loses focus; onChange on every change. The default is the enclosing Form's validationMode, else onSubmit. 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 async validate runs, 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 name keys the Form's errors. While the Form has errors for it, the field is invalid and its Error with no match shows them; the first change of the value clears them (the Form's onClearErrors).
  • Disabled: disabled, or a disabled Fieldset around the field, disables the control. A disabled field shows no error and has neither data-invalid nor data-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

partelementdata-primitives-partrole
Rootdivfield-rootholds the field's state; renders no role
Labellabelfield-labelnames the control
Controlinputfield-controla native input; or one of the integrated controls instead
Descriptionpfield-descriptiondescribes the control
Errordivfield-errorrendered 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>
controllabelled elementvalue given to validatefocused when invalidvalidated element
Field.Controlthe inputits textthe inputthe input
CheckboxCheckbox.RootcheckedCheckbox.Rootits hidden input
CheckboxGroupthe group (aria-labelledby)the array of checked valuesits first enabled Checkboxnone: validate decides
SwitchSwitch.RootcheckedSwitch.Rootits hidden checkbox input
RadioGroupthe group (aria-labelledby)the value, or nullits checked Radio, else its first enabled oneits first enabled Radio's hidden radio input
SelectSelect.Triggerthe value, or nullSelect.Triggerits hidden input
Sliderthe Root and its Thumbs (aria-labelledby)the number, or numbersits first Thumbnone: validate decides
ComboboxCombobox.Inputthe item, null, or the items with multipleCombobox.Inputits hidden input
DatePickerDatePicker.Triggerthe date, or nullDatePicker.Triggerits hidden input
DateRangePickerDateRangePicker.Triggerthe range { start, end }, or nullDateRangePicker.Triggerits 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

proptypedefaultdescription
namestringthe control's form name; it wins over the control's own, and keys the field's server errors in a Form
disabledbooleanfalsedisables the control; no error is shown. A disabled Fieldset around it does too
invalidbooleanfalseforces 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

proptypedefaultdescription
idstringgeneratedthe id the control's aria-labelledby points at

Control

proptypedefaultdescription
valuestringthe text, controlled
defaultValuestring""the initial text, uncontrolled
onValueChange(value: string, details: { reason }) => voidevery change (reason none)
idstringgeneratedthe input's id, the Label's for
namestringthe input's own name (the field's wins)
disabledbooleanfalsedisables the input

Every other native input attribute (type, required, pattern, minlength, ...) is forwarded.

Description

proptypedefaultdescription
idstringgeneratedthe id in the control's aria-describedby

Error

proptypedefaultdescription
matcha ValidityState key, (validity: FieldValidity) => boolean, or truewithout 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'")
idstringgeneratedthe 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:

attributewhen
data-invalidthe field shows an error, invalid is set, or its Form has errors for it
data-validthe field validated and is valid
data-dirtythe value differs from the value at mount
data-touchedthe control has lost focus once
data-filledthe value is not empty (a slider is always filled)
data-focusedthe control has focus (a slider: one of its thumbs)
data-disabledthe 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

entryexports
@fragiola/primitives-react/fieldChangeDetails, ChangeReason, Field, FieldControlProps, FieldDescriptionProps, FieldErrorMatch, FieldErrorProps, FieldLabelProps, FieldRootProps, FieldState, FieldValidate, FieldValidationMode, RenderProp
@fragiola/primitives-vue/fieldChangeDetails, ChangeReason, Field, FieldControlEmits, FieldControlProps, FieldDescriptionProps, FieldErrorMatch, FieldErrorProps, FieldLabelProps, FieldRootProps, FieldState, FieldValidate, FieldValidateResult, FieldValidationMode, FieldValidity, RenderSlotProps
@fragiola/primitives-angular/fieldChangeDetails, 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