2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { forwardRef } from 'react';
|
2023-05-29 13:58:36 +08:00
|
|
|
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
|
|
|
|
import classNames from 'classnames';
|
2022-07-28 20:33:10 +08:00
|
|
|
import type { MenuProps as RcMenuProps, MenuRef as RcMenuRef } from 'rc-menu';
|
|
|
|
import RcMenu from 'rc-menu';
|
2023-08-08 16:48:26 +08:00
|
|
|
import { useEvent } from 'rc-util';
|
2023-05-29 13:58:36 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-07-28 20:33:10 +08:00
|
|
|
import initCollapseMotion from '../_util/motion';
|
2024-02-24 14:50:03 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2024-05-18 15:07:26 +08:00
|
|
|
import type { GetProp } from '../_util/type';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-05-06 15:49:37 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2024-02-24 14:50:03 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2023-05-29 13:58:36 +08:00
|
|
|
import type { SiderContextProps } from '../layout/Sider';
|
2024-05-18 15:07:26 +08:00
|
|
|
import type { ItemType } from './interface';
|
2023-05-29 13:58:36 +08:00
|
|
|
import type { MenuContextProps, MenuTheme } from './MenuContext';
|
|
|
|
import MenuContext from './MenuContext';
|
2024-06-22 21:59:12 +08:00
|
|
|
import Divider from './MenuDivider';
|
2024-05-18 15:07:26 +08:00
|
|
|
import MenuItem from './MenuItem';
|
2022-07-28 20:33:10 +08:00
|
|
|
import OverrideContext from './OverrideContext';
|
2023-05-29 13:58:36 +08:00
|
|
|
import useStyle from './style';
|
2024-05-18 15:07:26 +08:00
|
|
|
import SubMenu from './SubMenu';
|
2022-07-28 20:33:10 +08:00
|
|
|
|
2024-02-26 09:55:08 +08:00
|
|
|
function isEmptyIcon(icon?: React.ReactNode) {
|
|
|
|
return icon === null || icon === false;
|
|
|
|
}
|
|
|
|
|
2024-05-18 15:07:26 +08:00
|
|
|
const MENU_COMPONENTS: GetProp<RcMenuProps, '_internalComponents'> = {
|
|
|
|
item: MenuItem,
|
|
|
|
submenu: SubMenu,
|
|
|
|
divider: Divider,
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface MenuProps extends Omit<RcMenuProps, 'items' | '_internalComponents'> {
|
2022-07-28 20:33:10 +08:00
|
|
|
theme?: MenuTheme;
|
|
|
|
inlineIndent?: number;
|
|
|
|
|
|
|
|
// >>>>> Private
|
|
|
|
/**
|
|
|
|
* @private Internal Usage. Not promise crash if used in production. Connect with chenshuai2144
|
|
|
|
* for removing.
|
|
|
|
*/
|
|
|
|
_internalDisableMenuItemTitleTooltip?: boolean;
|
|
|
|
|
|
|
|
items?: ItemType[];
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:17:26 +08:00
|
|
|
type InternalMenuProps = MenuProps &
|
2022-07-28 20:33:10 +08:00
|
|
|
SiderContextProps & {
|
|
|
|
collapsedWidth?: string | number;
|
|
|
|
};
|
|
|
|
|
|
|
|
const InternalMenu = forwardRef<RcMenuRef, InternalMenuProps>((props, ref) => {
|
|
|
|
const override = React.useContext(OverrideContext);
|
|
|
|
const overrideObj = override || {};
|
|
|
|
|
2023-07-05 19:48:33 +08:00
|
|
|
const { getPrefixCls, getPopupContainer, direction, menu } = React.useContext(ConfigContext);
|
2022-07-28 20:33:10 +08:00
|
|
|
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2022-09-19 22:17:26 +08:00
|
|
|
className,
|
2023-07-05 19:48:33 +08:00
|
|
|
style,
|
2022-09-19 22:17:26 +08:00
|
|
|
theme = 'light',
|
2022-07-28 20:33:10 +08:00
|
|
|
expandIcon,
|
|
|
|
_internalDisableMenuItemTitleTooltip,
|
|
|
|
inlineCollapsed,
|
|
|
|
siderCollapsed,
|
|
|
|
rootClassName,
|
|
|
|
mode,
|
|
|
|
selectable,
|
|
|
|
onClick,
|
2023-05-29 13:58:36 +08:00
|
|
|
overflowedIndicatorPopupClassName,
|
2022-07-28 20:33:10 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const passedProps = omit(restProps, ['collapsedWidth']);
|
|
|
|
|
|
|
|
// ======================== Warning ==========================
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Menu');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
warning(
|
|
|
|
!('inlineCollapsed' in props && mode !== 'inline'),
|
|
|
|
'usage',
|
|
|
|
'`inlineCollapsed` should only be used when `mode` is inline.',
|
|
|
|
);
|
|
|
|
|
|
|
|
warning(
|
|
|
|
!(props.siderCollapsed !== undefined && 'inlineCollapsed' in props),
|
|
|
|
'usage',
|
|
|
|
'`inlineCollapsed` not control Menu under Sider. Should set `collapsed` on Sider instead.',
|
|
|
|
);
|
|
|
|
|
2024-05-18 15:07:26 +08:00
|
|
|
warning.deprecated('items' in props && !props.children, 'children', 'items');
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
2022-07-28 20:33:10 +08:00
|
|
|
|
|
|
|
overrideObj.validator?.({ mode });
|
|
|
|
|
|
|
|
// ========================== Click ==========================
|
|
|
|
// Tell dropdown that item clicked
|
|
|
|
const onItemClick = useEvent<Required<MenuProps>['onClick']>((...args) => {
|
|
|
|
onClick?.(...args);
|
|
|
|
overrideObj.onClick?.();
|
|
|
|
});
|
|
|
|
|
|
|
|
// ========================== Mode ===========================
|
|
|
|
const mergedMode = overrideObj.mode || mode;
|
|
|
|
|
|
|
|
// ======================= Selectable ========================
|
|
|
|
const mergedSelectable = selectable ?? overrideObj.selectable;
|
|
|
|
|
|
|
|
// ======================== Collapsed ========================
|
|
|
|
// Inline Collapsed
|
|
|
|
const mergedInlineCollapsed = React.useMemo(() => {
|
|
|
|
if (siderCollapsed !== undefined) {
|
|
|
|
return siderCollapsed;
|
|
|
|
}
|
|
|
|
return inlineCollapsed;
|
|
|
|
}, [inlineCollapsed, siderCollapsed]);
|
|
|
|
|
2024-02-26 09:55:08 +08:00
|
|
|
const defaultMotions: MenuProps['defaultMotions'] = {
|
2022-07-28 20:33:10 +08:00
|
|
|
horizontal: { motionName: `${rootPrefixCls}-slide-up` },
|
|
|
|
inline: initCollapseMotion(rootPrefixCls),
|
|
|
|
other: { motionName: `${rootPrefixCls}-zoom-big` },
|
|
|
|
};
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('menu', customizePrefixCls || overrideObj.prefixCls);
|
2023-11-13 09:38:05 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls, !override);
|
2023-07-05 19:48:33 +08:00
|
|
|
const menuClassName = classNames(`${prefixCls}-${theme}`, menu?.className, className);
|
2022-07-28 20:33:10 +08:00
|
|
|
|
2024-02-26 09:55:08 +08:00
|
|
|
// ====================== ExpandIcon ========================
|
|
|
|
const mergedExpandIcon = React.useMemo<MenuProps['expandIcon']>(() => {
|
|
|
|
if (typeof expandIcon === 'function' || isEmptyIcon(expandIcon)) {
|
|
|
|
return expandIcon || null;
|
|
|
|
}
|
|
|
|
if (typeof overrideObj.expandIcon === 'function' || isEmptyIcon(overrideObj.expandIcon)) {
|
|
|
|
return overrideObj.expandIcon || null;
|
|
|
|
}
|
|
|
|
if (typeof menu?.expandIcon === 'function' || isEmptyIcon(menu?.expandIcon)) {
|
|
|
|
return menu?.expandIcon || null;
|
|
|
|
}
|
|
|
|
const mergedIcon = expandIcon ?? overrideObj?.expandIcon ?? menu?.expandIcon;
|
|
|
|
return cloneElement(mergedIcon, {
|
2023-08-03 20:17:32 +08:00
|
|
|
className: classNames(
|
|
|
|
`${prefixCls}-submenu-expand-icon`,
|
2024-06-23 14:57:41 +08:00
|
|
|
React.isValidElement<any>(mergedIcon) ? mergedIcon.props?.className : undefined,
|
2023-08-03 20:17:32 +08:00
|
|
|
),
|
2022-07-28 20:33:10 +08:00
|
|
|
});
|
2024-02-26 09:55:08 +08:00
|
|
|
}, [expandIcon, overrideObj?.expandIcon, menu?.expandIcon, prefixCls]);
|
2022-07-28 20:33:10 +08:00
|
|
|
|
|
|
|
// ======================== Context ==========================
|
2022-11-21 09:52:33 +08:00
|
|
|
const contextValue = React.useMemo<MenuContextProps>(
|
2022-07-28 20:33:10 +08:00
|
|
|
() => ({
|
|
|
|
prefixCls,
|
|
|
|
inlineCollapsed: mergedInlineCollapsed || false,
|
|
|
|
direction,
|
|
|
|
firstLevel: true,
|
2022-09-19 22:17:26 +08:00
|
|
|
theme,
|
2022-11-18 00:18:18 +08:00
|
|
|
mode: mergedMode,
|
2022-07-28 20:33:10 +08:00
|
|
|
disableMenuItemTitleTooltip: _internalDisableMenuItemTitleTooltip,
|
|
|
|
}),
|
2022-09-19 22:17:26 +08:00
|
|
|
[prefixCls, mergedInlineCollapsed, direction, _internalDisableMenuItemTitleTooltip, theme],
|
2022-07-28 20:33:10 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// ========================= Render ==========================
|
2023-11-13 09:38:05 +08:00
|
|
|
return wrapCSSVar(
|
2022-07-28 20:33:10 +08:00
|
|
|
<OverrideContext.Provider value={null}>
|
|
|
|
<MenuContext.Provider value={contextValue}>
|
|
|
|
<RcMenu
|
|
|
|
getPopupContainer={getPopupContainer}
|
|
|
|
overflowedIndicator={<EllipsisOutlined />}
|
2023-05-29 13:58:36 +08:00
|
|
|
overflowedIndicatorPopupClassName={classNames(
|
|
|
|
prefixCls,
|
|
|
|
`${prefixCls}-${theme}`,
|
|
|
|
overflowedIndicatorPopupClassName,
|
|
|
|
)}
|
2022-07-28 20:33:10 +08:00
|
|
|
mode={mergedMode}
|
|
|
|
selectable={mergedSelectable}
|
|
|
|
onClick={onItemClick}
|
|
|
|
{...passedProps}
|
|
|
|
inlineCollapsed={mergedInlineCollapsed}
|
2023-07-05 19:48:33 +08:00
|
|
|
style={{ ...menu?.style, ...style }}
|
2022-09-19 22:17:26 +08:00
|
|
|
className={menuClassName}
|
2022-07-28 20:33:10 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
direction={direction}
|
|
|
|
defaultMotions={defaultMotions}
|
|
|
|
expandIcon={mergedExpandIcon}
|
|
|
|
ref={ref}
|
2023-12-14 14:58:53 +08:00
|
|
|
rootClassName={classNames(
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
|
|
|
overrideObj.rootClassName,
|
|
|
|
cssVarCls,
|
|
|
|
rootCls,
|
|
|
|
)}
|
2024-05-18 15:07:26 +08:00
|
|
|
_internalComponents={MENU_COMPONENTS}
|
|
|
|
/>
|
2022-07-28 20:33:10 +08:00
|
|
|
</MenuContext.Provider>
|
|
|
|
</OverrideContext.Provider>,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default InternalMenu;
|