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 1 commit
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 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {

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);
},
};
});
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 @@ function getFocusElement(
*/
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;
}

function getNextFocusElement(
Expand Down
19 changes: 19 additions & 0 deletions tests/Focus.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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 activeKey = container.querySelector('li')
// expect(document.activeElement).toBe(activeKey);
// expect(activeKey).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