File size: 4,810 Bytes
1e92f2d |
1 2 3 4 5 6 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import * as React from 'react';
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import { cloneElement } from '../_util/reactNode';
import { devUseWarning } from '../_util/warning';
import { ConfigContext } from '../config-provider';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import type { PopoverProps } from '../popover';
import Popover from '../popover';
import Avatar from './Avatar';
import type { AvatarContextType, AvatarSize } from './AvatarContext';
import AvatarContext from './AvatarContext';
import useStyle from './style';
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>
);
};
export interface AvatarGroupProps {
className?: string;
rootClassName?: string;
children?: React.ReactNode;
style?: React.CSSProperties;
prefixCls?: string;
/** @deprecated Please use `max={{ count: number }}` */
maxCount?: number;
/** @deprecated Please use `max={{ style: CSSProperties }}` */
maxStyle?: React.CSSProperties;
/** @deprecated Please use `max={{ popover: PopoverProps }}` */
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';
}
const AvatarGroup: React.FC<AvatarGroupProps> = (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');
[
['maxCount', 'max={{ count: number }}'],
['maxStyle', 'max={{ style: CSSProperties }}'],
['maxPopoverPlacement', 'max={{ popover: PopoverProps }}'],
['maxPopoverTrigger', 'max={{ popover: PopoverProps }}'],
].forEach(([deprecatedName, newName]) => {
warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
});
}
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
const rootCls = useCSSVarCls(prefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
const cls = classNames(
groupPrefixCls,
{
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
},
cssVarCls,
rootCls,
className,
rootClassName,
hashId,
);
const childrenWithProps = toArray(children).map((child, index) =>
cloneElement(child, {
// eslint-disable-next-line react/no-array-index-key
key: `avatar-key-${index}`,
}),
);
const mergeCount = max?.count || maxCount;
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,
classNames: { root: classNames(`${groupPrefixCls}-popover`, max?.popover?.classNames?.root) },
placement: mergePopoverPlacement,
trigger: mergePopoverTrigger,
};
childrenShow.push(
<Popover key="avatar-popover-key" destroyOnHidden {...mergeProps}>
<Avatar style={mergeStyle}>{`+${numOfChildren - mergeCount}`}</Avatar>
</Popover>,
);
return wrapCSSVar(
<AvatarContextProvider shape={shape} size={size}>
<div className={cls} style={style}>
{childrenShow}
</div>
</AvatarContextProvider>,
);
}
return wrapCSSVar(
<AvatarContextProvider shape={shape} size={size}>
<div className={cls} style={style}>
{childrenWithProps}
</div>
</AvatarContextProvider>,
);
};
export default AvatarGroup;
|