Skip to content

Commit 27bf693

Browse files
authored
refactor: solve menu circular dependency issue (#655)
1 parent 8f89f99 commit 27bf693

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

src/MenuItemGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as React from 'react';
44
import { MenuContext } from './context/MenuContext';
55
import { useFullPath, useMeasure } from './context/PathContext';
66
import type { MenuItemGroupType } from './interface';
7-
import { parseChildren } from './utils/nodeUtil';
7+
import { parseChildren } from './utils/commonUtil';
88

99
export interface MenuItemGroupProps
1010
extends Omit<MenuItemGroupType, 'type' | 'children' | 'label'> {

src/SubMenu/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import classNames from 'classnames';
33
import Overflow from 'rc-overflow';
44
import warning from 'rc-util/lib/warning';
55
import SubMenuList from './SubMenuList';
6-
import { parseChildren } from '../utils/nodeUtil';
6+
import { parseChildren } from '../utils/commonUtil';
77
import type { MenuInfo, SubMenuType } from '../interface';
88
import MenuContextProvider, { MenuContext } from '../context/MenuContext';
99
import useMemoCallback from '../hooks/useMemoCallback';

src/utils/commonUtil.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import toArray from "rc-util/lib/Children/toArray";
2+
import * as React from 'react';
3+
4+
export function parseChildren(
5+
children: React.ReactNode | undefined,
6+
keyPath: string[],
7+
) {
8+
return toArray(children).map((child, index) => {
9+
if (React.isValidElement(child)) {
10+
const { key } = child;
11+
let eventKey = (child.props as any)?.eventKey ?? key;
12+
13+
const emptyKey = eventKey === null || eventKey === undefined;
14+
15+
if (emptyKey) {
16+
eventKey = `tmp_key-${[...keyPath, index].join('-')}`;
17+
}
18+
19+
const cloneProps = {
20+
key: eventKey,
21+
eventKey,
22+
} as any;
23+
24+
if (process.env.NODE_ENV !== 'production' && emptyKey) {
25+
cloneProps.warnKey = true;
26+
}
27+
28+
return React.cloneElement(child, cloneProps);
29+
}
30+
31+
return child;
32+
});
33+
}

src/utils/nodeUtil.tsx

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
11
import * as React from 'react';
2-
import toArray from 'rc-util/lib/Children/toArray';
32
import type { ItemType } from '../interface';
4-
import { Divider, MenuItem, MenuItemGroup, SubMenu } from '..';
3+
import MenuItemGroup from '../MenuItemGroup';
4+
import SubMenu from '../SubMenu';
5+
import Divider from '../Divider';
6+
import MenuItem from '../MenuItem';
7+
import { parseChildren } from './commonUtil';
58

6-
export function parseChildren(
7-
children: React.ReactNode | undefined,
8-
keyPath: string[],
9-
) {
10-
return toArray(children).map((child, index) => {
11-
if (React.isValidElement(child)) {
12-
const { key } = child;
13-
let eventKey = (child.props as any)?.eventKey ?? key;
14-
15-
const emptyKey = eventKey === null || eventKey === undefined;
16-
17-
if (emptyKey) {
18-
eventKey = `tmp_key-${[...keyPath, index].join('-')}`;
19-
}
20-
21-
const cloneProps = {
22-
key: eventKey,
23-
eventKey,
24-
} as any;
25-
26-
if (process.env.NODE_ENV !== 'production' && emptyKey) {
27-
cloneProps.warnKey = true;
28-
}
29-
30-
return React.cloneElement(child, cloneProps);
31-
}
32-
33-
return child;
34-
});
35-
}
369

3710
function convertItemsToNodes(list: ItemType[]) {
3811
return (list || [])

0 commit comments

Comments
 (0)