2020-07-13 12:45:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2020-08-09 18:54:53 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2020-07-13 12:45:37 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-11-05 16:22:21 +08:00
|
|
|
import Avatar from './avatar';
|
2020-07-13 12:45:37 +08:00
|
|
|
import Popover from '../popover';
|
2020-11-05 16:22:21 +08:00
|
|
|
import { AvatarSize, SizeContextProvider } from './SizeContext';
|
2020-07-13 12:45:37 +08:00
|
|
|
|
|
|
|
export interface GroupProps {
|
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
|
|
|
maxCount?: number;
|
|
|
|
maxStyle?: React.CSSProperties;
|
|
|
|
maxPopoverPlacement?: 'top' | 'bottom';
|
2020-10-24 22:00:43 +08:00
|
|
|
/*
|
|
|
|
* Size of avatar, options: `large`, `small`, `default`
|
|
|
|
* or a custom number size
|
|
|
|
* */
|
|
|
|
size?: AvatarSize;
|
2020-07-13 12:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Group: React.FC<GroupProps> = props => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-10-24 22:00:43 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className = '', maxCount, maxStyle, size } = props;
|
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
const prefixCls = getPrefixCls('avatar-group', customizePrefixCls);
|
2020-10-24 22:00:43 +08:00
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
const cls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
|
|
|
const { children, maxPopoverPlacement = 'top' } = props;
|
2020-12-09 17:12:32 +08:00
|
|
|
const childrenWithProps = toArray(children).map((child, index) =>
|
|
|
|
cloneElement(child, {
|
2020-08-09 18:54:53 +08:00
|
|
|
key: `avatar-key-${index}`,
|
2020-12-09 17:12:32 +08:00
|
|
|
}),
|
|
|
|
);
|
2020-08-09 18:54:53 +08:00
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
const numOfChildren = childrenWithProps.length;
|
|
|
|
if (maxCount && maxCount < numOfChildren) {
|
|
|
|
const childrenShow = childrenWithProps.slice(0, maxCount);
|
|
|
|
const childrenHidden = childrenWithProps.slice(maxCount, numOfChildren);
|
|
|
|
childrenShow.push(
|
|
|
|
<Popover
|
2020-08-09 18:54:53 +08:00
|
|
|
key="avatar-popover-key"
|
2020-07-13 12:45:37 +08:00
|
|
|
content={childrenHidden}
|
|
|
|
trigger="hover"
|
|
|
|
placement={maxPopoverPlacement}
|
|
|
|
overlayClassName={`${prefixCls}-popover`}
|
|
|
|
>
|
2020-11-05 16:22:21 +08:00
|
|
|
<Avatar style={maxStyle}>{`+${numOfChildren - maxCount}`}</Avatar>
|
2020-07-13 12:45:37 +08:00
|
|
|
</Popover>,
|
|
|
|
);
|
|
|
|
return (
|
2020-11-05 16:22:21 +08:00
|
|
|
<SizeContextProvider size={size}>
|
|
|
|
<div className={cls} style={props.style}>
|
|
|
|
{childrenShow}
|
|
|
|
</div>
|
|
|
|
</SizeContextProvider>
|
2020-07-13 12:45:37 +08:00
|
|
|
);
|
|
|
|
}
|
2020-11-05 16:22:21 +08:00
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
return (
|
2020-11-05 16:22:21 +08:00
|
|
|
<SizeContextProvider size={size}>
|
|
|
|
<div className={cls} style={props.style}>
|
|
|
|
{childrenWithProps}
|
|
|
|
</div>
|
|
|
|
</SizeContextProvider>
|
2020-07-13 12:45:37 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Group;
|