|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { withStyles } from 'material-ui/styles'; |
| 4 | +import classNames from 'classnames'; |
| 5 | +import Drawer from 'material-ui/Drawer'; |
| 6 | +import controllable from 'react-controllables'; |
| 7 | + |
| 8 | +import styles from './styles'; |
| 9 | + |
| 10 | +import NavBar from '../NavBar'; |
| 11 | +import Footer from '../Footer'; |
| 12 | + |
| 13 | +// TODO create Layout Library |
| 14 | + |
| 15 | +class Layout extends React.PureComponent { |
| 16 | + static propTypes = { |
| 17 | + title: PropTypes.string, |
| 18 | + classes: PropTypes.shape({}), |
| 19 | + children: PropTypes.element.isRequired, |
| 20 | + navbarPostion: PropTypes.string, |
| 21 | + stickyFooter: PropTypes.bool, |
| 22 | + footerContent: PropTypes.element, |
| 23 | + navBarContent: PropTypes.element, |
| 24 | + drawerOpen: PropTypes.bool.isRequired, |
| 25 | + onDrawerOpenChange: PropTypes.func, |
| 26 | + drawerContent: PropTypes.element, |
| 27 | + navBarProps: PropTypes.shape({}), |
| 28 | + drawerProps: PropTypes.shape({}), |
| 29 | + footerProps: PropTypes.shape({}), |
| 30 | + }; |
| 31 | + |
| 32 | + static defaultProps = { |
| 33 | + title: '', |
| 34 | + navbarPostion: 'default', |
| 35 | + stickyFooter: false, |
| 36 | + drawerOpen: false, |
| 37 | + }; |
| 38 | + |
| 39 | + handleDrawerClose = () => { |
| 40 | + if (!this.props.onDrawerOpenChange) return; |
| 41 | + |
| 42 | + this.props.onDrawerOpenChange(false); |
| 43 | + }; |
| 44 | + |
| 45 | + toggleDrawer = () => { |
| 46 | + if (!this.props.onDrawerOpenChange) return; |
| 47 | + |
| 48 | + this.props.onDrawerOpenChange(!this.props.drawerOpen); |
| 49 | + }; |
| 50 | + |
| 51 | + render() { |
| 52 | + const { |
| 53 | + title, |
| 54 | + logo, // TODO what kind of logo can we expect?..... |
| 55 | + classes, |
| 56 | + children, |
| 57 | + navbarPostion, |
| 58 | + stickyFooter, |
| 59 | + footerContent, |
| 60 | + navBarContent, |
| 61 | + drawerContent, |
| 62 | + drawerOpen, |
| 63 | + navBarProps, |
| 64 | + drawerProps, |
| 65 | + footerProps, |
| 66 | + } = this.props; |
| 67 | + |
| 68 | + const mainClassnames = classNames( |
| 69 | + classes.main, |
| 70 | + { [`${classes.mainFixedNavbar}`]: navbarPostion === 'fixed' }, |
| 71 | + { [`${classes.mainStickyFooter}`]: stickyFooter }, |
| 72 | + ); |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className={classes.layout}> |
| 76 | + <NavBar {...navBarProps} title={title} logo={logo} onIconClick={this.toggleDrawer}> |
| 77 | + {navBarContent} |
| 78 | + </NavBar> |
| 79 | + {drawerContent ? ( |
| 80 | + <Drawer {...drawerProps} open={drawerOpen} onRequestClose={this.handleDrawerClose}> |
| 81 | + {drawerContent} |
| 82 | + </Drawer> |
| 83 | + ) : null} |
| 84 | + <main className={mainClassnames}>{children}</main> |
| 85 | + {footerContent ? <Footer {...footerProps}>{footerContent}</Footer> : null} |
| 86 | + </div> |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export default controllable(withStyles(styles)(Layout), ['drawerOpen']); |
0 commit comments