All files / lib/Avatar index.js

86.36% Statements 38/44
90.28% Branches 65/72
75% Functions 9/12
86.05% Lines 37/43
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443                                  172x 172x           18x                                                                                       190x       190x   190x 173x 172x 172x 172x 172x 230x   172x     190x 6x 6x                             190x 173x                             190x 190x 3x                 187x   34x   34x 33x   34x                       35x     34x 153x 6x 147x 140x       190x 190x                                       190x                                 89x                                                                         89x                                                                                                                                                                                                                                                                                                                                                                                                                                
import React from 'react';
import PropTypes from 'prop-types';
import {
  Button,
  Loading,
  Icon,
} from '@collab-ui/react';
 
class Avatar extends React.Component {
  static displayName = 'Avatar';
 
  state = {
    isImageLoaded: false,
    isImageErrored: false
  };
 
  componentDidMount() {
    const img = this.image;
    Iif (img && img.complete) {
      this.handleImgLoaded();
    }
  }
 
  componentDidUpdate(prevProps) {
    Iif(prevProps.src !== this.props.src){
      this.handleImgChange();
    }
  }
 
  handleImgChange = () => {
    this.setState({
      isImageLoaded: false,
      isImageErrored: false
    });
  }
 
  handleImgError = () => {
    this.setState({
      isImageErrored: true
    });
  }
 
  handleImgLoaded = () => {
    this.setState({
      isImageLoaded: true
    });
  }
 
  render() {
    const {
      alt,
      backgroundColor,
      buttonClassName,
      className,
      color,
      failureBadge,
      hasNotification,
      hideDefaultTooltip,
      icon,
      isDecrypting,
      isOverview,
      onClick,
      size,
      src,
      theme,
      title,
      type,
      ...otherProps
    } = this.props;
    const {
      isImageLoaded,
      isImageErrored
    } = this.state;
 
    const getInitials = () => {
      if (!title.replace(/\s/g, '').length) return '';
      let letters = [];
      const words = title.trim().split(/ +/);
      const repeatTimes = Math.min(type === 'group' && 1 || 2, words.length);
      for (let i = 0; i < repeatTimes; i++) {
        letters.push(String.fromCodePoint(words[i].codePointAt(0)));
      }
      return letters.join('');
    };
 
    const getIcon = () => {
      Eif (icon.type.displayName === 'Icon') {
        return (
          <span
            className={
              'cui-avatar__icon' +
              `${isOverview ? ' cui-avatar__icon--overview' : ''}`
            }
            style={{ backgroundColor, color }}
          >
            {icon}
          </span>
        );
      }
      throw new Error('Icon prop should be a component of type Icon');
    };
 
    const getLetter = () => {
      return (
        <span
          key='letter'
          className={
            'cui-avatar__letter' +
            `${(isDecrypting && ` cui-decrypting`) || ''}` +
            `${(isImageLoaded && ` cui-avatar__img--hidden`) || ''}`
            }
          style={{ backgroundColor, color }}
        >
          {getInitials()}
        </span>
      );
    };
 
    const getChildren = () => {
      if(type === 'self') {
        return (
          <span
            key='self'
            className='cui-avatar__self'
            style={{ backgroundColor, color }}
          >
            <Icon name={size === 40 || size === 'medium' ? 'chat-active_18' : 'chat-active_16'} />
          </span>
        );
      } else if (src && !isImageErrored) {
      // image src is present and image has not yet errored
        const imgChildren = [];
         // image is not loaded and title is provided
        if (title && !isImageLoaded) {
          imgChildren.push(getLetter());
        }
        imgChildren.push(
          <img
            alt={alt}
            className={
              `cui-avatar__img` +
              `${(!isImageLoaded && ` cui-avatar__img--hidden`) || ''}`
            }
            draggable={false}
            key={`image-${imgChildren.length}`}
            onError={this.handleImgError}
            onLoad={this.handleImgLoaded}
            src={src}
            ref={ref => this.image = ref}
          />
        );
        return imgChildren;
      } else if (icon) {
        return getIcon();
      } else if (title) {
        return getLetter();
      }
    };
 
    const getAvatar = () => (
      <div
        className={
          'cui-avatar' +
          `${(onClick && ` cui-avatar--clickable`) || ''}` +
          `${(type && ` cui-avatar--${type}`) || ''}` +
          `${(size && ` cui-avatar--${size}`) || ''}` +
          `${(theme && ` cui-avatar--${theme}`) || ''}` +
          `${(isDecrypting && ` cui-decrypting`) || ''}` +
          `${(className && ` ${className}`) || ''}`
        }
        title={!hideDefaultTooltip ? title : ''}
        {...!onClick && {...otherProps}}
      >
        {getChildren()}
        {type === 'typing' && <Loading/>}
        {failureBadge && <span className='cui-avatar__failure-badge' />}
        {hasNotification && <span className='cui-avatar__notification-badge' />}
      </div>
    );
 
    return (
      onClick
      ?
      <Button
        className={buttonClassName}
        circle
        onClick={onClick}
        removeStyle
        {...otherProps}
      >
        {getAvatar()}
      </Button>
      : getAvatar()
    );
  }
}
 
Avatar.propTypes = {
  /** @prop Image alt tag | '' */
  alt: PropTypes.string,
  /** @prop Set Avatar background color | '' */
  backgroundColor: PropTypes.string,
  /** @prop Optional css class string for button | '' */
  buttonClassName: PropTypes.string,
  /** @prop Optional css class string for Avatar component | null */
  className: PropTypes.string,
  /** @prop Set Avatar text color | '' */
  color: PropTypes.string,
  /** @prop Set existance of a failureBadge on the Avatar | false */
  failureBadge: PropTypes.bool,
  /** @prop Set existance of a notification on the Avatar | false */
  hasNotification: PropTypes.bool,
  /** @prop Set the visibility of Avatar's default tooltip | false */
  hideDefaultTooltip: PropTypes.bool,
  /** @prop Optional icon component for the Avatar | null */
  icon: PropTypes.element,
  /** @prop Set if Avatar's content is decrypting | false */
  isDecrypting: PropTypes.bool,
  /** @prop Set existance of Avatar's Overview | false */
  isOverview: PropTypes.bool,
  /** @prop Handler to be called when the user taps the Avatar | null */
  onClick: PropTypes.func,
  /** @prop Set the size of the Avatar from one of the preconfigured options | 'medium' */
  size: PropTypes.oneOf(['xsmall', 'small', 'medium', 'large', 'xlarge', 18, 24, 28, 36, 40, 44, 52, 56, 72, 80, 84]),
  /** @prop Optional image source for the Avatar | null */
  src: PropTypes.string,
  /** @prop Optional Avatar color theme | null */
  theme: PropTypes.string,
  /** @prop set Avatar title / user's name | null */
  title: PropTypes.string,
  /** @prop optional Avatar type | '' */
  type: PropTypes.oneOf(['', 'active', 'inactive', 'dnd', 'ooo', 'group', 'typing', 'bot', 'self']),
};
 
Avatar.defaultProps = {
  alt: '',
  backgroundColor: '',
  buttonClassName: '',
  className: null,
  color: '',
  failureBadge: false,
  hasNotification: false,
  hideDefaultTooltip: false,
  icon: null,
  isDecrypting: false,
  isOverview: false,
  onClick: null,
  size: 'medium',
  src: null,
  theme: null,
  title: null,
  type: '',
};
 
export default Avatar;
 
/**
* @component avatar
* @section default
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarDefault extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith"/>
    );
  }
}
**/
 
/**
* @component avatar
* @section active
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarActive extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" type="active"/>
    );
  }
}
**/
 
/**
* @component avatar
* @section inactive
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarInactive extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" type="inactive" />
    );
  }
}
**/
 
/**
* @component avatar
* @section self
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarSelf extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" type="self"/>
    );
  }
}
**/
 
/**
* @component avatar
* @section status
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarStatus extends React.PureComponent {
  render() {
    return (
      <div className='row'>
        <div className="docs-example docs-example--spacing">
          <Avatar title="Tom Smith" size={36} type="dnd" />
        </div>
 
        <div className="docs-example docs-example--spacing">
          <Avatar title="Tom Smith" size={36} type="ooo" />
        </div>
      </div>
    );
  }
}
**/
 
/**
* @component avatar
* @section group
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarGroup extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" type="group"/>
    );
  }
}
**/
 
/**
* @component avatar
* @section bot
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarBot extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" type="bot" />
    );
  }
}
**/
 
/**
* @component avatar
* @section notification
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarNotification extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" hasNotification />
    );
  }
}
**/
 
/**
* @component avatar
* @section failure
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarFailure extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" failureBadge />
    );
  }
}
**/
 
/**
* @component avatar
* @section typing
* @react
 
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarTyping extends React.PureComponent {
  render() {
    return (
      <Avatar title="Tom Smith" type="typing"/>
    );
  }
}
**/
 
/**
* @component avatar
* @section composite
*
* @react
*
import { Avatar } from '@collab-ui/react';
 
 export default class AvatarComposite extends React.PureComponent {
  render() {
    return (
      <CompositeAvatar size={40}>
        <Avatar title="Tom Smith"/>
        <Avatar title="John William"/>
      </CompositeAvatar>
    );
  }
}
**/