All files / atom/tag/src Standard.js

31.25% Statements 5/16
70.96% Branches 22/31
33.33% Functions 1/3
33.33% Lines 5/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 83 84 85 86 87 88 89 90 91 92 93 94                    1x                                   9x 9x                               9x                                                                 1x                              
import {forwardRef} from 'react'
 
import cx from 'classnames'
import {emulateTab} from 'emulate-tab'
import PropTypes from 'prop-types'
 
import PrimitiveInjector from '@s-ui/react-primitive-injector'
 
import {onHandler} from './constants.js'
 
const StandardTag = forwardRef(
  (
    {
      className,
      as: As,
      closeIcon,
      icon,
      label,
      onClose = () => {},
      closeLabel,
      value,
      readOnly,
      disabled,
      title,
      ...props
    },
    forwardedRef
  ) => {
    const Component = As === undefined ? 'span' : As
    const handleKeyDown = event => {
      if (disabled || readOnly) return
      switch (event.key) {
        case 'Delete':
          onClose()
          emulateTab()
          emulateTab.backwards()
          break
        case 'Backspace':
          emulateTab.backwards()
          setTimeout(onClose(), 0)
          break
        default:
          break
      }
    }
    return (
      <PrimitiveInjector
        className={cx(className, closeIcon !== undefined && !(disabled || readOnly) && 'sui-AtomTag-hasClose')}
        {...(disabled && {'data-disabled': disabled})}
        {...(readOnly && !disabled && {'data-read-only': readOnly})}
      >
        <Component ref={forwardedRef} {...props}>
          {icon && <span className="sui-AtomTag-icon">{icon}</span>}
          {label ? (
            <span className="sui-AtomTag-label" title={title || label}>
              {label}
            </span>
          ) : null}
          {closeIcon && !(disabled || readOnly) && (
            <button
              className="sui-AtomTag-closeable"
              onClick={onHandler({disabled, readOnly}, onClose, {
                value,
                label
              })}
              onKeyDown={handleKeyDown}
              title={closeLabel}
              tabIndex={0}
            >
              <span className="sui-AtomTag-closeableIcon sui-AtomTag-secondary-icon">{closeIcon}</span>
            </button>
          )}
        </Component>
      </PrimitiveInjector>
    )
  }
)
 
StandardTag.propTypes = {
  readOnly: PropTypes.bool,
  disabled: PropTypes.bool,
  onClose: PropTypes.func,
  closeIcon: PropTypes.node,
  icon: PropTypes.node,
  label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
  className: PropTypes.string,
  title: PropTypes.string,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  as: PropTypes.elementType,
  closeLabel: PropTypes.string
}
 
export default StandardTag