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-05-14 20:54:49 +08:00
|
|
|
import { isValidElement } 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> {
|
|
|
|
static isMenuItem = true;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-03-25 18:37:15 +08:00
|
|
|
private menuItem: this;
|
2018-12-13 22:03:12 +08:00
|
|
|
|
2017-11-21 17:38:51 +08:00
|
|
|
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
|
2017-11-16 16:08:30 +08:00
|
|
|
this.menuItem.onKeyDown(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-03-28 09:24:16 +08:00
|
|
|
|
2019-03-25 18:37:15 +08:00
|
|
|
saveMenuItem = (menuItem: this) => {
|
2017-11-16 16:08:30 +08:00
|
|
|
this.menuItem = menuItem;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-20 23:26:40 +08:00
|
|
|
|
2020-05-27 22:19:07 +08:00
|
|
|
renderItemChildren(inlineCollapsed: boolean) {
|
|
|
|
const { icon, children, level, rootPrefixCls } = this.props;
|
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')) {
|
2020-05-27 22:19:07 +08:00
|
|
|
if (children && inlineCollapsed && level === 1 && typeof children === 'string') {
|
|
|
|
return (
|
|
|
|
<div className={`${rootPrefixCls}-inline-collapsed-noicon`}>{children.charAt(0)}</div>
|
|
|
|
);
|
|
|
|
}
|
2020-04-26 23:16:15 +08:00
|
|
|
return children;
|
|
|
|
}
|
|
|
|
return <span>{children}</span>;
|
|
|
|
}
|
|
|
|
|
2019-04-08 21:03:25 +08:00
|
|
|
renderItem = ({ siderCollapsed }: SiderContextProps) => {
|
2020-03-06 11:53:09 +08:00
|
|
|
const { level, className, children, rootPrefixCls } = 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
|
|
|
|
2018-05-10 10:59:33 +08:00
|
|
|
return (
|
2019-04-08 22:20:18 +08:00
|
|
|
<MenuContext.Consumer>
|
2020-01-02 19:10:16 +08:00
|
|
|
{({ inlineCollapsed, direction }: MenuContextProps) => {
|
2020-03-13 19:43:10 +08:00
|
|
|
let tooltipTitle = title;
|
|
|
|
if (typeof title === 'undefined') {
|
|
|
|
tooltipTitle = level === 1 ? children : '';
|
|
|
|
} else if (title === false) {
|
|
|
|
tooltipTitle = '';
|
|
|
|
}
|
2019-08-27 22:30:52 +08:00
|
|
|
const tooltipProps: TooltipProps = {
|
2020-03-13 19:43:10 +08:00
|
|
|
title: tooltipTitle,
|
2019-08-27 22:30:52 +08:00
|
|
|
};
|
2019-05-27 15:22:26 +08:00
|
|
|
|
2019-04-08 22:20:18 +08:00
|
|
|
if (!siderCollapsed && !inlineCollapsed) {
|
2019-08-27 22:30:52 +08:00
|
|
|
tooltipProps.title = null;
|
2019-05-27 15:22:26 +08:00
|
|
|
// Reset `visible` to fix control mode tooltip display not correct
|
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/16742
|
|
|
|
tooltipProps.visible = false;
|
2019-04-08 22:20:18 +08:00
|
|
|
}
|
2020-04-26 23:16:15 +08:00
|
|
|
const childrenLength = toArray(children).length;
|
2019-04-08 22:20:18 +08:00
|
|
|
return (
|
|
|
|
<Tooltip
|
2019-05-27 15:22:26 +08:00
|
|
|
{...tooltipProps}
|
2020-01-02 19:10:16 +08:00
|
|
|
placement={direction === 'rtl' ? 'left' : 'right'}
|
2019-04-08 22:20:18 +08:00
|
|
|
overlayClassName={`${rootPrefixCls}-inline-collapsed-tooltip`}
|
|
|
|
>
|
2020-03-06 11:53:09 +08:00
|
|
|
<Item
|
|
|
|
{...rest}
|
|
|
|
className={classNames(className, {
|
2020-04-30 21:14:10 +08:00
|
|
|
[`${rootPrefixCls}-item-danger`]: danger,
|
2020-04-26 23:16:15 +08:00
|
|
|
[`${rootPrefixCls}-item-only-child`]:
|
|
|
|
(icon ? childrenLength + 1 : childrenLength) === 1,
|
2020-03-06 11:53:09 +08:00
|
|
|
})}
|
|
|
|
title={title}
|
|
|
|
ref={this.saveMenuItem}
|
2020-04-26 23:16:15 +08:00
|
|
|
>
|
|
|
|
{icon}
|
2020-05-27 22:19:07 +08:00
|
|
|
{this.renderItemChildren(inlineCollapsed)}
|
2020-04-26 23:16:15 +08:00
|
|
|
</Item>
|
2019-04-08 22:20:18 +08:00
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</MenuContext.Consumer>
|
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
|
|
|
}
|
|
|
|
}
|