Info.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React, { Component } from 'react';
  2. import { connect } from 'dva';
  3. import router from 'umi/router';
  4. import { FormattedMessage } from 'umi/locale';
  5. import { Menu } from 'antd';
  6. import GridContent from '@/components/PageHeaderWrapper/GridContent';
  7. import styles from './Info.less';
  8. const { Item } = Menu;
  9. @connect(({ user }) => ({
  10. currentUser: user.currentUser,
  11. }))
  12. export default class Info extends Component {
  13. constructor(props) {
  14. super(props);
  15. const { match, location } = props;
  16. const menuMap = {
  17. base: <FormattedMessage id="app.settings.menuMap.basic" defaultMessage="Basic Settings" />,
  18. security: (
  19. <FormattedMessage id="app.settings.menuMap.security" defaultMessage="Security Settings" />
  20. ),
  21. binding: (
  22. <FormattedMessage id="app.settings.menuMap.binding" defaultMessage="Account Binding" />
  23. ),
  24. notification: (
  25. <FormattedMessage
  26. id="app.settings.menuMap.notification"
  27. defaultMessage="New Message Notification"
  28. />
  29. ),
  30. };
  31. const key = location.pathname.replace(`${match.path}/`, '');
  32. this.state = {
  33. mode: 'inline',
  34. menuMap,
  35. selectKey: menuMap[key] ? key : 'base',
  36. };
  37. }
  38. static getDerivedStateFromProps(props, state) {
  39. const { match, location } = props;
  40. let selectKey = location.pathname.replace(`${match.path}/`, '');
  41. selectKey = state.menuMap[selectKey] ? selectKey : 'base';
  42. if (selectKey !== state.selectKey) {
  43. return { selectKey };
  44. }
  45. return null;
  46. }
  47. componentDidMount() {
  48. window.addEventListener('resize', this.resize);
  49. this.resize();
  50. }
  51. componentWillUnmount() {
  52. window.removeEventListener('resize', this.resize);
  53. }
  54. getmenu = () => {
  55. const { menuMap } = this.state;
  56. return Object.keys(menuMap).map(item => <Item key={item}>{menuMap[item]}</Item>);
  57. };
  58. getRightTitle = () => {
  59. const { selectKey, menuMap } = this.state;
  60. return menuMap[selectKey];
  61. };
  62. selectKey = ({ key }) => {
  63. router.push(`/account/settings/${key}`);
  64. this.setState({
  65. selectKey: key,
  66. });
  67. };
  68. resize = () => {
  69. if (!this.main) {
  70. return;
  71. }
  72. requestAnimationFrame(() => {
  73. let mode = 'inline';
  74. const { offsetWidth } = this.main;
  75. if (this.main.offsetWidth < 641 && offsetWidth > 400) {
  76. mode = 'horizontal';
  77. }
  78. if (window.innerWidth < 768 && offsetWidth > 400) {
  79. mode = 'horizontal';
  80. }
  81. this.setState({
  82. mode,
  83. });
  84. });
  85. };
  86. render() {
  87. const { children, currentUser } = this.props;
  88. if (!currentUser.userid) {
  89. return '';
  90. }
  91. const { mode, selectKey } = this.state;
  92. return (
  93. <GridContent>
  94. <div
  95. className={styles.main}
  96. ref={ref => {
  97. this.main = ref;
  98. }}
  99. >
  100. <div className={styles.leftmenu}>
  101. <Menu mode={mode} selectedKeys={[selectKey]} onClick={this.selectKey}>
  102. {this.getmenu()}
  103. </Menu>
  104. </div>
  105. <div className={styles.right}>
  106. <div className={styles.title}>{this.getRightTitle()}</div>
  107. {children}
  108. </div>
  109. </div>
  110. </GridContent>
  111. );
  112. }
  113. }