Skip to content

Commit f6e1bd9

Browse files
feat: GridList autoFocus (#7640)
* feat: GridList autoFocus * Update packages/@react-aria/gridlist/src/useGridList.ts Co-authored-by: Reid Barber <[email protected]> --------- Co-authored-by: Reid Barber <[email protected]>
1 parent 873f6fe commit f6e1bd9

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

packages/@react-aria/gridlist/src/useGridList.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
DisabledBehavior,
1717
DOMAttributes,
1818
DOMProps,
19+
FocusStrategy,
1920
Key,
2021
KeyboardDelegate,
2122
LayoutDelegate,
@@ -30,6 +31,8 @@ import {useHasTabbableChild} from '@react-aria/focus';
3031
import {useSelectableList} from '@react-aria/selection';
3132

3233
export interface GridListProps<T> extends CollectionBase<T>, MultipleSelection {
34+
/** Whether to auto focus the gridlist or an option. */
35+
autoFocus?: boolean | FocusStrategy,
3336
/**
3437
* Handler that is called when a user performs an action on an item. The exact user event depends on
3538
* the collection's `selectionBehavior` prop and the interaction modality.
@@ -113,7 +116,8 @@ export function useGridList<T>(props: AriaGridListOptions<T>, state: ListState<T
113116
isVirtualized,
114117
selectOnFocus: state.selectionManager.selectionBehavior === 'replace',
115118
shouldFocusWrap: props.shouldFocusWrap,
116-
linkBehavior
119+
linkBehavior,
120+
autoFocus: props.autoFocus
117121
});
118122

119123
let id = useId(props.id);

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

+7
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ describe('GridList', () => {
128128
expect(itemRef.current).toBeInstanceOf(HTMLElement);
129129
});
130130

131+
it('should support autoFocus', () => {
132+
let {getByRole} = renderGridList({autoFocus: true});
133+
let gridList = getByRole('grid');
134+
135+
expect(document.activeElement).toBe(gridList);
136+
});
137+
131138
it('should support hover', async () => {
132139
let onHoverStart = jest.fn();
133140
let onHoverChange = jest.fn();

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

+6
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ describe('ListBox', () => {
337337
expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Hi']);
338338
});
339339

340+
it('should support autoFocus', () => {
341+
let {getByRole} = renderListbox({autoFocus: true});
342+
let listbox = getByRole('listbox');
343+
expect(document.activeElement).toBe(listbox);
344+
});
345+
340346
it('should support hover', async () => {
341347
let hoverStartSpy = jest.fn();
342348
let hoverChangeSpy = jest.fn();

0 commit comments

Comments
 (0)