index.js 1.53 KB
Newer Older
1 2 3 4 5 6
import React from 'react';
import { Tooltip, Avatar } from 'antd';
import classNames from 'classnames';

import styles from './index.less';

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
const avatarSizeToClassName = size =>
  classNames(styles.avatarItem, {
    [styles.avatarItemLarge]: size === 'large',
    [styles.avatarItemSmall]: size === 'small',
    [styles.avatarItemMini]: size === 'mini',
  });

const AvatarList = ({ children, size, maxLength, excessItemsStyle, ...other }) => {
  const numOfChildren = React.Children.count(children);
  const numToShow = maxLength >= numOfChildren ? numOfChildren : maxLength;

  const childrenWithProps = React.Children.toArray(children)
    .slice(0, numToShow)
    .map(child =>
      React.cloneElement(child, {
        size,
      })
    );

  if (numToShow < numOfChildren) {
    const cls = avatarSizeToClassName(size);

    childrenWithProps.push(
      <li key="exceed" className={cls}>
        <Avatar size={size} style={excessItemsStyle}>{`+${numOfChildren - maxLength}`}</Avatar>
      </li>
    );
  }
35 36 37 38 39 40 41 42

  return (
    <div {...other} className={styles.avatarList}>
      <ul> {childrenWithProps} </ul>
    </div>
  );
};

jim's avatar
jim committed
43
const Item = ({ src, size, tips, onClick = () => {} }) => {
44
  const cls = avatarSizeToClassName(size);
45 46

  return (
jim's avatar
jim committed
47 48 49 50 51 52 53 54
    <li className={cls} onClick={onClick}>
      {tips ? (
        <Tooltip title={tips}>
          <Avatar src={src} size={size} style={{ cursor: 'pointer' }} />
        </Tooltip>
      ) : (
        <Avatar src={src} size={size} />
      )}
55 56 57 58 59 60 61
    </li>
  );
};

AvatarList.Item = Item;

export default AvatarList;