index.jsx 2.0 KB
Newer Older
Y
yiminghe 已提交
1
import React from 'react';
2
import RcMenu, { Item, Divider, SubMenu, ItemGroup } from 'rc-menu';
Y
yiminghe 已提交
3
import animation from '../common/openAnimation';
Y
yiminghe 已提交
4

A
afc163 已提交
5 6 7
function noop() {
}

8
const Menu = React.createClass({
J
jljsj 已提交
9
  getDefaultProps() {
Y
yiminghe 已提交
10
    return {
A
afc163 已提交
11 12 13 14
      prefixCls: 'ant-menu',
      onClick: noop,
      onOpen: noop,
      onClose: noop,
A
afc163 已提交
15 16
      className: '',
      theme: 'light',  // or dark
A
afc163 已提交
17 18 19 20 21
    };
  },
  getInitialState() {
    return {
      openKeys: []
Y
yiminghe 已提交
22 23
    };
  },
A
afc163 已提交
24
  handleClick(e) {
A
afc163 已提交
25 26 27
    this.setState({
      openKeys: []
    });
A
afc163 已提交
28
    this.props.onClick(e);
A
afc163 已提交
29 30 31 32 33
  },
  handleOpenKeys(e) {
    this.setState({
      openKeys: e.openKeys
    });
A
afc163 已提交
34
    this.props.onOpen(e);
A
afc163 已提交
35 36 37 38 39
  },
  handleCloseKeys(e) {
    this.setState({
      openKeys: e.openKeys
    });
A
afc163 已提交
40
    this.props.onClose(e);
A
afc163 已提交
41
  },
J
jljsj 已提交
42
  render() {
43 44 45
    let openAnimation = this.props.openAnimation || this.props.openTransitionName;
    if (!openAnimation) {
      switch (this.props.mode) {
A
afc163 已提交
46 47 48 49 50 51 52 53 54 55
        case 'horizontal':
          openAnimation = 'slide-up';
          break;
        case 'vertical':
          openAnimation = 'zoom-big';
          break;
        case 'inline':
          openAnimation = animation;
          break;
        default:
56
      }
J
jljsj 已提交
57
    }
Y
yuntao.qyt 已提交
58

59
    let props = {};
60
    const className = `${this.props.className} ${this.props.prefixCls}-${this.props.theme}`;
A
afc163 已提交
61
    if (this.props.mode !== 'inline') {
62 63 64 65 66 67 68 69
      // 这组属性的目的是
      // 弹出型的菜单需要点击后立即关闭
      // 另外,弹出型的菜单的受控模式没有使用场景
      props = {
        openKeys: this.state.openKeys,
        onClick: this.handleClick,
        onOpen: this.handleOpenKeys,
        onClose: this.handleCloseKeys,
A
afc163 已提交
70
        openTransitionName: openAnimation,
71 72
        className,
      };
J
jljsj 已提交
73
    } else {
74
      props = {
A
afc163 已提交
75
        openAnimation,
76 77
        className,
      };
J
jljsj 已提交
78
    }
79
    return <RcMenu {...this.props} {...props} />;
Y
yiminghe 已提交
80 81 82
  }
});

83 84 85 86
Menu.Divider = Divider;
Menu.Item = Item;
Menu.SubMenu = SubMenu;
Menu.ItemGroup = ItemGroup;
Y
yiminghe 已提交
87

88
export default Menu;