2024-02-24 14:50:03 +08:00
|
|
|
import * as React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { MenuItemProps as RcMenuItemProps } from 'rc-menu';
|
|
|
|
import { Item } from 'rc-menu';
|
2020-03-06 11:53:09 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2022-11-21 09:52:33 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2024-02-24 14:50:03 +08:00
|
|
|
|
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SiderContextProps } from '../layout/Sider';
|
|
|
|
import { SiderContext } from '../layout/Sider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TooltipProps } from '../tooltip';
|
|
|
|
import Tooltip from '../tooltip';
|
|
|
|
import type { MenuContextProps } from './MenuContext';
|
|
|
|
import MenuContext from './MenuContext';
|
2017-06-30 21:07:01 +08:00
|
|
|
|
2020-07-06 11:40:52 +08:00
|
|
|
export interface MenuItemProps extends Omit<RcMenuItemProps, 'title'> {
|
2020-04-26 23:16:15 +08:00
|
|
|
icon?: React.ReactNode;
|
2020-04-30 21:14:10 +08:00
|
|
|
danger?: boolean;
|
2018-11-20 23:26:40 +08:00
|
|
|
title?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2023-05-10 10:20:17 +08:00
|
|
|
type MenuItemComponent = React.FC<MenuItemProps>;
|
|
|
|
|
|
|
|
type RestArgs<T> = T extends (arg: any, ...args: infer P) => any ? P : never;
|
|
|
|
|
|
|
|
type GenericProps<T = unknown> = T extends infer U extends MenuItemProps
|
|
|
|
? unknown extends U
|
|
|
|
? MenuItemProps
|
|
|
|
: U
|
|
|
|
: MenuItemProps;
|
|
|
|
|
2024-06-22 21:59:12 +08:00
|
|
|
type GenericComponent = Omit<MenuItemComponent, ''> &
|
|
|
|
(<T extends MenuItemProps>(
|
2023-05-10 10:20:17 +08:00
|
|
|
props: GenericProps<T>,
|
|
|
|
...args: RestArgs<MenuItemComponent>
|
2024-06-22 21:59:12 +08:00
|
|
|
) => ReturnType<MenuItemComponent>);
|
2023-05-10 10:20:17 +08:00
|
|
|
|
|
|
|
const MenuItem: GenericComponent = (props) => {
|
2022-11-21 09:52:33 +08:00
|
|
|
const { className, children, icon, title, danger } = props;
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
firstLevel,
|
|
|
|
direction,
|
|
|
|
disableMenuItemTitleTooltip,
|
|
|
|
inlineCollapsed: isInlineCollapsed,
|
|
|
|
} = React.useContext<MenuContextProps>(MenuContext);
|
|
|
|
const renderItemChildren = (inlineCollapsed: boolean) => {
|
2021-05-25 20:52:52 +08:00
|
|
|
const wrapNode = <span className={`${prefixCls}-title-content`}>{children}</span>;
|
2020-04-26 23:16:15 +08:00
|
|
|
// inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span
|
|
|
|
// ref: https://github.com/ant-design/ant-design/pull/23456
|
2024-02-24 14:50:03 +08:00
|
|
|
if (!icon || (React.isValidElement(children) && children.type === 'span')) {
|
2021-05-24 16:24:00 +08:00
|
|
|
if (children && inlineCollapsed && firstLevel && typeof children === 'string') {
|
|
|
|
return <div className={`${prefixCls}-inline-collapsed-noicon`}>{children.charAt(0)}</div>;
|
2020-05-27 22:19:07 +08:00
|
|
|
}
|
2020-04-26 23:16:15 +08:00
|
|
|
}
|
2021-05-25 20:52:52 +08:00
|
|
|
return wrapNode;
|
2022-11-21 09:52:33 +08:00
|
|
|
};
|
2021-12-24 10:57:35 +08:00
|
|
|
|
2023-01-01 21:17:05 +08:00
|
|
|
const { siderCollapsed } = React.useContext<SiderContextProps>(SiderContext);
|
2021-12-24 10:57:35 +08:00
|
|
|
|
2023-01-01 21:17:05 +08:00
|
|
|
let tooltipTitle = title;
|
2021-12-24 10:57:35 +08:00
|
|
|
|
2023-01-01 21:17:05 +08:00
|
|
|
if (typeof title === 'undefined') {
|
|
|
|
tooltipTitle = firstLevel ? children : '';
|
|
|
|
} else if (title === false) {
|
|
|
|
tooltipTitle = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
tooltipProps.open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const childrenLength = toArray(children).length;
|
|
|
|
|
|
|
|
let returnNode = (
|
|
|
|
<Item
|
|
|
|
{...omit(props, ['title', 'icon', 'danger'])}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-item-danger`]: danger,
|
|
|
|
[`${prefixCls}-item-only-child`]: (icon ? childrenLength + 1 : childrenLength) === 1,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
title={typeof title === 'string' ? title : undefined}
|
|
|
|
>
|
|
|
|
{cloneElement(icon, {
|
|
|
|
className: classNames(
|
2024-02-24 14:50:03 +08:00
|
|
|
React.isValidElement(icon) ? icon.props?.className : '',
|
2023-01-01 21:17:05 +08:00
|
|
|
`${prefixCls}-item-icon`,
|
|
|
|
),
|
|
|
|
})}
|
|
|
|
{renderItemChildren(isInlineCollapsed)}
|
|
|
|
</Item>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!disableMenuItemTitleTooltip) {
|
|
|
|
returnNode = (
|
|
|
|
<Tooltip
|
|
|
|
{...tooltipProps}
|
|
|
|
placement={direction === 'rtl' ? 'left' : 'right'}
|
|
|
|
overlayClassName={`${prefixCls}-inline-collapsed-tooltip`}
|
|
|
|
>
|
|
|
|
{returnNode}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return returnNode;
|
2022-11-21 09:52:33 +08:00
|
|
|
};
|
2019-04-08 21:03:25 +08:00
|
|
|
|
2022-11-21 09:52:33 +08:00
|
|
|
export default MenuItem;
|