ant-design/components/menu/SubMenu.tsx
David 2843bd32dd
feat: Add ability to overwrite Menu theme at Menu.SubMenu level. (#33971)
* Add theme support to Menu.SubMenu

* Tidy up docs

* Tidy up docs

* Update components/menu/index.en-US.md

* Update components/menu/index.en-US.md

Co-authored-by: Amumu <yoyo837@hotmail.com>

* Add Chinese lang docs

* Extend menu theme demo to additionally demonstrate Sub-menu theming

* Add submenu theme demo

* Revert "Extend menu theme demo to additionally demonstrate Sub-menu theming"

This reverts commit 642a2b5b72.

* Add tests

* Correct typo

* Make demo vertical so absolutely positioned popover doesn't overflow

* Make demo functional component

* Update components/menu/index.en-US.md

Co-authored-by: afc163 <afc163@gmail.com>

* Update components/menu/index.zh-CN.md

Co-authored-by: Amumu <yoyo837@hotmail.com>

* Update components/menu/demo/submenu-theme.md

Co-authored-by: MadCcc <1075746765@qq.com>

Co-authored-by: Amumu <yoyo837@hotmail.com>
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: MadCcc <1075746765@qq.com>
2022-02-14 15:21:33 +08:00

86 lines
2.4 KiB
TypeScript

import * as React from 'react';
import { SubMenu as RcSubMenu, useFullPath } from 'rc-menu';
import classNames from 'classnames';
import omit from 'rc-util/lib/omit';
import MenuContext, { MenuTheme } from './MenuContext';
import { isValidElement, cloneElement } from '../_util/reactNode';
interface TitleEventEntity {
key: string;
domEvent: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>;
}
export interface SubMenuProps {
className?: string;
disabled?: boolean;
level?: number;
title?: React.ReactNode;
icon?: React.ReactNode;
style?: React.CSSProperties;
onTitleClick?: (e: TitleEventEntity) => void;
onTitleMouseEnter?: (e: TitleEventEntity) => void;
onTitleMouseLeave?: (e: TitleEventEntity) => void;
popupOffset?: [number, number];
popupClassName?: string;
children?: React.ReactNode;
theme?: MenuTheme;
}
function SubMenu(props: SubMenuProps) {
const { popupClassName, icon, title, theme } = props;
const context = React.useContext(MenuContext);
const { prefixCls, inlineCollapsed, antdMenuTheme } = 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 = isValidElement(title) && title.type === 'span';
titleNode = (
<>
{cloneElement(icon, {
className: classNames(
isValidElement(icon) ? icon.props?.className : '',
`${prefixCls}-item-icon`,
),
})}
{titleIsSpan ? title : <span className={`${prefixCls}-title-content`}>{title}</span>}
</>
);
}
const contextValue = React.useMemo(
() => ({
...context,
firstLevel: false,
}),
[context],
);
return (
<MenuContext.Provider value={contextValue}>
<RcSubMenu
{...omit(props, ['icon'])}
title={titleNode}
popupClassName={classNames(
prefixCls,
`${prefixCls}-${theme || antdMenuTheme}`,
popupClassName,
)}
/>
</MenuContext.Provider>
);
}
export default SubMenu;