import * as React from 'react'; import classNames from 'classnames'; import toArray from 'rc-util/lib/Children/toArray'; import { cloneElement } from '../_util/reactNode'; import { ConfigContext } from '../config-provider'; import Avatar from './avatar'; import Popover from '../popover'; import useStyle from './style'; import type { AvatarSize } from './SizeContext'; import { SizeContextProvider } from './SizeContext'; export interface GroupProps { className?: string; children?: React.ReactNode; style?: React.CSSProperties; prefixCls?: string; maxCount?: number; maxStyle?: React.CSSProperties; maxPopoverPlacement?: 'top' | 'bottom'; maxPopoverTrigger?: 'hover' | 'focus' | 'click'; /* * Size of avatar, options: `large`, `small`, `default` * or a custom number size * */ size?: AvatarSize; } const Group: React.FC = props => { const { getPrefixCls, direction } = React.useContext(ConfigContext); const { prefixCls: customizePrefixCls, className = '', maxCount, maxStyle, size } = props; const prefixCls = getPrefixCls('avatar', customizePrefixCls); const groupPrefixCls = `${prefixCls}-group`; const [wrapSSR, hashId] = useStyle(prefixCls); const cls = classNames( groupPrefixCls, { [`${groupPrefixCls}-rtl`]: direction === 'rtl', }, className, hashId, ); const { children, maxPopoverPlacement = 'top', maxPopoverTrigger = 'hover' } = props; const childrenWithProps = toArray(children).map((child, index) => cloneElement(child, { key: `avatar-key-${index}`, }), ); const numOfChildren = childrenWithProps.length; if (maxCount && maxCount < numOfChildren) { const childrenShow = childrenWithProps.slice(0, maxCount); const childrenHidden = childrenWithProps.slice(maxCount, numOfChildren); childrenShow.push( {`+${numOfChildren - maxCount}`} , ); return wrapSSR(
{childrenShow}
, ); } return wrapSSR(
{childrenWithProps}
, ); }; export default Group;