All files / src Badge.js

100% Statements 13/13
75% Branches 3/4
100% Functions 3/3
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          1x                 1x 1x           7x           3x   3x   3x 21x 1x 1x       3x   3x              
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import Icon from './Icon'
 
const PROP_NAME_TO_CLASS_NAME = {
  primary: ['rev-Badge--primary'],
  secondary: ['rev-Badge--secondary'],
  tertiary: ['rev-Badge--tertiary'],
  accent: ['rev-Badge--accent'],
  alert: ['rev-Badge--alert'],
  warning: ['rev-Badge--warning'],
  success: ['rev-Badge--success'],
}
const PROP_NAMES = Object.keys(PROP_NAME_TO_CLASS_NAME)
const PROP_TYPES = {
  icon: PropTypes.string,
  className: PropTypes.string,
  children: PropTypes.node,
}
 
PROP_NAMES.forEach((key) => (PROP_TYPES[key] = PropTypes.bool))
 
export default class Badge extends Component {
  static propTypes = PROP_TYPES
 
  render() {
    const {className, children, icon, ...props} = this.props
 
    const propClassNames = []
 
    PROP_NAMES.forEach((name) => {
      if (props[name]) {
        propClassNames.push(PROP_NAME_TO_CLASS_NAME[name])
        delete props[name]
      }
    })
 
    const newClassName = classNames(className, 'rev-Badge', propClassNames)
 
    return (
      <span {...props} className={newClassName}>
        {icon ? <Icon i={icon} className="rev-Badge-icon" /> : null} {children}
      </span>
    )
  }
}