2020-07-13 12:45:37 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2020-07-13 12:45:37 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import Popover from '../popover';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
|
|
|
import Avatar from './avatar';
|
2023-07-27 20:04:32 +08:00
|
|
|
import AvatarContext from './AvatarContext';
|
|
|
|
import type { AvatarContextType, AvatarSize } from './AvatarContext';
|
2022-06-22 15:18:03 +08:00
|
|
|
import useStyle from './style';
|
2023-11-22 20:53:48 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2020-07-13 12:45:37 +08:00
|
|
|
|
2023-07-27 20:04:32 +08:00
|
|
|
interface ContextProps {
|
|
|
|
children?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AvatarContextProvider: React.FC<AvatarContextType & ContextProps> = (props) => {
|
|
|
|
const { size, shape } = React.useContext<AvatarContextType>(AvatarContext);
|
|
|
|
const avatarContextValue = React.useMemo<AvatarContextType>(
|
|
|
|
() => ({ size: props.size || size, shape: props.shape || shape }),
|
|
|
|
[props.size, props.shape, size, shape],
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<AvatarContext.Provider value={avatarContextValue}>{props.children}</AvatarContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
export interface GroupProps {
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2020-07-13 12:45:37 +08:00
|
|
|
children?: React.ReactNode;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
|
|
|
maxCount?: number;
|
|
|
|
maxStyle?: React.CSSProperties;
|
|
|
|
maxPopoverPlacement?: 'top' | 'bottom';
|
2021-09-17 09:52:51 +08:00
|
|
|
maxPopoverTrigger?: 'hover' | 'focus' | 'click';
|
2020-10-24 22:00:43 +08:00
|
|
|
/*
|
|
|
|
* Size of avatar, options: `large`, `small`, `default`
|
|
|
|
* or a custom number size
|
|
|
|
* */
|
|
|
|
size?: AvatarSize;
|
2023-07-27 20:04:32 +08:00
|
|
|
shape?: 'circle' | 'square';
|
2020-07-13 12:45:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const Group: React.FC<GroupProps> = (props) => {
|
2022-04-07 21:50:35 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2023-01-20 11:03:50 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
2023-07-27 20:04:32 +08:00
|
|
|
style,
|
2023-01-20 11:03:50 +08:00
|
|
|
maxCount,
|
|
|
|
maxStyle,
|
|
|
|
size,
|
2023-07-27 20:04:32 +08:00
|
|
|
shape,
|
|
|
|
maxPopoverPlacement = 'top',
|
|
|
|
maxPopoverTrigger = 'hover',
|
|
|
|
children,
|
2023-01-20 11:03:50 +08:00
|
|
|
} = props;
|
2020-10-24 22:00:43 +08:00
|
|
|
|
2022-03-29 13:56:19 +08:00
|
|
|
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
|
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2023-12-14 14:58:53 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2020-10-24 22:00:43 +08:00
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
const cls = classNames(
|
2022-03-29 13:56:19 +08:00
|
|
|
groupPrefixCls,
|
2020-07-13 12:45:37 +08:00
|
|
|
{
|
2022-03-29 13:56:19 +08:00
|
|
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
|
2020-07-13 12:45:37 +08:00
|
|
|
},
|
2023-11-22 20:53:48 +08:00
|
|
|
cssVarCls,
|
2023-12-14 14:58:53 +08:00
|
|
|
rootCls,
|
2020-07-13 12:45:37 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-29 13:56:19 +08:00
|
|
|
hashId,
|
2020-07-13 12:45:37 +08:00
|
|
|
);
|
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
const childrenWithProps = toArray(children).map((child, index) =>
|
2023-07-27 20:04:32 +08:00
|
|
|
cloneElement(child, { 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}
|
2021-09-17 09:52:51 +08:00
|
|
|
trigger={maxPopoverTrigger}
|
2020-07-13 12:45:37 +08:00
|
|
|
placement={maxPopoverPlacement}
|
2022-03-29 13:56:19 +08:00
|
|
|
overlayClassName={`${groupPrefixCls}-popover`}
|
2024-02-20 18:07:07 +08:00
|
|
|
destroyTooltipOnHide
|
2020-07-13 12:45:37 +08:00
|
|
|
>
|
2020-11-05 16:22:21 +08:00
|
|
|
<Avatar style={maxStyle}>{`+${numOfChildren - maxCount}`}</Avatar>
|
2020-07-13 12:45:37 +08:00
|
|
|
</Popover>,
|
|
|
|
);
|
2023-11-22 20:53:48 +08:00
|
|
|
return wrapCSSVar(
|
2023-07-27 20:04:32 +08:00
|
|
|
<AvatarContextProvider shape={shape} size={size}>
|
|
|
|
<div className={cls} style={style}>
|
2020-11-05 16:22:21 +08:00
|
|
|
{childrenShow}
|
|
|
|
</div>
|
2023-07-27 20:04:32 +08:00
|
|
|
</AvatarContextProvider>,
|
2020-07-13 12:45:37 +08:00
|
|
|
);
|
|
|
|
}
|
2020-11-05 16:22:21 +08:00
|
|
|
|
2023-11-22 20:53:48 +08:00
|
|
|
return wrapCSSVar(
|
2023-07-27 20:04:32 +08:00
|
|
|
<AvatarContextProvider shape={shape} size={size}>
|
|
|
|
<div className={cls} style={style}>
|
2020-11-05 16:22:21 +08:00
|
|
|
{childrenWithProps}
|
|
|
|
</div>
|
2023-07-27 20:04:32 +08:00
|
|
|
</AvatarContextProvider>,
|
2020-07-13 12:45:37 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Group;
|