Error summary
A form’s errors listed at its top, each a link that takes you to its field.
Demo
Usage
import { ErrorSummary, Field, Input } from '@tuttn/slant'
// After a submit: the errors, each pointing at its field by id. submitCount
// moves focus back to the summary after every failed submit.
<form noValidate onSubmit={publish}>
<ErrorSummary submitCount={submits} errors={[
{ field: 'title', message: 'Enter a title' },
{ field: 'slug', message: 'This slug is taken' },
]} />
<Field invalid={!!titleError}>
<Field.Label>Title</Field.Label>
<Input id="title" value={title} onChange={…} />
<Field.Error>{titleError}</Field.Error>
</Field>
…
</form>
// A field can be a control's name in the same form instead of an id: the link
// then goes to the control carrying it (for a Select or a Switch, the visible
// control next to its hidden input).
<ErrorSummary errors={[{ field: 'duration', message: 'Choose a duration' }]} />
// Your own heading and a line under it, at the level the page needs.
<ErrorSummary errors={errors} headingLevel={3}
title="The post wasn’t published" description="Fix these and publish again." />
// Errors that arrive without the reader's doing (a server's, on load) leave
// focus where it is; the next failed submit still moves it to the summary.
<ErrorSummary errors={serverErrors} autoFocus={false} submitCount={submits} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
errors | readonly ErrorSummaryItem[] | — | The form's errors, in the order of the fields. While the list is empty the summary renders nothing. |
title | ReactNode | messages.errorSummaryTitle(count): "There is a problem", "There are 2 problems" | The heading over the list, which also names the summary. |
description | ReactNode | — | A line under the heading, before the list: what happened, or what the reader can do. |
headingLevel | 2 | 3 | 4 | 2 | The heading's level, to fit the page's outline. |
submitCount | number | — | The number of times the form was submitted. Each change moves focus to the summary again while it has errors, so a second failed submit lands the reader on the list again. |
autoFocus | boolean | true | Moves focus to the summary when it appears with errors (on mount with errors, or when the list stops being empty). Turn it off for errors that arrive without the reader's doing, such as a server's on load; a change of `submitCount` still moves focus. |
ErrorSummaryItem | { field: string; message: ReactNode } | — | One error: `field` is an element id, else the name of a control in the same form (then in the document); `message` is the link’s text. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, the ref and other attributes go on the root, the element that takes focus. |
Keyboard
| Tab | moves from the summary, once it has focus, through its links in order |
| Enter | on a link, scrolls its field to the middle of the view and moves focus into it |
Accessibility
- The content sits in a
role="alert"wrapper, so it is read when it appears. The root is agroupnamed by the heading througharia-labelledby, withtabIndex={-1}, so it can take focus without being a Tab stop. - It takes focus when it appears with errors (
autoFocus, on by default) and again wheneversubmitCountchanges while it has errors (whateverautoFocussays), so a keyboard or screen-reader user lands on the list of what to fix. The browser scrolls it into view. - Each error is a real link (
href="#field"), so it is reachable by Tab and listed with the page’s links. Following it focuses the field itself, withpreventScroll, after scrolling its Field to the centre (smoothly unless the reader asks for reduced motion). A target that takes no focus itself, such as a TimeField’s group or a Repeater, gets its first Tab stop focused instead. - Give each field its own
Field.Errorwith the same words, and mark the Fieldinvalid: the summary says where the problems are, the field says what is wrong while the reader is on it. - The heading is the root’s
messages.errorSummaryTitle(count)(“There are 2 problems”) unless you passtitle;headingLevelfits it into the page’s outline.
When it shows
The summary renders nothing while errors is empty, so it can sit at the top of the form from the start. Fill it on submit, not as the reader types: a list that rewrites itself under each keystroke would be read out again and again. Clear an error from the list on the next submit, and from its Field as soon as it is fixed, if you like.
Following a link
A click or Enter on a link keeps the browser from jumping to the hash and does the move itself. It looks the target up by id, then by name inside the summary’s closest <form>, then by name anywhere in the document. A name that only a hidden input carries points at that input’s parent, so focus goes to the visible control beside it. A link whose target is gone does nothing.