SideBar.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // @flow
  2. // Copyright 2017 The go-ethereum Authors
  3. // This file is part of the go-ethereum library.
  4. //
  5. // The go-ethereum library is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // The go-ethereum library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  17. import React, {Component} from 'react';
  18. import withStyles from '@material-ui/core/styles/withStyles';
  19. import List from '@material-ui/core/List';
  20. import ListItem from '@material-ui/core/ListItem';
  21. import ListItemIcon from '@material-ui/core/ListItemIcon';
  22. import ListItemText from '@material-ui/core/ListItemText';
  23. import Icon from '@material-ui/core/Icon';
  24. import Transition from 'react-transition-group/Transition';
  25. import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
  26. import {MENU, DURATION} from '../common';
  27. // styles contains the constant styles of the component.
  28. const styles = {
  29. menu: {
  30. default: {
  31. transition: `margin-left ${DURATION}ms`,
  32. },
  33. transition: {
  34. entered: {marginLeft: -200},
  35. },
  36. },
  37. };
  38. // themeStyles returns the styles generated from the theme for the component.
  39. const themeStyles = theme => ({
  40. list: {
  41. background: theme.palette.grey[900],
  42. },
  43. listItem: {
  44. minWidth: theme.spacing.unit * 7,
  45. },
  46. icon: {
  47. fontSize: theme.spacing.unit * 3,
  48. overflow: 'unset',
  49. },
  50. });
  51. export type Props = {
  52. classes: Object, // injected by withStyles()
  53. opened: boolean,
  54. changeContent: string => void,
  55. };
  56. type State = {}
  57. // SideBar renders the sidebar of the dashboard.
  58. class SideBar extends Component<Props, State> {
  59. shouldComponentUpdate(nextProps: Readonly<Props>, nextState: Readonly<State>, nextContext: any) {
  60. return nextProps.opened !== this.props.opened;
  61. }
  62. // clickOn returns a click event handler function for the given menu item.
  63. clickOn = menu => (event) => {
  64. event.preventDefault();
  65. this.props.changeContent(menu);
  66. };
  67. // menuItems returns the menu items corresponding to the sidebar state.
  68. menuItems = (transitionState) => {
  69. const {classes} = this.props;
  70. const children = [];
  71. MENU.forEach((menu) => {
  72. children.push((
  73. <ListItem button key={menu.id} onClick={this.clickOn(menu.id)} className={classes.listItem}>
  74. <ListItemIcon>
  75. <Icon className={classes.icon}>
  76. <FontAwesomeIcon icon={menu.icon} />
  77. </Icon>
  78. </ListItemIcon>
  79. <ListItemText
  80. primary={menu.title}
  81. style={{
  82. ...styles.menu.default,
  83. ...styles.menu.transition[transitionState],
  84. padding: 0,
  85. }}
  86. />
  87. </ListItem>
  88. ));
  89. });
  90. return children;
  91. };
  92. // menu renders the list of the menu items.
  93. menu = (transitionState: Object) => (
  94. <div className={this.props.classes.list}>
  95. <List>
  96. {this.menuItems(transitionState)}
  97. </List>
  98. </div>
  99. );
  100. render() {
  101. return (
  102. <Transition mountOnEnter in={this.props.opened} timeout={{enter: DURATION}}>
  103. {this.menu}
  104. </Transition>
  105. );
  106. }
  107. }
  108. export default withStyles(themeStyles)(SideBar);