forked from react-component/menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuItemGroup.tsx
More file actions
79 lines (67 loc) · 2.07 KB
/
MenuItemGroup.tsx
File metadata and controls
79 lines (67 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import classNames from 'classnames';
import omit from '@rc-component/util/lib/omit';
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/commonUtil';
export interface MenuItemGroupProps
extends Omit<MenuItemGroupType, 'type' | 'children' | 'label'> {
title?: React.ReactNode;
children?: React.ReactNode;
/** @private Internal filled key. Do not set it directly */
eventKey?: string;
/** @private Do not use. Private warning empty usage */
warnKey?: boolean;
}
const InternalMenuItemGroup = React.forwardRef<
HTMLLIElement,
MenuItemGroupProps
>((props, ref) => {
const { className, title, eventKey, children, ...restProps } = props;
const { prefixCls } = React.useContext(MenuContext);
const groupPrefixCls = `${prefixCls}-item-group`;
return (
<li
ref={ref}
role="presentation"
{...restProps}
onClick={e => e.stopPropagation()}
className={classNames(groupPrefixCls, className)}
>
<div
role="presentation"
className={`${groupPrefixCls}-title`}
title={typeof title === 'string' ? title : undefined}
>
{title}
</div>
<ul role="group" className={`${groupPrefixCls}-list`}>
{children}
</ul>
</li>
);
});
const MenuItemGroup = React.forwardRef<HTMLLIElement, MenuItemGroupProps>(
(props, ref) => {
const { eventKey, children } = props;
const connectedKeyPath = useFullPath(eventKey);
const childList: React.ReactElement[] = parseChildren(
children,
connectedKeyPath,
);
const measure = useMeasure();
if (measure) {
return childList as any as React.ReactElement;
}
return (
<InternalMenuItemGroup ref={ref} {...omit(props, ['warnKey'])}>
{childList}
</InternalMenuItemGroup>
);
},
);
if (process.env.NODE_ENV !== 'production') {
MenuItemGroup.displayName = 'MenuItemGroup';
}
export default MenuItemGroup;