Skip to content

fix: select the autofocused item when selectOnFocus is true - #10396

Open
xevrion wants to merge 1 commit into
adobe:mainfrom
xevrion:fix/select-autofocused-item-on-focus
Open

fix: select the autofocused item when selectOnFocus is true#10396
xevrion wants to merge 1 commit into
adobe:mainfrom
xevrion:fix/select-autofocused-item-on-focus

Conversation

@xevrion

@xevrion xevrion commented Jul 30, 2026

Copy link
Copy Markdown

Closes #4391

The autoFocus effect in useSelectableCollection calls setFocused/setFocusedKey but never touches the selection, which makes it the only focus path in the file that ignores selectOnFocus; the focus-in handler, the Home and End handlers, and item navigation all call replaceSelection. With autoFocus="first" or "last" plus selectOnFocus, the item is focused but not selected.

The fix mirrors the guard item navigation already uses: after setFocusedKey, call replaceSelection when selectOnFocus is set, nothing is selected yet, and manager.canSelectItem allows it. The !selectedKeys.size guard is load-bearing because the effect has no dependency array and re-runs until the collection is populated, so it must not clobber an existing selection or the restore-selected-key branch above it. As LFDanLu noted on the issue, no Spectrum component currently combines these props, so the List test harness gained an autoFocus prop to make the combination testable.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Run yarn jest packages/react-aria/test/selection/useSelectableCollection.test.js. The two new selects the autofocused item if selectOnFocus cases (autoFocus="first" and "last") fail without the useSelectableCollection.ts change and pass with it; the third new case checks that autofocusing a collection with a default selection does not change that selection. Also ran the react-aria selection, menu, and grid suites and the react-aria-components ListBox, Select, ComboBox, and Menu suites (172 tests) with no regressions, plus oxlint and tsc on the touched files.

🧢 Your Project:

Personal contribution.

@xevrion xevrion closed this Jul 30, 2026
@xevrion xevrion reopened this Jul 30, 2026
@xevrion
xevrion force-pushed the fix/select-autofocused-item-on-focus branch from b915e4b to 1a01bad Compare July 30, 2026 07:13
@xevrion
xevrion force-pushed the fix/select-autofocused-item-on-focus branch from 1a01bad to 614b839 Compare July 30, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Selectable collection doesn't select auto focused item when autoFocus is "first" and selectOnFocus is true

1 participant