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