All files / src Drawer.js

100% Statements 13/13
100% Branches 0/0
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81            1x                   1x                                           4x 4x           1x       1x       6x 6x 42x   42x 42x   6x   6x                                    
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import StatelessDrawer from './StatelessDrawer'
import Expander from './Expander'
 
const BOOL_PROPS_TO_CLASS_NAMES = {
  left: 'rev-Drawer--left',
  right: 'rev-Drawer--right',
  top: 'rev-Drawer--top',
  scroll: 'rev-Drawer--scroll',
  fixed: 'rev-Drawer--fixed',
  overlay: 'rev-Drawer--overlay',
  collapsible: 'rev-Drawer--collapsible',
}
 
const BOOL_PROPS = Object.keys(BOOL_PROPS_TO_CLASS_NAMES)
 
export default class Drawer extends Component {
  static propTypes = {
    open: PropTypes.bool,
    children: PropTypes.node,
    className: PropTypes.string,
    closerChildren: PropTypes.node,
    expanderChildren: PropTypes.node,
    expanderClassName: PropTypes.string,
    expanderComponentClass: PropTypes.string,
  }
 
  static defaultProps = {
    expanderComponentClass: 'a',
    expanderClassName: '',
    expanderChildren: 'Expand this',
    closerChildren: 'Close This',
    open: false,
  }
 
  constructor(props) {
    super(props)
    this.state = {
      open: props.open,
    }
  }
 
  expandDrawer = () => {
    this.setState({open: true})
  }
 
  closeDrawer = () => {
    this.setState({open: false})
  }
 
  render() {
    const {open} = this.state
    const propClassNames = BOOL_PROPS.reduce((acc, key) => {
      const value = BOOL_PROPS_TO_CLASS_NAMES[key]
 
      acc[value] = this.props[key]
      return acc
    }, {})
    const newClassName = classNames(this.props.className, propClassNames)
 
    return (
      <StatelessDrawer
        open={open}
        className={newClassName}
        close={this.closeDrawer}
        expand={this.expandDrawer}
        expanderComponentClass={this.props.expanderComponentClass}
        expanderClassName={this.props.expanderClassName}
        expanderChildren={this.props.expanderChildren}
        closerChildren={this.props.closerChildren}
      >
        {this.props.children}
      </StatelessDrawer>
    )
  }
}
 
export {Drawer, Expander}