fix: select the autofocused item when selectOnFocus is true - #10396
Open
xevrion wants to merge 1 commit into
Open
fix: select the autofocused item when selectOnFocus is true#10396xevrion wants to merge 1 commit into
xevrion wants to merge 1 commit into
Conversation
xevrion
force-pushed
the
fix/select-autofocused-item-on-focus
branch
from
July 30, 2026 07:13
b915e4b to
1a01bad
Compare
xevrion
force-pushed
the
fix/select-autofocused-item-on-focus
branch
from
July 30, 2026 07:17
1a01bad to
614b839
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4391
The autoFocus effect in
useSelectableCollectioncallssetFocused/setFocusedKeybut never touches the selection, which makes it the only focus path in the file that ignoresselectOnFocus; the focus-in handler, the Home and End handlers, and item navigation all callreplaceSelection. WithautoFocus="first"or"last"plusselectOnFocus, the item is focused but not selected.The fix mirrors the guard item navigation already uses: after
setFocusedKey, callreplaceSelectionwhenselectOnFocusis set, nothing is selected yet, andmanager.canSelectItemallows it. The!selectedKeys.sizeguard 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 theListtest harness gained anautoFocusprop to make the combination testable.✅ Pull Request Checklist:
📝 Test Instructions:
Run
yarn jest packages/react-aria/test/selection/useSelectableCollection.test.js. The two newselects the autofocused item if selectOnFocuscases (autoFocus="first" and "last") fail without theuseSelectableCollection.tschange 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.