ant-design/components/menu/index.tsx

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as React from 'react';
import RcMenu, { Item, Divider, SubMenu, ItemGroup } from 'rc-menu';
2016-06-01 12:08:08 +08:00
import animation from '../_util/openAnimation';
2015-08-06 16:49:54 +08:00
2015-11-05 14:18:46 +08:00
function noop() {
}
export default class Menu extends React.Component {
static Divider = Divider;
static Item = Item;
static SubMenu = SubMenu;
static ItemGroup = ItemGroup;
static defaultProps = {
prefixCls: 'ant-menu',
onClick: noop,
onOpen: noop,
onClose: noop,
className: '',
theme: 'light', // or dark
2016-07-13 11:14:24 +08:00
};
constructor(props) {
super(props);
this.state = {
2016-05-07 16:06:02 +08:00
openKeys: [],
2015-08-06 16:49:54 +08:00
};
}
componentWillReceiveProps(nextProps) {
if (this.props.mode === 'inline' &&
nextProps.mode !== 'inline') {
this.switchModeFromInline = true;
}
2016-05-06 18:07:23 +08:00
if ('openKeys' in nextProps) {
2016-05-07 16:06:02 +08:00
this.setOpenKeys(nextProps.openKeys);
2016-05-06 18:07:23 +08:00
}
2016-04-27 17:44:25 +08:00
}
handleClick = (e) => {
2016-05-07 16:06:02 +08:00
this.setOpenKeys([]);
2015-11-05 19:09:33 +08:00
this.props.onClick(e);
}
handleOpenKeys = (e) => {
2016-05-06 18:07:23 +08:00
const { openKeys } = e;
2016-05-07 16:06:02 +08:00
this.setOpenKeys(openKeys);
2015-12-27 13:34:59 +08:00
this.props.onOpen(e);
}
handleCloseKeys = (e) => {
2016-05-06 18:07:23 +08:00
const { openKeys } = e;
2016-05-07 16:06:02 +08:00
this.setOpenKeys(openKeys);
this.props.onClose(e);
}
setOpenKeys(openKeys) {
2016-05-06 18:07:23 +08:00
if (!('openKeys' in this.props)) {
this.setState({ openKeys });
}
}
2015-08-24 18:18:46 +08:00
render() {
let openAnimation = this.props.openAnimation || this.props.openTransitionName;
if (!openAnimation) {
switch (this.props.mode) {
2016-01-21 22:45:21 +08:00
case 'horizontal':
openAnimation = 'slide-up';
break;
case 'vertical':
// When mode switch from inline
// submenu should hide without animation
if (this.switchModeFromInline) {
openAnimation = '';
this.switchModeFromInline = false;
} else {
openAnimation = 'zoom-big';
}
2016-01-21 22:45:21 +08:00
break;
case 'inline':
openAnimation = animation;
break;
default:
}
2015-08-24 18:18:46 +08:00
}
2015-11-12 14:57:54 +08:00
let props = {};
const className = `${this.props.className} ${this.props.prefixCls}-${this.props.theme}`;
if (this.props.mode !== 'inline') {
// 这组属性的目的是
// 弹出型的菜单需要点击后立即关闭
// 另外,弹出型的菜单的受控模式没有使用场景
props = {
openKeys: this.state.openKeys,
onClick: this.handleClick,
onOpen: this.handleOpenKeys,
onClose: this.handleCloseKeys,
openTransitionName: openAnimation,
className,
};
2015-08-24 18:18:46 +08:00
} else {
props = {
openAnimation,
className,
};
2015-08-24 18:18:46 +08:00
}
return <RcMenu {...this.props} {...props} />;
2015-08-06 16:49:54 +08:00
}
}