# All files / react/Avatar index.jsx

92.31% Statements 12/13
86.67% Branches 13/15
100% Functions 4/4
92.31% Lines 12/13

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

    1x
    9x

    9x
    6x
    6x
    9x

    1x
    9x

    9x

    9x

    1x

    1x

     import React from 'react'
    import classNames from 'classnames'
    import PropTypes from 'prop-types'
    import palette from '../../stylus/settings/palette.json'
    import styles from './styles.styl'
    import Icon from '../Icon'

    const nameToColor = (name = '') => {
      const colors = [
        palette['azure'],
        palette['melon'],
        palette['blazeOrange'],
        palette['pomegranate'],
        palette['mango'],
        palette['pumpkinOrange'],
        palette['darkPeriwinkle'],
        palette['purpley'],
        palette['lightishPurple'],
        palette['weirdGreen'],
        palette['puertoRico'],
        palette['emerald']
      ]
      const key =
        Array.from(name.toUpperCase())
          .map(letter => letter.charCodeAt(0))
          .reduce((sum, number) => sum + number, 0) % colors.length
      return colors[key]
    }

    export const Avatar = ({ text, textId, image, size, className, style }) => {
      const colored = {
        backgroundColor: `${nameToColor(textId || text)}`,
        color: 'white'
      }

      Iif (style) {
        Object.assign(colored, style)
      }

      return (
        <div
          className={classNames(
            styles['c-avatar'],
            {
              [styles[`c-avatar--${size}`]]: size
            },
            className
          )}
          style={text ? colored : style}
        >
          {image && <img src={image} className={styles['c-avatar-image']} alt="" />}
          {!image &&
            text && <span className={styles['c-avatar-initials']}>{text}</span>}
          {!image && !text && <Icon icon="people" />}
        </div>
      )
    }

    Avatar.propTypes = {
      text: PropTypes.string,
      image: PropTypes.string,
      size: PropTypes.oneOf(['small', 'medium']),
      className: PropTypes.string
    }

    Avatar.defaultProps = {
      text: '',
      image: '',
      size: 'medium',
      className: ''
    }

    export default Avatar