All files / src Button.js

93.33% Statements 14/15
75% Branches 6/8
100% Functions 2/2
93.33% Lines 14/15

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          1x                                 1x                       203x     203x   203x 2233x 1x   2233x       203x     203x                   203x     203x   203x                 203x              
import React, {Component, Children, createElement} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import Icon from './Icon'
 
const BOOL_PROPS_TO_CLASS_NAMES = {
  small: ['rev-Button--small'],
  large: ['rev-Button--large'],
 
  primary: ['rev-Button--primary'],
  secondary: ['rev-Button--secondary'],
  inverted: ['rev-Button--inverted'],
 
  success: ['rev-Button--success'],
  warning: ['rev-Button--warning'],
  alert: ['rev-Button--alert'],
  disabled: ['rev-Button--disabled'],
 
  expanded: ['rev-Button--expanded'],
 
  dropdown: ['rev-Button--dropdown'],
}
const BOOL_PROPS = Object.keys(BOOL_PROPS_TO_CLASS_NAMES)
 
export default class Button extends Component {
  static propTypes = {
    tag: PropTypes.string,
    icon: PropTypes.string,
    className: PropTypes.string,
    children: PropTypes.node,
  }
  /* eslint complexity: [2, 4] */
  render() {
    // Extract props that will not pass through.
    const {className, children, tag, icon, ...props} = this.props
 
    // Start building the className
    const boolClassNames = []
 
    BOOL_PROPS.forEach((name) => {
      if (props[name]) {
        boolClassNames.push(BOOL_PROPS_TO_CLASS_NAMES[name])
      }
      delete props[name]
    })
 
    // Of the props that WILL pass through, we need to use these.
    const {disabled, href} = props
 
    // Finish building the classNAme
    const buttonClassName = classNames(
      className,
      'rev-Button',
      boolClassNames,
      {
        disabled,
      }
    )
 
    // Modify underlying tag to suit props.
    const component = tag || (href ? 'a' : 'button')
 
    // Prepend icon if available
    let newChildren = children
 
    Iif (icon) {
      newChildren = [
        <Icon className="rev-Button-icon" key="icon" i={icon} />,
        ' ',
        ...Children.toArray(children),
      ]
    }
 
    // Finish
    return createElement(
      component,
      {...props, className: buttonClassName},
      newChildren
    )
  }
}