Repository navigation
[form] Focus the first invalid field in document order - #5287
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,238.56 ms -74.05 ms(-5.6%) | Renders: 78 (+0) | Paint: 1,940.45 ms -97.04 ms(-4.8%) No significant changes — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
flaviendelangle
left a comment
There was a problem hiding this comment.
PR review
This PR fixes invalid-field focus drifting away from the first DOM error by (1) making field re-registration update the fields Map entry in place instead of delete+re-add, and (2) selecting the invalid control by document position rather than registration order. The logic is correct, the DOM-position comparison uses the right compareDocumentPosition argument order, and the disconnected-tree fallback is genuinely exercised by the shadow-root test. Nothing merge-blocking; I found only two minor observations.
Bugs (0)
No findings.
The two behavioral changes hold up under scrutiny:
comesBeforeInSameTree(control, firstControl)returns true exactly whencontrolprecedesfirstControlin the same tree (element.compareDocumentPosition(reference) & FOLLOWINGmeansreferencefollowselement), so the loop keeps the DOM-earliest control. TheDOCUMENT_POSITION_DISCONNECTEDguard correctly falls back to registration order for separate roots — verified by the shadow-root test, which would focusbinstead ofaif the disconnected branch were mishandled.- Splitting
useRegisterFieldControlinto a register/re-register effect plus an unmount-only cleanup effect preserves every prior invariant: disable still deregisters (registerFieldControl(source, undefined)), unmount still deregisters (stablesourceRef/registerFieldControldeps), andidchanges still delete the previous entry insideregister. The removed per-render cleanup only elided a transient deregister that was immediately overwritten.
Tests (0)
No findings.
Coverage is strong: value-change stability, keyed-reorder DOM ordering (correctly rendered with strict: false, since Strict Mode's effect re-runs would re-register in DOM order and mask the divergence), and the disconnected-tree fallback each pin a distinct branch.
Simplifications (0)
No findings.
comesBeforeInSameTree overlaps with sortByDocumentPosition in CompositeList.tsx, but the added DOCUMENT_POSITION_DISCONNECTED guard is a real semantic difference, so keeping it local is reasonable rather than duplication worth consolidating.
Docs (0)
No findings.
Verdict
Approve — correct, well-tested fix with no blocking or non-blocking issues; the only NumberField test failure is a pre-existing ICU/locale issue on master, unrelated to this diff.
Two optional notes for the maintainer:
- ℹ️
Form.test.tsx— the two newrender(<App />, ...)/render(<Form>...)calls (value-change and keyed-reorder tests) omitawait, while the rest of the file (including the line changed at ~47) usesawait render(...). Harmless sincerenderreturns a synchronously-usable result, but inconsistent. - ℹ️
Form.tsx:63-64— if a still-registered field'scontrolRefever pointed at a node detached from the document while later invalid controls were connected, the disconnected guard would keep the detached control and.focus()would silently no-op. Not reachable through normal mount/unmount (unmount deregisters), so no action needed — just the one edge thecompareDocumentPositionfallback doesn't rank.
🤖 Review generated with Claude Code
Closes #5284
After an invalid submit,
Formfocuses the first invalid field by iterating the internal fields Map in insertion order. That order drifts: a control re-registers whenever its value changes, and the deregister/re-register cycle deletes and re-adds the Map entry, moving the field to the end. Toggling a required checkbox and submitting again then focuses a later field instead of the first invalid one in the DOM, which degrades the experience for keyboard and screen reader users relying on focus landing on the first error.Two changes:
focusFirstInvalidselects the invalid control by document position rather than registration order, since registration order can also diverge from DOM order without any re-registration (keyed fields reordered without remounting, portals). The comparison applies only when the controls are in the same tree; for disconnected controls (e.g. separate shadow roots), wherecompareDocumentPositionis implementation-specific, the now-stable registration order is kept.The existing contract is preserved: submission stays blocked when a field is invalid without a focusable control. The keyed-reorder regression test renders without Strict Mode, which re-runs effects when a keyed subtree moves and would re-register the fields in DOM order, masking the divergence the test pins.