ant-design/components/menu/MenuItem.tsx

73 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2017-06-30 21:07:01 +08:00
import { Item } from 'rc-menu';
import { ClickParam } from '.';
import MenuContext, { MenuContextProps } from './MenuContext';
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
export interface MenuItemProps
extends Omit<
React.HTMLAttributes<HTMLLIElement>,
'title' | 'onClick' | 'onMouseEnter' | 'onMouseLeave'
> {
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;
onMouseEnter?: (e: { key: string; domEvent: MouseEvent }) => void;
onMouseLeave?: (e: { key: string; domEvent: MouseEvent }) => void;
}
export default class MenuItem extends React.Component<MenuItemProps> {
static isMenuItem = true;
private menuItem: this;
2018-12-13 22:03:12 +08:00
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
this.menuItem.onKeyDown(e);
2018-12-07 20:02:01 +08:00
};
saveMenuItem = (menuItem: this) => {
this.menuItem = menuItem;
2018-12-07 20:02:01 +08:00
};
2019-04-08 21:03:25 +08:00
renderItem = ({ siderCollapsed }: SiderContextProps) => {
const { level, children, rootPrefixCls } = this.props;
const { title, ...rest } = this.props;
2018-05-10 10:59:33 +08:00
return (
2019-04-08 22:20:18 +08:00
<MenuContext.Consumer>
{({ inlineCollapsed }: MenuContextProps) => {
const tooltipProps: TooltipProps = {};
2019-04-08 22:20:18 +08:00
let titleNode = title || (level === 1 ? children : '');
if (!siderCollapsed && !inlineCollapsed) {
titleNode = null;
// 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
{...tooltipProps}
2019-04-08 22:20:18 +08:00
title={titleNode}
placement="right"
overlayClassName={`${rootPrefixCls}-inline-collapsed-tooltip`}
>
<Item {...rest} title={title} ref={this.saveMenuItem} />
</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>;
}
}