index.js 3.97 KB
Newer Older
bert committed
1 2 3 4 5 6 7 8
import React, { PropTypes } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { Row, Col, Icon, Menu, Dropdown } from 'antd'
import styles from './index.less'
// import styles from './index.less';
import { Link } from 'react-router'

bert committed
9
import * as menu from '../../store/modules/menu/menu_action'
bert committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

import logo from './logo.png'

const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
const DropdownButton = Dropdown.Button;

const defaultProps = {
  topMenu: [],
  currentIndex: 0
}

const propTypes = {
  topMenu: PropTypes.array,
  currentIndex: PropTypes.number
}

class Header extends React.Component {
  constructor () {
    super()
    this.onCollapseChange = this.onCollapseChange.bind(this);
    this.menuClickHandle = this.menuClickHandle.bind(this);
  }
  
  componentDidMount () {
    /**
     * 初始化头部菜单,通过服务端获取。
     */
bert committed
38
    this.props.menu.getTopMenu()
bert committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  }
    
  menuClickHandle (item) {
    /**
     * 加入二级导航时,可以通过判断字符串头标记来定位是住导航还是子导航
     * 
     */
    // if (item.key.indexOf('sub') != -1) {
    //   console.log('是主导航')
    // } else {
    //   console.log('是副级导航');
    // }
    
    /**
     * 特定功能:返回旧版
     * 
     * 判断 `key = index.html` 则跳转旧版
     * 因为在导航前加入了标记,所以这里判断时也需要加入 `sub` 标记 
     * 
     */
    
    /**
     * 默认情况更改 `面包屑` 信息
     */
    // this.props.menu.updateNavPath(item.keyPath, item.key)
  }
  
  onCollapseChange(collapse) {
    /**
     * 侧栏切换功能
     * 
     * 点击特定按钮更改侧栏状态,实现侧栏切换功能
     * 通过判断侧栏状态,显示不同的class
     * 该 `class` 在 `View/App` 下: `<div className={collapse ? "ant-layout-aside ant-layout-aside-collapse" : "ant-layout-aside"}>`
     * 该功能可以放在任意位置,目前放在顶部实现
     * 
     */
    // this.props.menu.updateCollapse(this.props.collapse)
  }
  
  render () {
bert committed
80 81
    const { topMenu } = this.props;
     console.log('topMenu', topMenu);
bert committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    const menu = topMenu.map((item) => {
      /**
       * 遍历 `topMenu` 显示顶部信息
       * 
       * `sub` 标记为是主导航
       * `key` 用于页面跳转与记录唯一
       * `name` 标签显示内容
       * 
       */
      return (
        <Menu.Item key={'sub'+ item.key}>
            <Link to={'' + item.key}>
              <Icon type={item.icon} />
              <span>{item.name}</span>
            </Link>
        </Menu.Item>
      )
    });
bert committed
100
    
bert committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    // return (
    //   <div className='ant-layout-header'>
    //     <div className="ant-layout-wrapper">
    //       <div className="collapse-icon" onClick={this.onCollapseChange}><Icon type="bars" /></div>
    //       <Menu 
    //         theme="light" 
    //         mode="horizontal" 
    //         onClick={this.menuClickHandle}
    //         defaultSelectedKeys={'sub' + selectKey} style={{lineHeight: '64px'}}>
    //         { menu }
    //       </Menu>
    //     </div>
    //   </div>
    // )
    return (
      <div className={styles['ant-layout-header']}>
        <div className={styles["ant-layout-wrapper"]}>
          <Menu 
            theme="light" 
            mode="horizontal" 
            onClick={this.menuClickHandle}
            style={{lineHeight: '64px'}}>
            { menu }
          </Menu>
        </div>
      </div>
    )
  }
}

Header.propTypes = propTypes;
Header.defaultProps = defaultProps;

const mapStateToProps = (state) => {
bert committed
135
  console.log('state', state);
bert committed
136
  return {    
bert committed
137
      topMenu       : state.menu.topMenu,
bert committed
138 139
      // currentIndex  : state.menu.currentIndex,
      // collapse      : state.menu.collapse,
bert committed
140
      // selectKey     : state.menu.selectKey
bert committed
141 142 143 144 145
  };
};

function mapDispatchToProps(dispatch) {
  return {
bert committed
146
    menu: bindActionCreators(menu, dispatch),
bert committed
147 148 149
  };
}

bert committed
150
export default connect(mapStateToProps, mapDispatchToProps)(Header);