2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-07-06 11:40:52 +08:00
|
|
|
import { Item, MenuItemProps as RcMenuItemProps } from 'rc-menu';
|
2020-03-06 11:53:09 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
import classNames from 'classnames';
|
2019-07-22 21:15:48 +08:00
|
|
|
import MenuContext, { MenuContextProps } from './MenuContext';
|
2019-05-27 15:22:26 +08:00
|
|
|
import Tooltip, { TooltipProps } from '../tooltip';
|
2019-04-08 21:03:25 +08:00
|
|
|
import { SiderContext, SiderContextProps } from '../layout/Sider';
|
2020-11-25 14:31:51 +08:00
|
|
|
import { isValidElement, cloneElement } from '../_util/reactNode';
|
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-05-24 16:24:00 +08:00
|
|
|
const { prefixCls, firstLevel, inlineCollapsed, direction } = this.context;
|
|
|
|
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;
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
{...tooltipProps}
|
|
|
|
placement={direction === 'rtl' ? 'left' : 'right'}
|
|
|
|
overlayClassName={`${prefixCls}-inline-collapsed-tooltip`}
|
|
|
|
>
|
|
|
|
<Item
|
|
|
|
{...rest}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-item-danger`]: danger,
|
|
|
|
[`${prefixCls}-item-only-child`]: (icon ? childrenLength + 1 : childrenLength) === 1,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
title={typeof title === 'string' ? title : undefined}
|
|
|
|
>
|
|
|
|
{cloneElement(icon, {
|
|
|
|
className: classNames(
|
|
|
|
isValidElement(icon) ? icon.props?.className : '',
|
|
|
|
`${prefixCls}-item-icon`,
|
|
|
|
),
|
|
|
|
})}
|
|
|
|
{this.renderItemChildren(inlineCollapsed)}
|
|
|
|
</Item>
|
|
|
|
</Tooltip>
|
2018-05-10 10:59:33 +08:00
|
|
|
);
|
2019-04-08 21:03:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <SiderContext.Consumer>{this.renderItem}</SiderContext.Consumer>;
|
2017-10-27 20:21:34 +08:00
|
|
|
}
|
|
|
|
}
|