Skip to content
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

fix(@react-aria/selection): don't mutate non-empty selection upon focus #7513

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/@react-aria/selection/src/useSelectableCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,11 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
}

manager.setFocused(true);

if (manager.focusedKey == null) {
let navigateToFirstKey = (key: Key | undefined | null) => {
let navigateToKey = (key: Key | undefined | null) => {
if (key != null) {
manager.setFocusedKey(key);
if (selectOnFocus) {
if (selectOnFocus && !manager.isSelected(key)) {
manager.replaceSelection(key);
}
}
Expand All @@ -357,9 +356,9 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
// and either focus the first or last item accordingly.
let relatedTarget = e.relatedTarget as Element;
if (relatedTarget && (e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING)) {
navigateToFirstKey(manager.lastSelectedKey ?? delegate.getLastKey?.());
navigateToKey(manager.lastSelectedKey ?? delegate.getLastKey?.());
} else {
navigateToFirstKey(manager.firstSelectedKey ?? delegate.getFirstKey?.());
navigateToKey(manager.firstSelectedKey ?? delegate.getFirstKey?.());
}
} else if (!isVirtualized && scrollRef.current) {
// Restore the scroll position to what it was before.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {fireEvent, installPointerEvent, pointerMap, render, simulateDesktop, simulateMobile} from '@react-spectrum/test-utils-internal';
import {fireEvent, installPointerEvent, pointerMap, render, simulateDesktop, simulateMobile, within} from '@react-spectrum/test-utils-internal';
import {Item} from '@react-stately/collections';
import {List} from '../stories/List';
import React from 'react';
Expand Down Expand Up @@ -107,5 +107,43 @@ describe('useSelectableCollection', () => {
expect(options[1]).not.toHaveAttribute('aria-selected');
expect(options[2]).toHaveAttribute('aria-selected', 'true');
});

it('focuses first/last selected item on focus enter and does not change the selection', async () => {
let onSelectionChange1 = jest.fn();
let onSelectionChange2 = jest.fn();
let {getByText, getAllByRole} = render(
<>
<List
selectionMode="multiple"
selectionBehavior="replace"
defaultSelectedKeys={['i2', 'i3']}
onSelectionChange={onSelectionChange1}>
<Item key="i1">Paco de Lucia</Item>
<Item key="i2">Vicente Amigo</Item>
<Item key="i3">Gerardo Nunez</Item>
</List>
<button>focus stop</button>
<List
selectionMode="multiple"
selectionBehavior="replace"
defaultSelectedKeys={['i2', 'i3']}
onSelectionChange={onSelectionChange2}>
<Item key="i1">Paco de Lucia</Item>
<Item key="i2">Vicente Amigo</Item>
<Item key="i3">Gerardo Nunez</Item>
</List>
</>
);
let [firstList, secondList] = getAllByRole('listbox');
await user.click(getByText('focus stop'));
await user.tab();
expect(document.activeElement).toBe(within(secondList).getByRole('option', {name: 'Vicente Amigo'}));
await user.click(getByText('focus stop'));
await user.tab({shift: true});
expect(document.activeElement).toBe(within(firstList).getByRole('option', {name: 'Gerardo Nunez'}));

expect(onSelectionChange1).not.toHaveBeenCalled();
expect(onSelectionChange2).not.toHaveBeenCalled();
});
});
});