From 614b839577d8566c0c99c9d43bf1399d7e09b08c Mon Sep 17 00:00:00 2001 From: Yash Bavadiya Date: Thu, 30 Jul 2026 08:58:06 +0530 Subject: [PATCH] fix: select the autofocused item when selectOnFocus is true --- .../src/selection/useSelectableCollection.ts | 9 +++++ .../react-aria/stories/selection/List.tsx | 2 ++ .../selection/useSelectableCollection.test.js | 33 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/packages/react-aria/src/selection/useSelectableCollection.ts b/packages/react-aria/src/selection/useSelectableCollection.ts index 6cb2e4c7772..3fa8374175c 100644 --- a/packages/react-aria/src/selection/useSelectableCollection.ts +++ b/packages/react-aria/src/selection/useSelectableCollection.ts @@ -625,6 +625,15 @@ export function useSelectableCollection( manager.setFocused(true); manager.setFocusedKey(focusedKey); + if ( + focusedKey != null && + selectOnFocus && + !selectedKeys.size && + manager.canSelectItem(focusedKey) + ) { + manager.replaceSelection(focusedKey); + } + // If no default focus key is selected, focus the collection itself. if (focusedKey == null && !shouldUseVirtualFocus && ref.current) { focusSafely(ref.current); diff --git a/packages/react-aria/stories/selection/List.tsx b/packages/react-aria/stories/selection/List.tsx index 3c4edb9b185..7c6d99b4301 100644 --- a/packages/react-aria/stories/selection/List.tsx +++ b/packages/react-aria/stories/selection/List.tsx @@ -1,6 +1,7 @@ import { AsyncLoadable, CollectionBase, + FocusStrategy, MultipleSelection, Node, SelectionBehavior @@ -56,4 +57,5 @@ export function List(props: ListProps): JSX.Element { export interface ListProps extends CollectionBase, AsyncLoadable, MultipleSelection { selectionBehavior?: SelectionBehavior; + autoFocus?: boolean | FocusStrategy; } diff --git a/packages/react-aria/test/selection/useSelectableCollection.test.js b/packages/react-aria/test/selection/useSelectableCollection.test.js index b09e54d5c90..89b6225a357 100644 --- a/packages/react-aria/test/selection/useSelectableCollection.test.js +++ b/packages/react-aria/test/selection/useSelectableCollection.test.js @@ -49,6 +49,39 @@ describe('useSelectableCollection', () => { expect(options[0]).toHaveAttribute('aria-selected', 'true'); }); + it.each` + autoFocus | index + ${'first'} | ${0} + ${'last'} | ${2} + `( + 'selects the autofocused item if selectOnFocus with autoFocus=$autoFocus', + ({autoFocus, index}) => { + let {getAllByRole} = render( + + Paco de Lucia + Vicente Amigo + Gerardo Nunez + + ); + let options = getAllByRole('option'); + expect(document.activeElement).toBe(options[index]); + expect(options[index]).toHaveAttribute('aria-selected', 'true'); + } + ); + + it('does not change the selection when autofocusing an already selected collection', () => { + let {getAllByRole} = render( + + Paco de Lucia + Vicente Amigo + Gerardo Nunez + + ); + let options = getAllByRole('option'); + expect(options[2]).toHaveAttribute('aria-selected', 'true'); + expect(options[0]).not.toHaveAttribute('aria-selected'); + }); + it('can navigate without replacing the selection in multiple selection selectOnFocus', async () => { let {getAllByRole} = render(