mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-23 01:45:05 +08:00
af707aa26b
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
Co-authored-by: thinkasany <480968828@qq.com>
81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import * as React from 'react';
|
|
import omit from '@rc-component/util/lib/omit';
|
|
import classNames from 'classnames';
|
|
import { SubMenu as RcSubMenu, useFullPath } from 'rc-menu';
|
|
|
|
import { useZIndex } from '../_util/hooks/useZIndex';
|
|
import { cloneElement } from '../_util/reactNode';
|
|
import type { SubMenuType } from './interface';
|
|
import type { MenuContextProps } from './MenuContext';
|
|
import MenuContext from './MenuContext';
|
|
|
|
export interface SubMenuProps extends Omit<SubMenuType, 'ref' | 'key' | 'children' | 'label'> {
|
|
title?: React.ReactNode;
|
|
children?: React.ReactNode;
|
|
}
|
|
|
|
const SubMenu: React.FC<SubMenuProps> = (props) => {
|
|
const { popupClassName, icon, title, theme: customTheme } = props;
|
|
const context = React.useContext(MenuContext);
|
|
const { prefixCls, inlineCollapsed, theme: contextTheme } = context;
|
|
|
|
const parentPath = useFullPath();
|
|
|
|
let titleNode: React.ReactNode;
|
|
|
|
if (!icon) {
|
|
titleNode =
|
|
inlineCollapsed && !parentPath.length && title && typeof title === 'string' ? (
|
|
<div className={`${prefixCls}-inline-collapsed-noicon`}>{title.charAt(0)}</div>
|
|
) : (
|
|
<span className={`${prefixCls}-title-content`}>{title}</span>
|
|
);
|
|
} else {
|
|
// inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span
|
|
// ref: https://github.com/ant-design/ant-design/pull/23456
|
|
const titleIsSpan = React.isValidElement(title) && title.type === 'span';
|
|
titleNode = (
|
|
<>
|
|
{cloneElement(icon, {
|
|
className: classNames(
|
|
React.isValidElement(icon)
|
|
? (icon as React.ReactElement<{ className?: string }>).props?.className
|
|
: '',
|
|
`${prefixCls}-item-icon`,
|
|
),
|
|
})}
|
|
{titleIsSpan ? title : <span className={`${prefixCls}-title-content`}>{title}</span>}
|
|
</>
|
|
);
|
|
}
|
|
|
|
const contextValue = React.useMemo<MenuContextProps>(
|
|
() => ({ ...context, firstLevel: false }),
|
|
[context],
|
|
);
|
|
|
|
// ============================ zIndex ============================
|
|
const [zIndex] = useZIndex('Menu');
|
|
|
|
return (
|
|
<MenuContext.Provider value={contextValue}>
|
|
<RcSubMenu
|
|
{...omit(props, ['icon'])}
|
|
title={titleNode}
|
|
popupClassName={classNames(
|
|
prefixCls,
|
|
popupClassName,
|
|
`${prefixCls}-${customTheme || contextTheme}`,
|
|
)}
|
|
popupStyle={{
|
|
zIndex,
|
|
// fix: https://github.com/ant-design/ant-design/issues/47826#issuecomment-2360737237
|
|
...props.popupStyle,
|
|
}}
|
|
/>
|
|
</MenuContext.Provider>
|
|
);
|
|
};
|
|
|
|
export default SubMenu;
|