Skip to content

Commit a2e519a

Browse files
committed
feat: expose shouldFocusOnHover prop for ListBox
1 parent 1dd65ff commit a2e519a

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

packages/@react-aria/listbox/src/useListBox.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ export interface AriaListBoxOptions<T> extends Omit<AriaListBoxProps<T>, 'childr
4848
*/
4949
shouldUseVirtualFocus?: boolean,
5050

51-
/** Whether options should be focused when the user hovers over them. */
52-
shouldFocusOnHover?: boolean,
53-
5451
/**
5552
* The behavior of links in the collection.
5653
* - 'action': link behaves like onAction.

packages/@react-types/listbox/src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface AriaListBoxProps<T> extends AriaListBoxPropsBase<T> {
4040
selectionBehavior?: SelectionBehavior,
4141
/** Whether selection should occur on press up instead of press down. */
4242
shouldSelectOnPressUp?: boolean,
43+
/** Whether options should be focused when the user hovers over them. */
44+
shouldFocusOnHover?: boolean;
4345
/**
4446
* Handler that is called when a user performs an action on an item. The exact user event depends on
4547
* the collection's `selectionBehavior` prop and the interaction modality.

packages/react-aria-components/test/ListBox.test.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,9 +1297,9 @@ describe('ListBox', () => {
12971297
let {getAllByRole} = renderListbox({selectionMode: 'single', onSelectionChange});
12981298
let items = getAllByRole('option');
12991299

1300-
await user.pointer({target: items[0], keys: '[MouseLeft>]'});
1300+
await user.pointer({target: items[0], keys: '[MouseLeft>]'});
13011301
expect(onSelectionChange).toBeCalledTimes(1);
1302-
1302+
13031303
await user.pointer({target: items[0], keys: '[/MouseLeft]'});
13041304
expect(onSelectionChange).toBeCalledTimes(1);
13051305
});
@@ -1309,9 +1309,9 @@ describe('ListBox', () => {
13091309
let {getAllByRole} = renderListbox({selectionMode: 'single', onSelectionChange, shouldSelectOnPressUp: false});
13101310
let items = getAllByRole('option');
13111311

1312-
await user.pointer({target: items[0], keys: '[MouseLeft>]'});
1312+
await user.pointer({target: items[0], keys: '[MouseLeft>]'});
13131313
expect(onSelectionChange).toBeCalledTimes(1);
1314-
1314+
13151315
await user.pointer({target: items[0], keys: '[/MouseLeft]'});
13161316
expect(onSelectionChange).toBeCalledTimes(1);
13171317
});
@@ -1321,11 +1321,40 @@ describe('ListBox', () => {
13211321
let {getAllByRole} = renderListbox({selectionMode: 'single', onSelectionChange, shouldSelectOnPressUp: true});
13221322
let items = getAllByRole('option');
13231323

1324-
await user.pointer({target: items[0], keys: '[MouseLeft>]'});
1324+
await user.pointer({target: items[0], keys: '[MouseLeft>]'});
13251325
expect(onSelectionChange).toBeCalledTimes(0);
1326-
1326+
13271327
await user.pointer({target: items[0], keys: '[/MouseLeft]'});
13281328
expect(onSelectionChange).toBeCalledTimes(1);
13291329
});
13301330
});
1331+
1332+
describe('shouldFocusOnHover', () => {
1333+
it('should focus options on hovering with shouldFocusOnHover', async () => {
1334+
let {getAllByRole} = renderListbox({shouldFocusOnHover: true});
1335+
let options = getAllByRole('option');
1336+
let option1 = options[0];
1337+
let option2 = options[1];
1338+
1339+
expect(option1).not.toHaveFocus();
1340+
expect(option2).not.toHaveFocus();
1341+
1342+
await user.hover(option1);
1343+
expect(option1).toHaveFocus();
1344+
1345+
keyPress('ArrowDown');
1346+
expect(option1).not.toHaveFocus();
1347+
expect(option2).toHaveFocus();
1348+
});
1349+
1350+
it.each([false, undefined])('should not focus options on hovering when shouldFocusOnHover is not true', async (shouldFocusOnHover) => {
1351+
let {getAllByRole} = renderListbox({shouldFocusOnHover});
1352+
let option = getAllByRole('option')[0];
1353+
1354+
expect(option).not.toHaveFocus();
1355+
1356+
await user.hover(option);
1357+
expect(option).not.toHaveFocus();
1358+
});
1359+
});
13311360
});

0 commit comments

Comments
 (0)