All files / atom/checkbox/src CheckboxIcon.js

72.72% Statements 8/11
66.66% Branches 4/6
50% Functions 1/2
70% Lines 7/10

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            1x                       64x 64x 28x           28x                                                   1x 1x                          
import {useRef} from 'react'
 
import PropTypes from 'prop-types'
 
import {CHECKBOX_SIZES, CHECKBOX_STATUS, className, pressedValue} from './config.js'
 
const CheckboxIcon = ({
  disabled,
  size,
  status,
  onClick,
  icon: SVGIcon,
  checked,
  className: classNameProp,
  indeterminate,
  isNative,
  ...props
}) => {
  const buttonRef = useRef()
  if (!SVGIcon) return null
  const handleKeydown = event => {
    if (event.key === 'Enter') {
      event.preventDefault()
      event.stopPropagation()
    }
  }
  return (
    <button
      ref={buttonRef}
      type="button"
      className={className({
        size,
        checked,
        disabled,
        indeterminate,
        className: classNameProp
      })}
      onClick={onClick(buttonRef)}
      onKeyDown={handleKeydown}
      disabled={disabled}
      {...(Object.values(CHECKBOX_STATUS).includes(status) && {
        'data-status': status
      })}
      aria-pressed={pressedValue({checked, indeterminate})}
      aria-hidden={isNative}
      {...props}
    >
      <SVGIcon size={size} />
    </button>
  )
}
 
CheckboxIcon.displayName = 'CheckboxIcon'
CheckboxIcon.propTypes = {
  disabled: PropTypes.bool,
  size: PropTypes.oneOf(Object.values(CHECKBOX_SIZES)),
  status: PropTypes.oneOf(Object.values(CHECKBOX_STATUS)),
  onClick: PropTypes.func,
  icon: PropTypes.elementType,
  checked: PropTypes.bool,
  indeterminate: PropTypes.bool,
  isNative: PropTypes.bool,
  className: PropTypes.string
}
 
export default CheckboxIcon