All files / lib/CompositeAvatar index.js

100% Statements 13/13
83.33% Branches 10/12
100% Functions 4/4
100% Lines 12/12
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      89x 12x 12x 22x     12x 12x 10x   2x     12x                         89x   89x                 89x              
import React from 'react';
import PropTypes from 'prop-types';
 
const CompositeAvatar = props => {
  const { children, size, className } = props;
  const isAvatar = () => {
    return children.reduce((prev, child) => prev && child.type.displayName === 'Avatar', true);
  };
 
  const getChildren = () => {
    if (children.length === 2 && isAvatar()) {
      return children;
    }
    throw new Error('Children should have 2 Avatar component');
  };
 
  return (
    <div
      className={
        'cui-composite-avatar' +
        `${(size && ` cui-composite-avatar--${size}`) || ''}` +
        `${(className && ` ${className}`) || ''}`
      }
    >
      {getChildren()}
    </div>
  );
};
 
CompositeAvatar.displayName = 'CompositeAvatar';
 
CompositeAvatar.propTypes = {
  /** @prop Children nodes to render inside CompositeAvatar | null */
  children: PropTypes.node,
  /** @prop Optional css class string | '' */
  className: PropTypes.string,
  /** @prop Sets the size of the CompositeAvatar | 'medium' */
  size: PropTypes.oneOf(['small', 'medium', 'large', 28, 40, 84, 135]),
};
 
CompositeAvatar.defaultProps = {
  className: '',
  size: 'medium',
  children: null,
};
 
export default CompositeAvatar;