2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { MenuItemProps as RcMenuItemProps } from 'rc-menu';
|
|
|
|
import { Item } from 'rc-menu';
|
2020-03-06 11:53:09 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2022-11-21 09:52:33 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SiderContextProps } from '../layout/Sider';
|
|
|
|
import { SiderContext } from '../layout/Sider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TooltipProps } from '../tooltip';
|
|
|
|
import Tooltip from '../tooltip';
|
|
|
|
import { cloneElement, isValidElement } from '../_util/reactNode';
|
|
|
|
import type { MenuContextProps } from './MenuContext';
|
|
|
|
import MenuContext from './MenuContext';
|
2017-06-30 21:07:01 +08:00
|
|
|
|
2020-07-06 11:40:52 +08:00
|
|
|
export interface MenuItemProps extends Omit<RcMenuItemProps, 'title'> {
|
2020-04-26 23:16:15 +08:00
|
|
|
icon?: React.ReactNode;
|
2020-04-30 21:14:10 +08:00
|
|
|
danger?: boolean;
|
2018-11-20 23:26:40 +08:00
|
|
|
title?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:52:33 +08:00
|
|
|
const MenuItem: React.FC<MenuItemProps> = (props) => {
|
|
|
|
const { className, children, icon, title, danger } = props;
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
firstLevel,
|
|
|
|
direction,
|
|
|
|
disableMenuItemTitleTooltip,
|
|
|
|
inlineCollapsed: isInlineCollapsed,
|
|
|
|
} = React.useContext<MenuContextProps>(MenuContext);
|
|
|
|
const renderItemChildren = (inlineCollapsed: boolean) => {
|
2021-05-25 20:52:52 +08:00
|
|
|
const wrapNode = <span className={`${prefixCls}-title-content`}>{children}</span>;
|
2020-04-26 23:16:15 +08:00
|
|
|
// inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span
|
|
|
|
// ref: https://github.com/ant-design/ant-design/pull/23456
|
2020-05-14 20:54:49 +08:00
|
|
|
if (!icon || (isValidElement(children) && children.type === 'span')) {
|
2021-05-24 16:24:00 +08:00
|
|
|
if (children && inlineCollapsed && firstLevel && typeof children === 'string') {
|
|
|
|
return <div className={`${prefixCls}-inline-collapsed-noicon`}>{children.charAt(0)}</div>;
|
2020-05-27 22:19:07 +08:00
|
|
|
}
|
2020-04-26 23:16:15 +08:00
|
|
|
}
|
2021-05-25 20:52:52 +08:00
|
|
|
return wrapNode;
|
2022-11-21 09:52:33 +08:00
|
|
|
};
|
|
|
|
const renderItem = ({ siderCollapsed }: SiderContextProps) => {
|
2021-05-24 16:24:00 +08:00
|
|
|
let tooltipTitle = title;
|
|
|
|
if (typeof title === 'undefined') {
|
|
|
|
tooltipTitle = firstLevel ? children : '';
|
|
|
|
} else if (title === false) {
|
|
|
|
tooltipTitle = '';
|
|
|
|
}
|
2022-11-21 09:52:33 +08:00
|
|
|
const tooltipProps: TooltipProps = { title: tooltipTitle };
|
|
|
|
if (!siderCollapsed && !isInlineCollapsed) {
|
2021-05-24 16:24:00 +08:00
|
|
|
tooltipProps.title = null;
|
2022-08-26 17:17:13 +08:00
|
|
|
// Reset `open` to fix control mode tooltip display not correct
|
2021-05-24 16:24:00 +08:00
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/16742
|
2022-08-26 17:17:13 +08:00
|
|
|
tooltipProps.open = false;
|
2021-05-24 16:24:00 +08:00
|
|
|
}
|
|
|
|
const childrenLength = toArray(children).length;
|
2021-12-24 10:57:35 +08:00
|
|
|
|
|
|
|
let returnNode = (
|
|
|
|
<Item
|
2022-11-21 09:52:33 +08:00
|
|
|
{...omit(props, ['title', 'icon', 'danger'])}
|
2021-12-24 10:57:35 +08:00
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-item-danger`]: danger,
|
|
|
|
[`${prefixCls}-item-only-child`]: (icon ? childrenLength + 1 : childrenLength) === 1,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
title={typeof title === 'string' ? title : undefined}
|
2021-05-24 16:24:00 +08:00
|
|
|
>
|
2021-12-24 10:57:35 +08:00
|
|
|
{cloneElement(icon, {
|
|
|
|
className: classNames(
|
|
|
|
isValidElement(icon) ? icon.props?.className : '',
|
|
|
|
`${prefixCls}-item-icon`,
|
|
|
|
),
|
|
|
|
})}
|
2022-11-21 09:52:33 +08:00
|
|
|
{renderItemChildren(isInlineCollapsed)}
|
2021-12-24 10:57:35 +08:00
|
|
|
</Item>
|
2018-05-10 10:59:33 +08:00
|
|
|
);
|
2021-12-24 10:57:35 +08:00
|
|
|
|
|
|
|
if (!disableMenuItemTitleTooltip) {
|
|
|
|
returnNode = (
|
|
|
|
<Tooltip
|
|
|
|
{...tooltipProps}
|
|
|
|
placement={direction === 'rtl' ? 'left' : 'right'}
|
|
|
|
overlayClassName={`${prefixCls}-inline-collapsed-tooltip`}
|
|
|
|
>
|
|
|
|
{returnNode}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnNode;
|
2019-04-08 21:03:25 +08:00
|
|
|
};
|
2022-11-21 09:52:33 +08:00
|
|
|
return <SiderContext.Consumer>{renderItem}</SiderContext.Consumer>;
|
|
|
|
};
|
2019-04-08 21:03:25 +08:00
|
|
|
|
2022-11-21 09:52:33 +08:00
|
|
|
export default MenuItem;
|