Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix focus #679

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@

if (shouldFocusKey && elementToFocus) {
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
elementToFocus?.focus?.(options);
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
focusableElements.find(el => el !== containerRef.current)?.focus?.(options);

Check warning on line 408 in src/Menu.tsx

View check run for this annotation

Codecov / codecov/patch

src/Menu.tsx#L408

Added line #L408 was not covered by tests
},
};
});
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useAccessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@
*/
export function getFocusableElements(
container: HTMLElement,
elements: Set<HTMLElement>,
elements?: Set<HTMLElement>,
) {
const list = getFocusNodeList(container, true);
return list.filter(ele => elements.has(ele));
if (elements?.size > 0) {
return list.filter(ele => elements.has(ele));
}
return list;

Check warning on line 145 in src/hooks/useAccessibility.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useAccessibility.ts#L145

Added line #L145 was not covered by tests
}

function getNextFocusElement(
Expand Down
22 changes: 21 additions & 1 deletion tests/Focus.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { act, fireEvent, render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import Menu, { MenuItem, MenuItemGroup, MenuRef, SubMenu } from '../src';
import type { MenuRef } from '../src';
import Menu, { MenuItem, MenuItemGroup, SubMenu } from '../src';

describe('Focus', () => {
beforeAll(() => {
Expand Down Expand Up @@ -89,6 +90,25 @@ describe('Focus', () => {
expect(activeKey).toHaveClass('rc-menu-item-active');
});

it('should focus active item through ref without activeKey', async () => {
const menuRef = React.createRef<MenuRef>();
const { container } = await act(async () =>
render(
<Menu ref={menuRef}>
<MenuItem key="light">Light</MenuItem>
<MenuItem key="Cat">Cat</MenuItem>
</Menu>,
),
);

act(() => menuRef.current.focus());

// first li
const activeLi = container.querySelector('li');
expect(document.activeElement).toBe(activeLi);
expect(activeLi).toHaveClass('rc-menu-item-active');
});

it('focus moves to the next accessible menu item if the first child is empty group', async () => {
const menuRef = React.createRef<MenuRef>();
const { getByTestId } = await act(async () =>
Expand Down
Loading