Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/hooks/useAccessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default function useAccessibility({
}
};

const focusMenu = () => {
const focusMenu = (options?: FocusOptions) => {
if (overlayRef.current?.focus) {
overlayRef.current.focus();
overlayRef.current.focus(options);
focusMenuRef.current = true;
return true;
}
Expand Down Expand Up @@ -62,7 +62,7 @@ export default function useAccessibility({
window.addEventListener('keydown', handleKeyDown);
if (autoFocus) {
// FIXME: hack with raf
raf(focusMenu, 3);
raf(() => focusMenu({ preventScroll: true }), 3);
}
return () => {
window.removeEventListener('keydown', handleKeyDown);
Expand Down
75 changes: 40 additions & 35 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -586,42 +586,47 @@ describe('dropdown', () => {

it('should support autoFocus', async () => {
jest.useFakeTimers();
const focusSpy = jest.spyOn(HTMLElement.prototype, 'focus');

const overlay = (
<Menu>
<MenuItem key="1">
<span className="my-menuitem">one</span>
</MenuItem>
<MenuItem key="2">two</MenuItem>
</Menu>
);
const { container } = render(
<Dropdown autoFocus trigger={['click']} overlay={overlay}>
<button className="my-button">open</button>
</Dropdown>,
);
const trigger = container.querySelector('.my-button');

// Open menu
fireEvent.click(trigger);

await waitForTime();

expect(
container
.querySelector('.rc-dropdown')
.classList.contains('rc-dropdown-hidden'),
).toBeFalsy();
expect(document.activeElement.className).toContain('menu');

// Close menu with Tab
window.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 9 })); // Tab

await waitForTime();

expect(document.activeElement.className).toContain('my-button');

jest.useRealTimers();
try {
const overlay = (
<Menu>
<MenuItem key="1">
<span className="my-menuitem">one</span>
</MenuItem>
<MenuItem key="2">two</MenuItem>
</Menu>
);
const { container } = render(
<Dropdown autoFocus trigger={['click']} overlay={overlay}>
<button className="my-button">open</button>
</Dropdown>,
);
const trigger = container.querySelector('.my-button');

// Open menu
fireEvent.click(trigger);

await waitForTime();

expect(
container
.querySelector('.rc-dropdown')
.classList.contains('rc-dropdown-hidden'),
).toBeFalsy();
expect(document.activeElement.className).toContain('menu');
expect(focusSpy).toHaveBeenCalledWith({ preventScroll: true });

// Close menu with Tab
window.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 9 })); // Tab

await waitForTime();

expect(document.activeElement.className).toContain('my-button');
} finally {
focusSpy.mockRestore();
jest.useRealTimers();
}
});

it('children cannot be given ref should not throw', () => {
Expand Down
Loading