Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion src/MenuItemGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import { MenuContext } from './context/MenuContext';
import { useFullPath, useMeasure } from './context/PathContext';
import type { MenuItemGroupType } from './interface';
import { parseChildren } from './utils/nodeUtil';
import { parseChildren } from './utils/commonUtil';

export interface MenuItemGroupProps
extends Omit<MenuItemGroupType, 'type' | 'children' | 'label'> {
Expand Down
2 changes: 1 addition & 1 deletion src/SubMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames';
import Overflow from 'rc-overflow';
import warning from 'rc-util/lib/warning';
import SubMenuList from './SubMenuList';
import { parseChildren } from '../utils/nodeUtil';
import { parseChildren } from '../utils/commonUtil';
import type { MenuInfo, SubMenuType } from '../interface';
import MenuContextProvider, { MenuContext } from '../context/MenuContext';
import useMemoCallback from '../hooks/useMemoCallback';
Expand Down
33 changes: 33 additions & 0 deletions src/utils/commonUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import toArray from "rc-util/lib/Children/toArray";
import * as React from 'react';

export function parseChildren(
children: React.ReactNode | undefined,
keyPath: string[],
) {
return toArray(children).map((child, index) => {
if (React.isValidElement(child)) {
const { key } = child;
let eventKey = (child.props as any)?.eventKey ?? key;

const emptyKey = eventKey === null || eventKey === undefined;

if (emptyKey) {
eventKey = `tmp_key-${[...keyPath, index].join('-')}`;
}

const cloneProps = {
key: eventKey,
eventKey,
} as any;

if (process.env.NODE_ENV !== 'production' && emptyKey) {
cloneProps.warnKey = true;
}

return React.cloneElement(child, cloneProps);
}

return child;
});
}
37 changes: 5 additions & 32 deletions src/utils/nodeUtil.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
import * as React from 'react';
import toArray from 'rc-util/lib/Children/toArray';
import type { ItemType } from '../interface';
import { Divider, MenuItem, MenuItemGroup, SubMenu } from '..';
import MenuItemGroup from '../MenuItemGroup';
import SubMenu from '../SubMenu';
import Divider from '../Divider';
import MenuItem from '../MenuItem';
import { parseChildren } from './commonUtil';

export function parseChildren(
children: React.ReactNode | undefined,
keyPath: string[],
) {
return toArray(children).map((child, index) => {
if (React.isValidElement(child)) {
const { key } = child;
let eventKey = (child.props as any)?.eventKey ?? key;

const emptyKey = eventKey === null || eventKey === undefined;

if (emptyKey) {
eventKey = `tmp_key-${[...keyPath, index].join('-')}`;
}

const cloneProps = {
key: eventKey,
eventKey,
} as any;

if (process.env.NODE_ENV !== 'production' && emptyKey) {
cloneProps.warnKey = true;
}

return React.cloneElement(child, cloneProps);
}

return child;
});
}

function convertItemsToNodes(list: ItemType[]) {
return (list || [])
Expand Down