Skip to content

Commit

Permalink
feat: GridList autoFocus (#7640)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
snowystinger and reidbarber authored Jan 27, 2025
1 parent 873f6fe commit f6e1bd9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/@react-aria/gridlist/src/useGridList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DisabledBehavior,
DOMAttributes,
DOMProps,
FocusStrategy,
Key,
KeyboardDelegate,
LayoutDelegate,
Expand All @@ -30,6 +31,8 @@ import {useHasTabbableChild} from '@react-aria/focus';
import {useSelectableList} from '@react-aria/selection';

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

let id = useId(props.id);
Expand Down
7 changes: 7 additions & 0 deletions packages/react-aria-components/test/GridList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ describe('GridList', () => {
expect(itemRef.current).toBeInstanceOf(HTMLElement);
});

it('should support autoFocus', () => {
let {getByRole} = renderGridList({autoFocus: true});
let gridList = getByRole('grid');

expect(document.activeElement).toBe(gridList);
});

it('should support hover', async () => {
let onHoverStart = jest.fn();
let onHoverChange = jest.fn();
Expand Down
6 changes: 6 additions & 0 deletions packages/react-aria-components/test/ListBox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ describe('ListBox', () => {
expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Hi']);
});

it('should support autoFocus', () => {
let {getByRole} = renderListbox({autoFocus: true});
let listbox = getByRole('listbox');
expect(document.activeElement).toBe(listbox);
});

it('should support hover', async () => {
let hoverStartSpy = jest.fn();
let hoverChangeSpy = jest.fn();
Expand Down

1 comment on commit f6e1bd9

@rspbot
Copy link

@rspbot rspbot commented on f6e1bd9 Jan 27, 2025

Choose a reason for hiding this comment

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

Please sign in to comment.