All files / atom/input/src/Input/Wrappers/Addons InputAddons.js

100% Statements 5/5
100% Branches 12/12
100% Functions 1/1
100% Lines 5/5

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            1x 47x 40x   7x                             1x                            
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import {INPUT_SHAPES, SIZES} from '../../../config.js'
import {ADDON_TYPES, BASE_CLASS_ADDON_WRAPPER, getClassName} from './config.js'
 
const InputAddon = ({leftAddon, rightAddon, shape, size, children}) => {
  if (!(leftAddon || rightAddon)) {
    return children
  }
  return (
    <div
      className={cx(
        BASE_CLASS_ADDON_WRAPPER,
        shape && `${BASE_CLASS_ADDON_WRAPPER}-shape-${shape}`,
        size && `${BASE_CLASS_ADDON_WRAPPER}-size-${size}`
      )}
    >
      {leftAddon && <span className={getClassName({type: ADDON_TYPES.LEFT, shape})}>{leftAddon}</span>}
      {children}
      {rightAddon && <span className={getClassName({type: ADDON_TYPES.RIGHT, shape})}>{rightAddon}</span>}
    </div>
  )
}
 
InputAddon.propTypes = {
  /* inner react node element */
  children: PropTypes.node,
  /* Left addon component, text,... */
  leftAddon: PropTypes.any,
  /* Right addon component, text,... */
  rightAddon: PropTypes.any,
  /** Sets the shape of the input field. It can be 'rounded', 'square' or 'circle' */
  shape: PropTypes.oneOf(Object.values(INPUT_SHAPES)),
  /* 'Sets the size of the inputAddon */
  size: PropTypes.oneOf(Object.values(SIZES))
}
 
export default InputAddon