Skip to content

Commit c52225a

Browse files
authored
fix: path register & add use hooks (#395)
* fix: path register & add use hooks * test: Update snasphot
1 parent f6068f2 commit c52225a

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

src/Menu.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface MenuProps
5353
children?: React.ReactNode;
5454

5555
disabled?: boolean;
56-
/** Disable auto overflow */
56+
/** @private Disable auto overflow. Pls note the prop name may refactor since we do not final decided. */
5757
disabledOverflow?: boolean;
5858

5959
/** direction of menu */
@@ -225,9 +225,8 @@ const Menu: React.FC<MenuProps> = props => {
225225
});
226226

227227
// >>>>> Cache & Reset open keys when inlineCollapsed changed
228-
const [inlineCacheOpenKeys, setInlineCacheOpenKeys] = React.useState(
229-
mergedOpenKeys,
230-
);
228+
const [inlineCacheOpenKeys, setInlineCacheOpenKeys] =
229+
React.useState(mergedOpenKeys);
231230

232231
const isInlineMode = mergedMode === 'inline';
233232

@@ -275,9 +274,10 @@ const Menu: React.FC<MenuProps> = props => {
275274
[registerPath, unregisterPath],
276275
);
277276

278-
const pathUserContext = React.useMemo(() => ({ isSubPathKey }), [
279-
isSubPathKey,
280-
]);
277+
const pathUserContext = React.useMemo(
278+
() => ({ isSubPathKey }),
279+
[isSubPathKey],
280+
);
281281

282282
React.useEffect(() => {
283283
refreshOverflowKeys(

src/hooks/useKeyRecords.ts

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

1313
export default function useKeyRecords() {
14-
const [, forceUpdate] = React.useState({});
14+
const [, 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([]);
1818
const updateRef = useRef(0);
19+
const destroyRef = useRef(false);
20+
21+
const forceUpdate = () => {
22+
if (!destroyRef.current) {
23+
internalForceUpdate({});
24+
}
25+
};
1926

2027
const registerPath = useCallback((key: string, keyPath: string[]) => {
2128
// Warning for invalidate or duplicated `key`
@@ -36,7 +43,7 @@ export default function useKeyRecords() {
3643

3744
nextSlice(() => {
3845
if (id === updateRef.current) {
39-
forceUpdate({});
46+
forceUpdate();
4047
}
4148
});
4249
}, []);
@@ -100,6 +107,13 @@ export default function useKeyRecords() {
100107
return pathKeys;
101108
}, []);
102109

110+
React.useEffect(
111+
() => () => {
112+
destroyRef.current = true;
113+
},
114+
[],
115+
);
116+
103117
return {
104118
// Register
105119
registerPath,

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import Menu, { MenuProps } from './Menu';
22
import MenuItem, { MenuItemProps } from './MenuItem';
33
import SubMenu, { SubMenuProps } from './SubMenu';
44
import MenuItemGroup, { MenuItemGroupProps } from './MenuItemGroup';
5+
import { useFullPath as useOriginFullPath } from './context/PathContext';
56
import Divider from './Divider';
67

8+
/** @private Only used for antd internal. Do not use in your production. */
9+
const useFullPath = useOriginFullPath;
10+
711
export {
812
SubMenu,
913
MenuItem as Item,
@@ -15,6 +19,7 @@ export {
1519
SubMenuProps,
1620
MenuItemProps,
1721
MenuItemGroupProps,
22+
useFullPath,
1823
};
1924

2025
type MenuType = typeof Menu & {

tests/Menu.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,5 +521,23 @@ describe('Menu', () => {
521521

522522
expect(wrapper.exists('.rc-menu-submenu-active')).toBeTruthy();
523523
});
524+
525+
it('not warning on destroy', async () => {
526+
const errorSpy = jest.spyOn(console, 'error');
527+
528+
const wrapper = mount(
529+
<Menu>
530+
<MenuItem key="bamboo">Bamboo</MenuItem>
531+
</Menu>,
532+
);
533+
534+
wrapper.unmount();
535+
536+
await Promise.resolve();
537+
538+
expect(errorSpy).not.toHaveBeenCalled();
539+
540+
errorSpy.mockRestore();
541+
});
524542
});
525543
/* eslint-enable */

tests/__snapshots__/Responsive.spec.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Array [
4545
aria-hidden="true"
4646
class="rc-menu-overflow-item rc-menu-overflow-item-rest rc-menu-submenu rc-menu-submenu-horizontal"
4747
role="none"
48-
style="opacity:0;height:0;overflow-y:hidden;order:9007199254740991;pointer-events:none"
48+
style="opacity:0;height:0;overflow-y:hidden;order:9007199254740991;pointer-events:none;position:absolute"
4949
>
5050
<div
5151
aria-expanded="false"

0 commit comments

Comments
 (0)