SideBar.jsx 3.3 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/styles/withStyles';
  19. import List, {ListItem, ListItemIcon, ListItemText} from 'material-ui/List';
  20. import Icon from 'material-ui/Icon';
  21. import Transition from 'react-transition-group/Transition';
  22. import {Icon as FontAwesome} from 'react-fa';
  23. import {MENU, DURATION} from './Common';
  24. // menuDefault is the default style of the menu.
  25. const menuDefault = {
  26. transition: `margin-left ${DURATION}ms`,
  27. };
  28. // menuTransition is the additional style of the menu corresponding to the transition's state.
  29. const menuTransition = {
  30. entered: {marginLeft: -200},
  31. };
  32. // Styles for the SideBar component.
  33. const styles = theme => ({
  34. list: {
  35. background: theme.palette.background.appBar,
  36. },
  37. listItem: {
  38. minWidth: theme.spacing.unit * 3,
  39. },
  40. icon: {
  41. fontSize: theme.spacing.unit * 3,
  42. },
  43. });
  44. export type Props = {
  45. classes: Object,
  46. opened: boolean,
  47. changeContent: () => {},
  48. };
  49. // SideBar renders the sidebar of the dashboard.
  50. class SideBar extends Component<Props> {
  51. constructor(props) {
  52. super(props);
  53. // clickOn contains onClick event functions for the menu items.
  54. // Instantiate only once, and reuse the existing functions to prevent the creation of
  55. // new function instances every time the render method is triggered.
  56. this.clickOn = {};
  57. MENU.forEach((menu) => {
  58. this.clickOn[menu.id] = (event) => {
  59. event.preventDefault();
  60. props.changeContent(menu.id);
  61. };
  62. });
  63. }
  64. shouldComponentUpdate(nextProps) {
  65. return nextProps.opened !== this.props.opened;
  66. }
  67. menuItems = (transitionState) => {
  68. const {classes} = this.props;
  69. const children = [];
  70. MENU.forEach((menu) => {
  71. children.push(
  72. <ListItem button key={menu.id} onClick={this.clickOn[menu.id]} className={classes.listItem}>
  73. <ListItemIcon>
  74. <Icon className={classes.icon}>
  75. <FontAwesome name={menu.icon} />
  76. </Icon>
  77. </ListItemIcon>
  78. <ListItemText
  79. primary={menu.title}
  80. style={{
  81. ...menuDefault,
  82. ...menuTransition[transitionState],
  83. padding: 0,
  84. }}
  85. />
  86. </ListItem>,
  87. );
  88. });
  89. return children;
  90. };
  91. // menu renders the list of the menu items.
  92. menu = (transitionState) => {
  93. const {classes} = this.props; // The classes property is injected by withStyles().
  94. return (
  95. <div className={classes.list}>
  96. <List>
  97. {this.menuItems(transitionState)}
  98. </List>
  99. </div>
  100. );
  101. };
  102. render() {
  103. return (
  104. <Transition mountOnEnter in={this.props.opened} timeout={{enter: DURATION}}>
  105. {this.menu}
  106. </Transition>
  107. );
  108. }
  109. }
  110. export default withStyles(styles)(SideBar);