API: components
Form
A native form that validates its fields on submission, focuses the first invalid one, shows server errors and sets its fields' validation mode.
A native <form> that runs the Field layer's validation itself: its
submission validates every field, shows their errors, focuses the first invalid field and is
blocked while one is invalid. It shows server errors on the fields they name, and gives its fields
a default validationMode. The APG has no form pattern; it follows Base UI's Form (the
behaviour rule). Form is one component, not a namespace.
Controls the browser cannot validate (a Slider, a
CheckboxGroup) block a Form's submission through their field's
validate, which a plain <form> cannot do.
Behaviour
novalidate: the Form validates, not the browser, and shows no browser bubble.- A submission (a submit button, Enter in a text field,
requestSubmit()): every Field inside validates as on a submission attempt, and shows its errors; anonSubmitfield validates on every change from then on. While a field is invalid (its validity,invalid, or server errors for it), the submission is blocked before any listener of yours runs, and focus moves to the first invalid field in document order (a text input's text is selected). Otherwise yoursubmitlistener runs: it may stillpreventDefaultand send theFormDatawithfetch. - Server errors:
errorsis{ [fieldName]: string | string[] }, keyed by the Fields'name. The Form keeps its own copy; a new object replaces it. A field with errors is invalid and its Error with nomatchshows them. Whenerrorschanges after a submission went on (the server answered), focus moves to the first invalid field. - Clearing: the first change of a field's value hides its server errors at once, and
onClearErrorsreceives the errors left (that field's key removed), whether or not you pass the new object back. validationMode: the default of every Field inside it; a Field's own wins.- A form reset clears neither the Form's errors nor the fields' states, as in Base UI.
Anatomy
| part | element | data-primitives-part | role |
|---|---|---|---|
| Form | form | form-root | none of its own; a form with an accessible name (aria-label) |
Usage
import { Field } from "@fragiola/primitives-react/field";
import { Form, type FormErrors } from "@fragiola/primitives-react/form";
const [errors, setErrors] = useState<FormErrors>({});
<Form
aria-label="Create account"
validationMode="onBlur"
errors={errors}
onClearErrors={setErrors}
onSubmit={async (event) => {
event.preventDefault();
const response = await fetch("/api/accounts", {
method: "POST",
body: new FormData(event.currentTarget),
});
if (!response.ok) setErrors((await response.json()) as FormErrors);
}}
>
<Field.Root name="username">
<Field.Label>Username</Field.Label>
<Field.Control required />
<Field.Error />
</Field.Root>
<button type="submit">Create account</button>
</Form>;Props
It 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 (action, method, ...). Vue binds them as :errors, validation-mode and
@clear-errors; Angular as [errors], validationMode and (clearErrors).
Form
| prop | type | default | description |
|---|---|---|---|
errors | FormErrors ({ [name]: string | string[] }) | {} | server errors, keyed by the Fields' name |
onClearErrors | (errors: FormErrors) => void | a field's value changed while the Form had errors for it: the errors left | |
validationMode | "onSubmit" | "onBlur" | "onChange" | "onSubmit" | the default of every Field inside it |
onSubmit (react) | (event: FormEvent<HTMLFormElement>) => void | the native submit event, only for a submission that goes on (Vue: @submit; Angular: (submit), both native) |
State and part attributes
| part | attributes |
|---|---|
| Form | novalidate |
The fields inside carry their own state (Field); a field with server errors
carries data-invalid and aria-invalid="true".
Keyboard
Nothing of its own: Enter in a text field submits, natively.
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/form | FieldValidationMode, Form, FormErrors, FormProps, FormState, RenderProp |
@fragiola/primitives-vue/form | FieldValidationMode, Form, FormEmits, FormErrors, FormProps, FormState, RenderSlotProps |
@fragiola/primitives-angular/form | FieldValidationMode, Form, FormErrors |