ant-design/components/avatar/group.tsx

151 lines
4.6 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2020-07-13 12:45:37 +08:00
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import { cloneElement } from '../_util/reactNode';
import { devUseWarning } from '../_util/warning';
2020-07-13 12:45:37 +08:00
import { ConfigContext } from '../config-provider';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import type { PopoverProps } from '../popover';
2020-07-13 12:45:37 +08:00
import Popover from '../popover';
2022-06-22 14:57:09 +08:00
import Avatar from './avatar';
import AvatarContext from './AvatarContext';
import type { AvatarContextType, AvatarSize } from './AvatarContext';
2022-06-22 15:18:03 +08:00
import useStyle from './style';
2020-07-13 12:45:37 +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;
rootClassName?: string;
2020-07-13 12:45:37 +08:00
children?: React.ReactNode;
style?: React.CSSProperties;
prefixCls?: string;
/** @deprecated Please use `max={{ count: number }}` */
2020-07-13 12:45:37 +08:00
maxCount?: number;
/** @deprecated Please use `max={{ style: CSSProperties }}` */
2020-07-13 12:45:37 +08:00
maxStyle?: React.CSSProperties;
/** @deprecated Please use `max={{ popover: PopoverProps }}` */
2020-07-13 12:45:37 +08:00
maxPopoverPlacement?: 'top' | 'bottom';
/** @deprecated Please use `max={{ popover: PopoverProps }}` */
maxPopoverTrigger?: 'hover' | 'focus' | 'click';
max?: {
count?: number;
style?: React.CSSProperties;
popover?: PopoverProps;
};
/*
* Size of avatar, options: `large`, `small`, `default`
* or a custom number size
* */
size?: AvatarSize;
shape?: 'circle' | 'square';
2020-07-13 12:45:37 +08:00
}
const Group: React.FC<GroupProps> = (props) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const {
prefixCls: customizePrefixCls,
className,
rootClassName,
style,
maxCount,
maxStyle,
size,
shape,
maxPopoverPlacement,
maxPopoverTrigger,
children,
max,
} = props;
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Avatar.Group');
warning.deprecated(!maxCount, 'maxCount', 'max={{ count: number }}');
warning.deprecated(!maxStyle, 'maxStyle', 'max={{ style: CSSProperties }}');
warning.deprecated(
!maxPopoverPlacement,
'maxPopoverPlacement',
'max={{ popover: PopoverProps }}',
);
warning.deprecated(!maxPopoverTrigger, 'maxPopoverTrigger', 'max={{ popover: PopoverProps }}');
}
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
const rootCls = useCSSVarCls(prefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
2020-07-13 12:45:37 +08:00
const cls = classNames(
groupPrefixCls,
2020-07-13 12:45:37 +08:00
{
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
2020-07-13 12:45:37 +08:00
},
cssVarCls,
rootCls,
2020-07-13 12:45:37 +08:00
className,
rootClassName,
hashId,
2020-07-13 12:45:37 +08:00
);
const childrenWithProps = toArray(children).map((child, index) =>
cloneElement(child, { key: `avatar-key-${index}` }),
);
const mergeCount = max?.count || maxCount;
2020-07-13 12:45:37 +08:00
const numOfChildren = childrenWithProps.length;
if (mergeCount && mergeCount < numOfChildren) {
const childrenShow = childrenWithProps.slice(0, mergeCount);
const childrenHidden = childrenWithProps.slice(mergeCount, numOfChildren);
const mergeStyle = max?.style || maxStyle;
const mergePopoverTrigger = max?.popover?.trigger || maxPopoverTrigger || 'hover';
const mergePopoverPlacement = max?.popover?.placement || maxPopoverPlacement || 'top';
const mergeProps = {
content: childrenHidden,
...max?.popover,
overlayClassName: classNames(`${groupPrefixCls}-popover`, max?.popover?.overlayClassName),
placement: mergePopoverPlacement,
trigger: mergePopoverTrigger,
};
2020-07-13 12:45:37 +08:00
childrenShow.push(
<Popover key="avatar-popover-key" destroyTooltipOnHide {...mergeProps}>
<Avatar style={mergeStyle}>{`+${numOfChildren - mergeCount}`}</Avatar>
2020-07-13 12:45:37 +08:00
</Popover>,
);
return wrapCSSVar(
<AvatarContextProvider shape={shape} size={size}>
<div className={cls} style={style}>
{childrenShow}
</div>
</AvatarContextProvider>,
2020-07-13 12:45:37 +08:00
);
}
return wrapCSSVar(
<AvatarContextProvider shape={shape} size={size}>
<div className={cls} style={style}>
{childrenWithProps}
</div>
</AvatarContextProvider>,
2020-07-13 12:45:37 +08:00
);
};
export default Group;