|
|
@ -2,6 +2,7 @@ import classNames from 'classnames'; |
|
|
|
import type { MenuItemProps as RcMenuItemProps } from 'rc-menu'; |
|
|
|
import { Item } from 'rc-menu'; |
|
|
|
import toArray from 'rc-util/lib/Children/toArray'; |
|
|
|
import omit from 'rc-util/lib/omit'; |
|
|
|
import * as React from 'react'; |
|
|
|
import type { SiderContextProps } from '../layout/Sider'; |
|
|
|
import { SiderContext } from '../layout/Sider'; |
|
|
@ -17,15 +18,16 @@ export interface MenuItemProps extends Omit<RcMenuItemProps, 'title'> { |
|
|
|
title?: React.ReactNode; |
|
|
|
} |
|
|
|
|
|
|
|
export default class MenuItem extends React.Component<MenuItemProps> { |
|
|
|
static contextType = MenuContext; |
|
|
|
|
|
|
|
context: MenuContextProps; |
|
|
|
|
|
|
|
renderItemChildren(inlineCollapsed: boolean) { |
|
|
|
const { prefixCls, firstLevel } = this.context; |
|
|
|
const { icon, children } = this.props; |
|
|
|
|
|
|
|
const MenuItem: React.FC<MenuItemProps> = (props) => { |
|
|
|
const { className, children, icon, title, danger } = props; |
|
|
|
const { |
|
|
|
prefixCls, |
|
|
|
firstLevel, |
|
|
|
direction, |
|
|
|
disableMenuItemTitleTooltip, |
|
|
|
inlineCollapsed: isInlineCollapsed, |
|
|
|
} = React.useContext<MenuContextProps>(MenuContext); |
|
|
|
const renderItemChildren = (inlineCollapsed: boolean) => { |
|
|
|
const wrapNode = <span className={`${prefixCls}-title-content`}>{children}</span>; |
|
|
|
// inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span
|
|
|
|
// ref: https://github.com/ant-design/ant-design/pull/23456
|
|
|
@ -35,25 +37,16 @@ export default class MenuItem extends React.Component<MenuItemProps> { |
|
|
|
} |
|
|
|
} |
|
|
|
return wrapNode; |
|
|
|
} |
|
|
|
|
|
|
|
renderItem = ({ siderCollapsed }: SiderContextProps) => { |
|
|
|
const { prefixCls, firstLevel, inlineCollapsed, direction, disableMenuItemTitleTooltip } = |
|
|
|
this.context; |
|
|
|
const { className, children } = this.props; |
|
|
|
const { title, icon, danger, ...rest } = this.props; |
|
|
|
|
|
|
|
}; |
|
|
|
const renderItem = ({ siderCollapsed }: SiderContextProps) => { |
|
|
|
let tooltipTitle = title; |
|
|
|
if (typeof title === 'undefined') { |
|
|
|
tooltipTitle = firstLevel ? children : ''; |
|
|
|
} else if (title === false) { |
|
|
|
tooltipTitle = ''; |
|
|
|
} |
|
|
|
const tooltipProps: TooltipProps = { |
|
|
|
title: tooltipTitle, |
|
|
|
}; |
|
|
|
|
|
|
|
if (!siderCollapsed && !inlineCollapsed) { |
|
|
|
const tooltipProps: TooltipProps = { title: tooltipTitle }; |
|
|
|
if (!siderCollapsed && !isInlineCollapsed) { |
|
|
|
tooltipProps.title = null; |
|
|
|
// Reset `open` to fix control mode tooltip display not correct
|
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/16742
|
|
|
@ -63,7 +56,7 @@ export default class MenuItem extends React.Component<MenuItemProps> { |
|
|
|
|
|
|
|
let returnNode = ( |
|
|
|
<Item |
|
|
|
{...rest} |
|
|
|
{...omit(props, ['title', 'icon', 'danger'])} |
|
|
|
className={classNames( |
|
|
|
{ |
|
|
|
[`${prefixCls}-item-danger`]: danger, |
|
|
@ -79,7 +72,7 @@ export default class MenuItem extends React.Component<MenuItemProps> { |
|
|
|
`${prefixCls}-item-icon`, |
|
|
|
), |
|
|
|
})} |
|
|
|
{this.renderItemChildren(inlineCollapsed)} |
|
|
|
{renderItemChildren(isInlineCollapsed)} |
|
|
|
</Item> |
|
|
|
); |
|
|
|
|
|
|
@ -97,8 +90,7 @@ export default class MenuItem extends React.Component<MenuItemProps> { |
|
|
|
|
|
|
|
return returnNode; |
|
|
|
}; |
|
|
|
return <SiderContext.Consumer>{renderItem}</SiderContext.Consumer>; |
|
|
|
}; |
|
|
|
|
|
|
|
render() { |
|
|
|
return <SiderContext.Consumer>{this.renderItem}</SiderContext.Consumer>; |
|
|
|
} |
|
|
|
} |
|
|
|
export default MenuItem; |
|
|
|