All files / src StatelessDrawer.js

100% Statements 12/12
50% Branches 2/4
100% Functions 4/4
100% Lines 12/12

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 82 83 84 85 86          1x                   1x                                             2x 2x         2x 2x         12x 84x   84x 84x   12x   12x                                                      
import React from 'react'
import classNames from 'classnames'
import PropTypes from 'prop-types'
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 StatelessDrawer extends React.Component {
  static propTypes = {
    open: PropTypes.bool,
    close: PropTypes.func,
    expand: PropTypes.func,
    children: PropTypes.node,
    className: PropTypes.string,
    closerChildren: PropTypes.node,
    expanderChildren: PropTypes.node,
    expanderComponentClass: PropTypes.string,
  }
 
  static defaultProps = {
    expanderComponentClass: 'a',
    expanderClassName: '',
    expanderChildren: 'Expand this',
    closerChildren: 'Close This',
    open: false,
  }
 
  expand = () => {
    Eif (this.props.expand) {
      this.props.expand()
    }
  }
 
  close = () => {
    Eif (this.props.close) {
      this.props.close()
    }
  }
 
  render() {
    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 (
      <Expander
        open={this.props.open}
        className={newClassName}
        closer={
          /* eslint-disable jsx-a11y/anchor-is-valid, jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
          <a className="rev-Drawer-closer" onClick={this.close}>
            {this.props.closerChildren}
          </a>
          /* eslint-enable jsx-a11y/anchor-is-valid, jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
        }
      >
        {React.createElement(
          this.props.expanderComponentClass,
          {
            className: 'rev-Drawer-expander',
            onClick: this.expand,
          },
          this.props.expanderChildren
        )}
        {this.props.children}
      </Expander>
    )
  }
}
 
export {Expander}