You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.2 KiB

import * as React from 'react';
import { Item } from 'rc-menu';
import { ClickParam } from '.';
import MenuContext, { MenuContextProps } from './MenuContext';
import Tooltip, { TooltipProps } from '../tooltip';
import { SiderContext, SiderContextProps } from '../layout/Sider';
export interface MenuItemProps
extends Omit<
React.HTMLAttributes<HTMLLIElement>,
'title' | 'onClick' | 'onMouseEnter' | 'onMouseLeave'
> {
rootPrefixCls?: string;
disabled?: boolean;
level?: number;
title?: React.ReactNode;
children?: React.ReactNode;
className?: string;
style?: React.CSSProperties;
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;
6 years ago
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
this.menuItem.onKeyDown(e);
};
saveMenuItem = (menuItem: this) => {
this.menuItem = menuItem;
};
renderItem = ({ siderCollapsed }: SiderContextProps) => {
const { level, children, rootPrefixCls } = this.props;
const { title, ...rest } = this.props;
return (
6 years ago
<MenuContext.Consumer>
{({ inlineCollapsed }: MenuContextProps) => {
const tooltipProps: TooltipProps = {};
6 years ago
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;
6 years ago
}
return (
<Tooltip
{...tooltipProps}
6 years ago
title={titleNode}
placement="right"
overlayClassName={`${rootPrefixCls}-inline-collapsed-tooltip`}
>
<Item {...rest} title={title} ref={this.saveMenuItem} />
</Tooltip>
);
}}
</MenuContext.Consumer>
);
};
render() {
return <SiderContext.Consumer>{this.renderItem}</SiderContext.Consumer>;
}
}