-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.tsx
More file actions
80 lines (71 loc) · 2.1 KB
/
index.tsx
File metadata and controls
80 lines (71 loc) · 2.1 KB
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
'use client';
import classNames from 'classnames';
import type { FC } from 'react';
import { useState, useMemo, Fragment } from 'react';
import type { AvatarProps } from '@/components/Common/AvatarGroup/Avatar';
import Avatar from '@/components/Common/AvatarGroup/Avatar';
import avatarstyles from '@/components/Common/AvatarGroup/Avatar/index.module.css';
import AvatarOverlay from '@/components/Common/AvatarGroup/Overlay';
import Tooltip from '@/components/Common/Tooltip';
import styles from './index.module.css';
type AvatarGroupProps = {
avatars: Array<AvatarProps>;
limit?: number;
isExpandable?: boolean;
size?: AvatarProps['size'];
container?: HTMLElement;
};
const AvatarGroup: FC<AvatarGroupProps> = ({
avatars,
limit = 10,
isExpandable = true,
size = 'small',
container,
}) => {
const [showMore, setShowMore] = useState(false);
const renderAvatars = useMemo(
() => avatars.slice(0, showMore ? avatars.length : limit),
[showMore, avatars, limit]
);
return (
<div
className={classNames(styles.avatarGroup, styles[size], {
[styles.expandable]: avatars.length > limit,
})}
>
{renderAvatars.map(({ ...avatar }) => (
<Fragment key={avatar.nickname}>
<Tooltip
asChild
container={container}
content={<AvatarOverlay {...avatar} />}
>
<Avatar
{...avatar}
size={size}
className={classNames({
'cursor-pointer': avatar.url,
'pointer-events-none': !avatar.url,
})}
/>
</Tooltip>
</Fragment>
))}
{avatars.length > limit && (
<span
onClick={isExpandable ? () => setShowMore(prev => !prev) : undefined}
className={classNames(
avatarstyles.avatar,
avatarstyles[size],
'cursor-pointer'
)}
>
<span className={avatarstyles.item}>
{`${showMore ? '-' : '+'}${avatars.length - limit}`}
</span>
</span>
)}
</div>
);
};
export default AvatarGroup;