Skip to content
Alea0.1

Forms

Form

Validation on submit, focus on the first problem, and server errors shown on their fields.

Ant Design: Form

import { Form, FormActions, ErrorSummary } from '@alea/ui';

Examples

Form with server errors

Try taken@example.com: the server answer appears on the field.

Error summary

For long forms: what to fix, with links to the fields.

Props

Form props

A form that validates on submit, moves focus to the first problem, and shows server errors on their fields.

PropTypeDefaultDescription
childrenrequiredReactNode-The children of the component.
gapnumber20Space between fields. Default 20 px.
onSubmit((values: Record<string, FormDataEntryValue>, event: FormEvent<HTMLFormElement>) => void | Record<string, string | string[]> | Promise<...>)-Called with the form values when every field is valid. Return server errors by field name to show them on the fields; a promise shows the loading state.

FormActions props

The buttons at the end of a form.

PropTypeDefaultDescription
align"start" | "end" | "stretch"'start'start (default), end, or stretch for full-width buttons.
isLoadingboolean-Shows the spinner on the submit button.
secondaryReactNode-A cancel or secondary button.
submitLabelReactNode'Save'The submit button text. Default "Save".

ErrorSummary props

PropTypeDefaultDescription
errorsrequiredRecord<string, string>-The errors by field id: { email: 'Use a valid email.' }. Each links to its field.
titleReactNode'Fix these to continue'The heading. Default "Fix these to continue".

Accessibility

  • On submit the first invalid field gets focus and its error is read.
  • ErrorSummary is announced and links to each field.

Keyboard

KeyDoes
Enter (in a field)Submits
TabMoves between fields and the summary links

Do and don't

Do

  • Return { field: message } from onSubmit for server errors.

Don't

  • Don't disable the submit button to show a form is incomplete.
Tested with axe, keyboard and screen reader roles