mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-05 05:28:20 +08:00
8934bbfffa
* feat: Menu.Item support icon
* docs: update menu item usage
* docs: remove span wrap,when use icon
* fix: Collapsed depends on the span
* ✅ fix demo snapshot
* do not wrap children when it is span
* Menu.SubMenu support icon prop
* fix sub menu title
* fix eslint
* update dropdown demo
* fix extra icon attribute on li
Co-authored-by: MengZhaoFly <1424254461@qq.com>
104 lines
3.3 KiB
TypeScript
104 lines
3.3 KiB
TypeScript
import * as React from 'react';
|
|
import { Item } from 'rc-menu';
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
import classNames from 'classnames';
|
|
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;
|
|
icon?: React.ReactNode;
|
|
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;
|
|
|
|
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
|
|
this.menuItem.onKeyDown(e);
|
|
};
|
|
|
|
saveMenuItem = (menuItem: this) => {
|
|
this.menuItem = menuItem;
|
|
};
|
|
|
|
renderItemChildren() {
|
|
const { icon, children } = this.props;
|
|
// inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span
|
|
// ref: https://github.com/ant-design/ant-design/pull/23456
|
|
if (!icon || (React.isValidElement(children) && children.type === 'span')) {
|
|
return children;
|
|
}
|
|
return <span>{children}</span>;
|
|
}
|
|
|
|
renderItem = ({ siderCollapsed }: SiderContextProps) => {
|
|
const { level, className, children, rootPrefixCls } = this.props;
|
|
const { title, icon, ...rest } = this.props;
|
|
|
|
return (
|
|
<MenuContext.Consumer>
|
|
{({ inlineCollapsed, direction }: MenuContextProps) => {
|
|
let tooltipTitle = title;
|
|
if (typeof title === 'undefined') {
|
|
tooltipTitle = level === 1 ? children : '';
|
|
} else if (title === false) {
|
|
tooltipTitle = '';
|
|
}
|
|
const tooltipProps: TooltipProps = {
|
|
title: tooltipTitle,
|
|
};
|
|
|
|
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={`${rootPrefixCls}-inline-collapsed-tooltip`}
|
|
>
|
|
<Item
|
|
{...rest}
|
|
className={classNames(className, {
|
|
[`${rootPrefixCls}-item-only-child`]:
|
|
(icon ? childrenLength + 1 : childrenLength) === 1,
|
|
})}
|
|
title={title}
|
|
ref={this.saveMenuItem}
|
|
>
|
|
{icon}
|
|
{this.renderItemChildren()}
|
|
</Item>
|
|
</Tooltip>
|
|
);
|
|
}}
|
|
</MenuContext.Consumer>
|
|
);
|
|
};
|
|
|
|
render() {
|
|
return <SiderContext.Consumer>{this.renderItem}</SiderContext.Consumer>;
|
|
}
|
|
}
|