Skip to content
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
24 changes: 24 additions & 0 deletions packages/react-aria-components/test/Tree.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,30 @@ describe('Tree', () => {
expect(rows).toHaveLength(17);
});

it('should expand/collapse a row when using Space on the row when selectionMode is none', async () => {
if (type !== 'keyboard') {
return;
}

let {getAllByRole} = render(<DynamicTree />);
let rows = getAllByRole('row');
expect(rows).toHaveLength(20);

await user.tab();
expect(document.activeElement).toBe(rows[0]);
expect(rows[0]).toHaveAttribute('data-expanded', 'true');

await user.keyboard(' ');
expect(document.activeElement).toBe(rows[0]);
expect(rows[0]).not.toHaveAttribute('data-expanded');
expect(onExpandedChange).toHaveBeenCalledTimes(1);

await user.keyboard(' ');
expect(document.activeElement).toBe(rows[0]);
expect(rows[0]).toHaveAttribute('data-expanded', 'true');
expect(onExpandedChange).toHaveBeenCalledTimes(2);
});

it('should not expand when clicking/using Enter on the row if the row is selectable', async () => {
let {getAllByRole} = render(<DynamicTree treeProps={{selectionMode: 'multiple'}} />);
let rows = getAllByRole('row');
Expand Down
10 changes: 8 additions & 2 deletions packages/react-aria/src/selection/useSelectableItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
// oxlint-disable-next-line react/react-compiler
itemPressProps.onPress = e => {
if (hasPrimaryAction || (hasSecondaryAction && e.pointerType !== 'mouse')) {
if (e.pointerType === 'keyboard' && !isActionKey(e.key)) {
if (
e.pointerType === 'keyboard' &&
!isActionKey(e.key) &&
!(!allowsSelection && isSelectionKey(e.key))
) {
return;
}

Expand Down Expand Up @@ -353,7 +357,9 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
e.pointerType === 'touch' ||
e.pointerType === 'pen' ||
e.pointerType === 'virtual' ||
(e.pointerType === 'keyboard' && hasAction && isActionKey(e.key)) ||
(e.pointerType === 'keyboard' &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the comment about this:

Both primary and secondary actions occur on Enter key up

It seems like it might have been intentional we only did Enter, not Space. Will see if anyone recalls.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing this. I'll wait until someone who remembers the original intent can provide more context.

hasAction &&
(isActionKey(e.key) || (!allowsSelection && isSelectionKey(e.key)))) ||
(e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current)
) {
if (hasAction) {
Expand Down