Skip to content

Commit 8f489a2

Browse files
authored
feat: expose shouldFocusOnHover prop for ListBox (#8171)
1 parent 544f751 commit 8f489a2

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,35 @@ describe('ListBox', () => {
13321332
});
13331333
});
13341334

1335+
describe('shouldFocusOnHover', () => {
1336+
it('should focus options on hovering with shouldFocusOnHover', async () => {
1337+
let {getAllByRole} = renderListbox({shouldFocusOnHover: true});
1338+
let options = getAllByRole('option');
1339+
let option1 = options[0];
1340+
let option2 = options[1];
1341+
1342+
expect(option1).not.toHaveFocus();
1343+
expect(option2).not.toHaveFocus();
1344+
1345+
await user.hover(option1);
1346+
expect(option1).toHaveFocus();
1347+
1348+
keyPress('ArrowDown');
1349+
expect(option1).not.toHaveFocus();
1350+
expect(option2).toHaveFocus();
1351+
});
1352+
1353+
it.each([false, undefined])('should not focus options on hovering when shouldFocusOnHover is not true', async (shouldFocusOnHover) => {
1354+
let {getAllByRole} = renderListbox({shouldFocusOnHover});
1355+
let option = getAllByRole('option')[0];
1356+
1357+
expect(option).not.toHaveFocus();
1358+
1359+
await user.hover(option);
1360+
expect(option).not.toHaveFocus();
1361+
});
1362+
});
1363+
13351364
describe('async loading', () => {
13361365
let items = [
13371366
{name: 'Foo'},

0 commit comments

Comments
 (0)