leftMenu.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React from 'react';
  2. import { Menu, Icon, Layout } from 'antd';
  3. import '@/account/leftTab.less';
  4. const SubMenu = Menu.SubMenu;
  5. import { Link } from 'react-router';
  6. import { adminMenu } from '../menu';
  7. import logo from 'img/acc-logo.png';
  8. const LeftTab = React.createClass({
  9. getInitialState() {
  10. return {
  11. openKeys: [ 'sub1' ],
  12. current: 'index',
  13. switch: false,
  14. rootSubmenuKeys :['sub1', 'sub2','sub3','sub4','sub5','sub6','sub7','sub8','sub9','sub10']
  15. };
  16. },
  17. menuClick(e) {
  18. this.setState({
  19. current: e.key,
  20. });
  21. window.location.hash = e.key;
  22. },
  23. iconFun() {
  24. let menuW = document.querySelector('.ant-layout-sider');
  25. if (this.state.switch) {
  26. menuW.style.width = '200px';
  27. menuW.style.flex = '0 0 200px';
  28. } else {
  29. menuW.style.width = '50px';
  30. menuW.style.flex = '0 0 50px';
  31. }
  32. this.setState({
  33. switch: !this.state.switch
  34. });
  35. },
  36. onOpenChange(openKeys){
  37. const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1);
  38. if (this.state.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  39. this.setState({ openKeys });
  40. } else {
  41. this.setState({
  42. openKeys: latestOpenKey ? [latestOpenKey] : [],
  43. });
  44. }
  45. },
  46. componentWillMount() {
  47. if (window.location.hash) {
  48. let current = window.location.hash.substr(2),
  49. open=[];
  50. if (!current.length) {
  51. window.location.hash = 'index';
  52. this.setState({
  53. openKeys:['sub1'],
  54. current:'index'
  55. })
  56. }else{
  57. adminMenu.map(item=>{
  58. if(item.children){
  59. item.children.map(atem=>{
  60. if(current==atem.url){
  61. open.push(item.url)
  62. return false;
  63. }
  64. })
  65. }
  66. })
  67. this.setState({
  68. openKeys:open,
  69. current
  70. });
  71. }
  72. }else{
  73. window.location.hash = 'index';
  74. this.setState({
  75. openKeys:['sub1'],
  76. current:'index'
  77. })
  78. }
  79. },
  80. render() {
  81. return (
  82. <div>
  83. <div className="acc-index">
  84. {!this.state.switch && <img src={logo} alt="" />}
  85. {!this.state.switch && (
  86. <span>
  87. <a href={globalConfig.context + "/admin/index.html"}>管理员中心</a>
  88. </span>
  89. )}
  90. <Icon type={this.state.switch ? 'forward' : 'backward'} className="icon" onClick={this.iconFun} />
  91. </div>
  92. <Menu
  93. onClick={this.menuClick}
  94. defaultOpenKeys={this.state.openKeys}
  95. selectedKeys={[ this.state.current ]}
  96. openKeys={this.state.openKeys}
  97. onOpenChange={this.onOpenChange}
  98. mode={this.state.switch ? 'vertical' : 'inline'}
  99. theme="light"
  100. className="account-left"
  101. >
  102. {adminMenu.map((subMenu) => {
  103. if (subMenu.children && subMenu.children.length) {
  104. return (
  105. <SubMenu
  106. key={subMenu.url}
  107. title={
  108. <span>
  109. <Icon type={subMenu.icon} />
  110. {!this.state.switch && <span>{subMenu.name}</span>}
  111. </span>
  112. }
  113. >
  114. {subMenu.children.map((menu) => <Menu.Item key={menu.url}>{menu.name}</Menu.Item>)}
  115. </SubMenu>
  116. );
  117. }
  118. return (
  119. <Menu.Item key={subMenu.url}>
  120. <Link to={`/${subMenu.url}`}>
  121. <Icon type={subMenu.icon} />
  122. <span className="nav-text">{subMenu.name}</span>
  123. </Link>
  124. </Menu.Item>
  125. );
  126. })}
  127. </Menu>
  128. </div>
  129. );
  130. }
  131. });
  132. export default LeftTab;