2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2017-11-11 00:07:03 +08:00
|
|
|
import { SubMenu as RcSubMenu } from 'rc-menu';
|
|
|
|
import classNames from 'classnames';
|
2020-04-26 23:16:15 +08:00
|
|
|
import omit from 'omit.js';
|
2019-07-22 21:15:48 +08:00
|
|
|
import MenuContext, { MenuContextProps } from './MenuContext';
|
2019-04-05 16:15:01 +08:00
|
|
|
|
2019-02-06 08:35:47 +08:00
|
|
|
interface TitleEventEntity {
|
2019-01-28 13:29:20 +08:00
|
|
|
key: string;
|
|
|
|
domEvent: Event;
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:32:35 +08:00
|
|
|
export interface SubMenuProps {
|
|
|
|
rootPrefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
title?: React.ReactNode;
|
2020-04-26 23:16:15 +08:00
|
|
|
icon?: React.ReactNode;
|
2019-01-30 16:26:32 +08:00
|
|
|
style?: React.CSSProperties;
|
2019-02-06 08:35:47 +08:00
|
|
|
onTitleClick?: (e: TitleEventEntity) => void;
|
|
|
|
onTitleMouseEnter?: (e: TitleEventEntity) => void;
|
|
|
|
onTitleMouseLeave?: (e: TitleEventEntity) => void;
|
2019-06-22 14:53:51 +08:00
|
|
|
popupOffset?: [number, number];
|
2019-08-17 22:38:11 +08:00
|
|
|
popupClassName?: string;
|
2019-01-23 10:32:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class SubMenu extends React.Component<SubMenuProps, any> {
|
2017-11-11 00:07:03 +08:00
|
|
|
static contextTypes = {
|
|
|
|
antdMenuTheme: PropTypes.string,
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-06-01 18:09:11 +08:00
|
|
|
// fix issue:https://github.com/ant-design/ant-design/issues/8666
|
|
|
|
static isSubMenu = 1;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2017-11-16 16:08:30 +08:00
|
|
|
private subMenu: any;
|
2018-12-13 22:03:12 +08:00
|
|
|
|
2017-11-21 17:38:51 +08:00
|
|
|
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
|
2017-11-16 16:08:30 +08:00
|
|
|
this.subMenu.onKeyDown(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-03-12 19:52:43 +08:00
|
|
|
|
2017-11-21 17:38:51 +08:00
|
|
|
saveSubMenu = (subMenu: any) => {
|
2017-11-16 16:08:30 +08:00
|
|
|
this.subMenu = subMenu;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-03-12 19:52:43 +08:00
|
|
|
|
2020-04-26 23:16:15 +08:00
|
|
|
renderTitle() {
|
|
|
|
const { icon, title } = this.props;
|
|
|
|
if (!icon) {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
// 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';
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{icon}
|
|
|
|
{titleIsSpan ? title : <span>{title}</span>}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:07:03 +08:00
|
|
|
render() {
|
2019-08-17 22:38:11 +08:00
|
|
|
const { rootPrefixCls, popupClassName } = this.props;
|
2017-11-11 00:07:03 +08:00
|
|
|
return (
|
2019-04-05 16:15:01 +08:00
|
|
|
<MenuContext.Consumer>
|
|
|
|
{({ antdMenuTheme }: MenuContextProps) => (
|
|
|
|
<RcSubMenu
|
2020-04-26 23:16:15 +08:00
|
|
|
{...omit(this.props, ['icon'])}
|
|
|
|
title={this.renderTitle()}
|
2019-04-05 16:15:01 +08:00
|
|
|
ref={this.saveSubMenu}
|
2020-04-07 13:32:29 +08:00
|
|
|
popupClassName={classNames(
|
|
|
|
rootPrefixCls,
|
|
|
|
`${rootPrefixCls}-${antdMenuTheme}`,
|
|
|
|
popupClassName,
|
|
|
|
)}
|
2019-04-05 16:15:01 +08:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</MenuContext.Consumer>
|
2017-11-11 00:07:03 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SubMenu;
|