-
Notifications
You must be signed in to change notification settings - Fork 2
feat(form): scroll to first invalid field on validation error #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type {Ref} from 'vue'; | ||
|
|
||
| import {watch} from 'vue'; | ||
|
|
||
| import type {ValidationErrors} from './types'; | ||
|
|
||
| /** The mark `@script-development/ui-inputs` renders from `:invalid`. */ | ||
| const DEFAULT_TARGET = '[aria-invalid="true"]'; | ||
|
|
||
| /** | ||
| * On every `errors` change, scroll the first invalid field into view (the mark the | ||
| * presentation layer sets); a no-op when nothing is marked. | ||
| * | ||
| * - `root` scopes the query to one form's subtree — omitted: document-wide; `null`: no | ||
| * scroll, never falling back to document. | ||
| * - `target` is the selector for the mark (default `[aria-invalid="true"]`); pass your | ||
| * own when your inputs mark errors with a class instead. | ||
| * - `behavior` is `'auto'` under `prefers-reduced-motion: reduce` — a JS `scrollIntoView` | ||
| * behavior is not subject to the CSS media query, so it is honoured here explicitly. | ||
| * | ||
| * `flush: 'post'` fires after the mark paints. Call inside `setup()` (as `useForm` | ||
| * does) so the watcher stops on unmount. | ||
| */ | ||
| export const useScrollToFirstError = ( | ||
| errors: Ref<ValidationErrors>, | ||
| root?: Ref<HTMLElement | null>, | ||
| target: string = DEFAULT_TARGET, | ||
| ): void => { | ||
| watch( | ||
| errors, | ||
| () => { | ||
| const scope = root === undefined ? document : root.value; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor — default-on + document-wide + a shared Cheapest hardening: skip targets that are not rendered (
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same root cause as the two crit threads above (shared httpService / document-wide query race) — agree scrollRoot needs to be documented as required for dialog-hosted forms, not left implicit. |
||
| const field = scope?.querySelector(target); | ||
| if (!field) return; | ||
|
|
||
| const behavior = matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth'; | ||
| field.scrollIntoView({behavior, block: 'center'}); | ||
| }, | ||
| {flush: 'post'}, | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document-wide scrolling selects another form's invalid field before the submitting form
When scrollRoot is omitted, useScrollToFirstError queries document for the first invalid element. That query ignores which useForm instance received the 422 response. An earlier invalid control in form B can scroll the user away from form A after A's 422.
crit · finding
bd0c2be21ef3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed, same root cause as the sibling thread on form.ts:32 — document-wide querySelector has no notion of which useForm submitted, so an earlier invalid control from an unrelated mounted form wins the scroll. scrollRoot fixes it but is opt-in; nothing here forces a multi-form page to pass it.