2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-06-30 21:07:01 +08:00
|
|
|
import { Item } from 'rc-menu';
|
2020-03-06 11:53:09 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
import classNames from 'classnames';
|
2019-03-28 09:24:16 +08:00
|
|
|
import { ClickParam } from '.';
|
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';
|
2017-06-30 21:07:01 +08:00
|
|
|
|
2019-07-15 15:43:11 +08:00
|
|
|
export interface MenuItemProps
|
|
|
|
extends Omit<
|
|
|
|
React.HTMLAttributes<HTMLLIElement>,
|
|
|
|
'title' | 'onClick' | 'onMouseEnter' | 'onMouseLeave'
|
|
|
|
> {
|
2018-11-20 23:26:40 +08:00
|
|
|
rootPrefixCls?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
level?: number;
|
|
|
|
title?: React.ReactNode;
|
|
|
|
children?: React.ReactNode;
|
2018-12-02 23:55:25 +08:00
|
|
|
className?: string;
|
2019-01-23 23:49:23 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-12-07 10:54:21 +08:00
|
|
|
onClick?: (param: ClickParam) => void;
|
2019-02-06 05:29:58 +08:00
|
|
|
onMouseEnter?: (e: { key: string; domEvent: MouseEvent }) => void;
|
|
|
|
onMouseLeave?: (e: { key: string; domEvent: MouseEvent }) => void;
|
2018-11-20 23:26:40 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
2019-04-08 21:03:25 +08:00
|
|
|
const { title, ...rest } = this.props;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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, {
|
|
|
|
[`${rootPrefixCls}-item-only-child`]: toArray(children).length === 1,
|
|
|
|
})}
|
|
|
|
title={title}
|
|
|
|
ref={this.saveMenuItem}
|
|
|
|
/>
|
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
|
|
|
}
|
|
|
|
}
|