ant-design/components/menu/MenuItem.tsx
lijianan ab0e07e25d
Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
refactor: [v6] use rc-component (#52337)
* refactor: use @rc-component

* chore: adjust compile

* test: fix logic

* chore: back of reset

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
2025-01-10 14:14:31 +08:00

129 lines
3.9 KiB
TypeScript

import * as React from 'react';
import toArray from '@rc-component/util/lib/Children/toArray';
import omit from '@rc-component/util/lib/omit';
import classNames from 'classnames';
import type { MenuItemProps as RcMenuItemProps } from 'rc-menu';
import { Item } from 'rc-menu';
import { cloneElement } from '../_util/reactNode';
import type { SiderContextProps } from '../layout/Sider';
import { SiderContext } from '../layout/Sider';
import type { TooltipProps } from '../tooltip';
import Tooltip from '../tooltip';
import type { MenuContextProps } from './MenuContext';
import MenuContext from './MenuContext';
export interface MenuItemProps extends Omit<RcMenuItemProps, 'title'> {
icon?: React.ReactNode;
danger?: boolean;
title?: React.ReactNode;
}
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;
type GenericComponent = Omit<MenuItemComponent, ''> &
(<T extends MenuItemProps>(
props: GenericProps<T>,
...args: RestArgs<MenuItemComponent>
) => ReturnType<MenuItemComponent>);
const MenuItem: GenericComponent = (props) => {
const { className, children, icon, title, danger, extra } = props;
const {
prefixCls,
firstLevel,
direction,
disableMenuItemTitleTooltip,
inlineCollapsed: isInlineCollapsed,
} = React.useContext<MenuContextProps>(MenuContext);
const renderItemChildren = (inlineCollapsed: boolean) => {
const label = (children as React.ReactNode[])?.[0];
const wrapNode = (
<span
className={classNames(`${prefixCls}-title-content`, {
[`${prefixCls}-title-content-with-extra`]: !!extra || extra === 0,
})}
>
{children}
</span>
);
// 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')) {
if (children && inlineCollapsed && firstLevel && typeof label === 'string') {
return <div className={`${prefixCls}-inline-collapsed-noicon`}>{label.charAt(0)}</div>;
}
}
return wrapNode;
};
const { siderCollapsed } = React.useContext<SiderContextProps>(SiderContext);
let tooltipTitle = title;
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(
React.isValidElement(icon)
? (icon as React.ReactElement<{ className?: string }>).props?.className
: '',
`${prefixCls}-item-icon`,
),
})}
{renderItemChildren(isInlineCollapsed)}
</Item>
);
if (!disableMenuItemTitleTooltip) {
returnNode = (
<Tooltip
{...tooltipProps}
placement={direction === 'rtl' ? 'left' : 'right'}
classNames={{ root: `${prefixCls}-inline-collapsed-tooltip` }}
>
{returnNode}
</Tooltip>
);
}
return returnNode;
};
export default MenuItem;