github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/dashboard/assets/components/SideBar.jsx (about)

     1  // @flow
     2  
     3  
     4  import React, {Component} from 'react';
     5  
     6  import withStyles from 'material-ui/styles/withStyles';
     7  import List, {ListItem, ListItemIcon, ListItemText} from 'material-ui/List';
     8  import Icon from 'material-ui/Icon';
     9  import Transition from 'react-transition-group/Transition';
    10  import {Icon as FontAwesome} from 'react-fa';
    11  
    12  import {MENU, DURATION} from '../common';
    13  
    14  // styles contains the constant styles of the component.
    15  const styles = {
    16  	menu: {
    17  		default: {
    18  			transition: `margin-left ${DURATION}ms`,
    19  		},
    20  		transition: {
    21  			entered: {marginLeft: -200},
    22  		},
    23  	},
    24  };
    25  
    26  // themeStyles returns the styles generated from the theme for the component.
    27  const themeStyles = theme => ({
    28  	list: {
    29  		background: theme.palette.grey[900],
    30  	},
    31  	listItem: {
    32  		minWidth: theme.spacing.unit * 7,
    33  	},
    34  	icon: {
    35  		fontSize: theme.spacing.unit * 3,
    36  	},
    37  });
    38  
    39  export type Props = {
    40  	classes: Object, // injected by withStyles()
    41  	opened: boolean,
    42  	changeContent: string => void,
    43  };
    44  
    45  // SideBar renders the sidebar of the dashboard.
    46  class SideBar extends Component<Props> {
    47  	shouldComponentUpdate(nextProps) {
    48  		return nextProps.opened !== this.props.opened;
    49  	}
    50  
    51  	// clickOn returns a click event handler function for the given menu item.
    52  	clickOn = menu => (event) => {
    53  		event.preventDefault();
    54  		this.props.changeContent(menu);
    55  	};
    56  
    57  	// menuItems returns the menu items corresponding to the sidebar state.
    58  	menuItems = (transitionState) => {
    59  		const {classes} = this.props;
    60  		const children = [];
    61  		MENU.forEach((menu) => {
    62  			children.push((
    63  				<ListItem button key={menu.id} onClick={this.clickOn(menu.id)} className={classes.listItem}>
    64  					<ListItemIcon>
    65  						<Icon className={classes.icon}>
    66  							<FontAwesome name={menu.icon} />
    67  						</Icon>
    68  					</ListItemIcon>
    69  					<ListItemText
    70  						primary={menu.title}
    71  						style={{
    72  							...styles.menu.default,
    73  							...styles.menu.transition[transitionState],
    74  							padding: 0,
    75  						}}
    76  					/>
    77  				</ListItem>
    78  			));
    79  		});
    80  		return children;
    81  	};
    82  
    83  	// menu renders the list of the menu items.
    84  	menu = (transitionState: Object) => (
    85  		<div className={this.props.classes.list}>
    86  			<List>
    87  				{this.menuItems(transitionState)}
    88  			</List>
    89  		</div>
    90  	);
    91  
    92  	render() {
    93  		return (
    94  			<Transition mountOnEnter in={this.props.opened} timeout={{enter: DURATION}}>
    95  				{this.menu}
    96  			</Transition>
    97  		);
    98  	}
    99  }
   100  
   101  export default withStyles(themeStyles)(SideBar);