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-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;
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:24:16 +08:00
|
|
|
export default class MenuItem extends React.Component<MenuItemProps> {
|
2021-05-24 16:24:00 +08:00
|
|
|
static contextType = MenuContext;
|
|
|
|
|
|
|
|
context: MenuContextProps;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-27 22:19:07 +08:00
|
|
|
renderItemChildren(inlineCollapsed: boolean) {
|
2021-05-24 16:24:00 +08:00
|
|
|
const { prefixCls, firstLevel } = this.context;
|
|
|
|
const { icon, children } = this.props;
|
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;
|
2020-04-26 23:16:15 +08:00
|
|
|
}
|
|
|
|
|
2019-04-08 21:03:25 +08:00
|
|
|
renderItem = ({ siderCollapsed }: SiderContextProps) => {
|
2021-12-24 10:57:35 +08:00
|
|
|
const { prefixCls, firstLevel, inlineCollapsed, direction, disableMenuItemTitleTooltip } =
|
|
|
|
this.context;
|
2021-05-24 16:24:00 +08:00
|
|
|
const { className, children } = this.props;
|
2020-04-30 21:14:10 +08:00
|
|
|
const { title, icon, danger, ...rest } = this.props;
|
2019-04-08 21:03:25 +08:00
|
|
|
|
2021-05-24 16:24:00 +08:00
|
|
|
let tooltipTitle = title;
|
|
|
|
if (typeof title === 'undefined') {
|
|
|
|
tooltipTitle = firstLevel ? children : '';
|
|
|
|
} else if (title === false) {
|
|
|
|
tooltipTitle = '';
|
|
|
|
}
|
|
|
|
const tooltipProps: TooltipProps = {
|
|
|
|
title: tooltipTitle,
|
|
|
|
};
|
2019-05-27 15:22:26 +08:00
|
|
|
|
2021-05-24 16:24:00 +08:00
|
|
|
if (!siderCollapsed && !inlineCollapsed) {
|
|
|
|
tooltipProps.title = null;
|
|
|
|
// Reset `visible` to fix control mode tooltip display not correct
|
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/16742
|
|
|
|
tooltipProps.visible = false;
|
|
|
|
}
|
|
|
|
const childrenLength = toArray(children).length;
|
2021-12-24 10:57:35 +08:00
|
|
|
|
|
|
|
let returnNode = (
|
|
|
|
<Item
|
|
|
|
{...rest}
|
|
|
|
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`,
|
|
|
|
),
|
|
|
|
})}
|
|
|
|
{this.renderItemChildren(inlineCollapsed)}
|
|
|
|
</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
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <SiderContext.Consumer>{this.renderItem}</SiderContext.Consumer>;
|
2017-10-27 20:21:34 +08:00
|
|
|
}
|
|
|
|
}
|