Skip to content

Commit 8dfdef4

Browse files
authored
fix: update parent selection when menu items load dynamically (#895)
* fix: update parent selection when menu items load dynamically * test: assert child selection after menu items load dynamically
1 parent a963efe commit 8dfdef4

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎src/hooks/useKeyRecords.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const getPathKeys = (keyPathStr: string) => keyPathStr.split(PATH_SPLIT);
1111
export const OVERFLOW_KEY = 'rc-menu-more';
1212

1313
export default function useKeyRecords() {
14-
const [, internalForceUpdate] = React.useState({});
14+
const [keyPathVersion, internalForceUpdate] = React.useState({});
1515
const key2pathRef = useRef(new Map<string, string>());
1616
const path2keyRef = useRef(new Map<string, string>());
1717
const [overflowKeys, setOverflowKeys] = React.useState([]);
@@ -80,7 +80,9 @@ export default function useKeyRecords() {
8080
const pathKeyList = getKeyPath(pathKey, true);
8181
return pathKeyList.includes(eventKey);
8282
}),
83-
[getKeyPath],
83+
// Path records live in refs, so updates must also refresh context consumers.
84+
// eslint-disable-next-line react-hooks/exhaustive-deps
85+
[getKeyPath, keyPathVersion],
8486
);
8587
const getKeys = () => {
8688
const keys = [...key2pathRef.current.keys()];
@@ -107,12 +109,12 @@ export default function useKeyRecords() {
107109
return pathKeys;
108110
}, []);
109111

110-
React.useEffect(
111-
() => () => {
112+
React.useEffect(() => {
113+
destroyRef.current = false;
114+
return () => {
112115
destroyRef.current = true;
113-
},
114-
[],
115-
);
116+
};
117+
}, []);
116118

117119
return {
118120
// Register

‎tests/DynamicSelection.spec.tsx‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
import { act, render } from '@testing-library/react';
3+
import Menu from '../src';
4+
import type { MenuProps } from '../src';
5+
6+
describe.each([false, true])('dynamic selection with StrictMode=%s', strictMode => {
7+
const wrapper = strictMode ? React.StrictMode : React.Fragment;
8+
9+
describe.each(['defaultSelectedKeys', 'selectedKeys'] as const)('%s', selectionProp => {
10+
it.each<MenuProps['mode']>(['inline', 'vertical', 'horizontal'])(
11+
'highlights the parent and child when items load in %s mode',
12+
async mode => {
13+
const selectionProps = { [selectionProp]: ['child'] };
14+
const { container, getAllByText, rerender } = render(
15+
<Menu mode={mode} {...selectionProps} items={[]} forceSubMenuRender />,
16+
{ wrapper },
17+
);
18+
19+
await act(async () => {
20+
rerender(
21+
<Menu
22+
mode={mode}
23+
{...selectionProps}
24+
forceSubMenuRender
25+
items={[
26+
{
27+
key: 'parent',
28+
label: 'Parent',
29+
children: [
30+
{
31+
type: 'group',
32+
label: 'Group',
33+
children: [{ key: 'child', label: 'Child' }],
34+
},
35+
],
36+
},
37+
]}
38+
/>,
39+
);
40+
});
41+
42+
expect(container.querySelector('.rc-menu-submenu')).toHaveClass('rc-menu-submenu-selected');
43+
getAllByText('Child').forEach(child => {
44+
expect(child.closest('.rc-menu-item')).toHaveClass('rc-menu-item-selected');
45+
});
46+
},
47+
);
48+
});
49+
});

0 commit comments

Comments
 (0)