2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-07-06 11:40:52 +08:00
|
|
|
import RcMenu, { Divider, ItemGroup, MenuProps as RcMenuProps } from 'rc-menu';
|
2017-06-30 18:08:30 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-24 23:24:06 +08:00
|
|
|
import SubMenu, { SubMenuProps } from './SubMenu';
|
|
|
|
import Item, { MenuItemProps } from './MenuItem';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2019-04-05 16:15:01 +08:00
|
|
|
import { SiderContext, SiderContextProps } from '../layout/Sider';
|
2019-10-14 15:01:16 +08:00
|
|
|
import collapseMotion from '../_util/motion';
|
2020-11-06 15:25:43 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2019-07-22 21:15:48 +08:00
|
|
|
import MenuContext, { MenuTheme } from './MenuContext';
|
2015-08-06 16:49:54 +08:00
|
|
|
|
2020-07-06 11:40:52 +08:00
|
|
|
export { MenuItemGroupProps } from 'rc-menu';
|
2016-08-15 12:00:05 +08:00
|
|
|
|
2017-11-21 17:38:51 +08:00
|
|
|
export type MenuMode = 'vertical' | 'vertical-left' | 'vertical-right' | 'horizontal' | 'inline';
|
|
|
|
|
2020-07-06 11:40:52 +08:00
|
|
|
export interface MenuProps extends RcMenuProps {
|
2018-04-10 10:50:59 +08:00
|
|
|
theme?: MenuTheme;
|
2017-04-26 10:11:03 +08:00
|
|
|
inlineIndent?: number;
|
2018-06-06 21:05:32 +08:00
|
|
|
focusable?: boolean;
|
2016-08-15 12:00:05 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
type InternalMenuProps = MenuProps & SiderContextProps;
|
|
|
|
|
2020-07-06 11:40:52 +08:00
|
|
|
class InternalMenu extends React.Component<InternalMenuProps> {
|
2018-06-06 21:05:32 +08:00
|
|
|
static defaultProps: Partial<MenuProps> = {
|
2016-03-28 23:21:47 +08:00
|
|
|
className: '',
|
2018-06-06 21:05:32 +08:00
|
|
|
theme: 'light', // or dark
|
2018-05-01 18:50:22 +08:00
|
|
|
focusable: false,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2018-12-13 02:07:58 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
constructor(props: InternalMenuProps) {
|
2016-03-28 23:21:47 +08:00
|
|
|
super(props);
|
2016-09-14 11:42:06 +08:00
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2017-06-30 20:27:39 +08:00
|
|
|
!('inlineCollapsed' in props && props.mode !== 'inline'),
|
2019-02-27 15:32:29 +08:00
|
|
|
'Menu',
|
|
|
|
'`inlineCollapsed` should only be used when `mode` is inline.',
|
2017-06-30 20:27:39 +08:00
|
|
|
);
|
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-05-27 20:53:43 +08:00
|
|
|
!(props.siderCollapsed !== undefined && 'inlineCollapsed' in props),
|
|
|
|
'Menu',
|
|
|
|
'`inlineCollapsed` not control Menu under Sider. Should set `collapsed` on Sider instead.',
|
|
|
|
);
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getInlineCollapsed() {
|
2020-04-30 21:29:16 +08:00
|
|
|
const { inlineCollapsed, siderCollapsed } = this.props;
|
|
|
|
if (siderCollapsed !== undefined) {
|
|
|
|
return siderCollapsed;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
return inlineCollapsed;
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderMenu = ({ getPopupContainer, getPrefixCls, direction }: ConfigConsumerProps) => {
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
|
2020-11-06 15:25:43 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, theme, expandIcon } = this.props;
|
2020-07-06 11:40:52 +08:00
|
|
|
const defaultMotions = {
|
2021-02-08 17:09:13 +08:00
|
|
|
horizontal: { motionName: `${rootPrefixCls}-slide-up` },
|
2020-08-05 21:23:15 +08:00
|
|
|
inline: collapseMotion,
|
2021-02-08 17:09:13 +08:00
|
|
|
other: { motionName: `${rootPrefixCls}-zoom-big` },
|
2020-07-06 11:40:52 +08:00
|
|
|
};
|
2017-06-30 18:08:30 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('menu', customizePrefixCls);
|
2020-09-06 13:07:39 +08:00
|
|
|
const menuClassName = classNames(
|
|
|
|
`${prefixCls}-${theme}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-inline-collapsed`]: this.getInlineCollapsed(),
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2015-11-12 14:57:54 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
return (
|
|
|
|
<MenuContext.Provider
|
|
|
|
value={{
|
|
|
|
inlineCollapsed: this.getInlineCollapsed() || false,
|
2020-04-30 21:29:16 +08:00
|
|
|
antdMenuTheme: theme,
|
2020-01-02 19:10:16 +08:00
|
|
|
direction,
|
2019-04-05 16:15:01 +08:00
|
|
|
}}
|
|
|
|
>
|
2020-01-02 19:10:16 +08:00
|
|
|
<RcMenu
|
|
|
|
getPopupContainer={getPopupContainer}
|
2020-07-06 11:40:52 +08:00
|
|
|
{...this.props}
|
|
|
|
className={menuClassName}
|
2020-01-02 19:10:16 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
direction={direction}
|
2020-07-06 11:40:52 +08:00
|
|
|
defaultMotions={defaultMotions}
|
2020-11-06 15:25:43 +08:00
|
|
|
expandIcon={cloneElement(expandIcon, {
|
|
|
|
className: `${prefixCls}-submenu-expand-icon`,
|
|
|
|
})}
|
2020-01-02 19:10:16 +08:00
|
|
|
/>
|
2019-04-05 16:15:01 +08:00
|
|
|
</MenuContext.Provider>
|
|
|
|
);
|
2020-01-02 19:10:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <ConfigConsumer>{this.renderMenu}</ConfigConsumer>;
|
2018-11-26 12:06:42 +08:00
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
2019-03-07 16:58:53 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
// We should keep this as ref-able
|
2021-01-24 23:24:06 +08:00
|
|
|
class Menu extends React.Component<MenuProps, {}> {
|
2019-04-05 16:15:01 +08:00
|
|
|
static Divider = Divider;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
static Item = Item;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
static SubMenu = SubMenu;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
static ItemGroup = ItemGroup;
|
2019-03-07 16:58:53 +08:00
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<SiderContext.Consumer>
|
|
|
|
{(context: SiderContextProps) => <InternalMenu {...this.props} {...context} />}
|
|
|
|
</SiderContext.Consumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 23:24:06 +08:00
|
|
|
|
|
|
|
export { MenuTheme, SubMenuProps, MenuItemProps };
|
|
|
|
|
|
|
|
export default Menu;
|