ant-design/components/avatar/group.tsx

113 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-07-13 12:45:37 +08:00
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
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';
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;
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;
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 = 'top',
maxPopoverTrigger = 'hover',
children,
} = props;
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
const [wrapSSR, hashId] = useStyle(prefixCls);
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
},
className,
rootClassName,
hashId,
2020-07-13 12:45:37 +08:00
);
const childrenWithProps = toArray(children).map((child, index) =>
cloneElement(child, { key: `avatar-key-${index}` }),
);
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
key="avatar-popover-key"
2020-07-13 12:45:37 +08:00
content={childrenHidden}
trigger={maxPopoverTrigger}
2020-07-13 12:45:37 +08:00
placement={maxPopoverPlacement}
overlayClassName={`${groupPrefixCls}-popover`}
2020-07-13 12:45:37 +08:00
>
<Avatar style={maxStyle}>{`+${numOfChildren - maxCount}`}</Avatar>
2020-07-13 12:45:37 +08:00
</Popover>,
);
return wrapSSR(
<AvatarContextProvider shape={shape} size={size}>
<div className={cls} style={style}>
{childrenShow}
</div>
</AvatarContextProvider>,
2020-07-13 12:45:37 +08:00
);
}
return wrapSSR(
<AvatarContextProvider shape={shape} size={size}>
<div className={cls} style={style}>
{childrenWithProps}
</div>
</AvatarContextProvider>,
2020-07-13 12:45:37 +08:00
);
};
export default Group;