All files / molecule/avatar/src/AvatarFallbackName index.js

100% Statements 8/8
75% Branches 3/4
100% Functions 1/1
100% Lines 8/8

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             2x 2x 2x   2x         2x             1x 1x                  
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import useBackgroundColor from '../useBackgroundColor.js'
import {BASE_CLASS_NAME} from './settings.js'
 
const MoleculeAvatarFallbackName = ({
  name: nameProp,
  size,
  className: classNameProp,
  backgroundColor: backgroundColorProp,
  ...others
}) => {
  const className = cx(BASE_CLASS_NAME, classNameProp, `${BASE_CLASS_NAME}--${size}`)
  const [firstName, lastName] = nameProp.split(' ')
  const name = firstName && lastName ? `${firstName.charAt(0)}${lastName.charAt(0)}` : firstName.charAt(0)
 
  const backgroundColor = useBackgroundColor({
    name: nameProp,
    backgroundColor: backgroundColorProp
  })
 
  return (
    <div className={className} aria-label={nameProp} style={{backgroundColor}} {...others}>
      {name}
    </div>
  )
}
 
MoleculeAvatarFallbackName.displayName = 'MoleculeAvatarFallbackName'
MoleculeAvatarFallbackName.propTypes = {
  className: PropTypes.string,
  backgroundColor: PropTypes.string,
  name: PropTypes.string,
  src: PropTypes.string,
  size: PropTypes.string
}
 
export default MoleculeAvatarFallbackName