-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-crm): add custom avatar group
- Loading branch information
1 parent
5f1dc62
commit 3a68d75
Showing
3 changed files
with
167 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
examples/app-crm/src/components/custom-avatar-group.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { FC } from "react"; | ||
import { CustomAvatar } from "./custom-avatar"; | ||
import { AvatarProps, Space, Tooltip } from "antd"; | ||
import { Text } from "./text"; | ||
|
||
type Props = { | ||
avatars: { | ||
name?: string; | ||
src?: string; | ||
}[]; | ||
size?: AvatarProps["size"]; | ||
maxCount?: number; | ||
containerStyle?: React.CSSProperties; | ||
avatarStyle?: AvatarProps["style"]; | ||
gap?: string; | ||
overlap?: boolean; | ||
}; | ||
|
||
export const CustomAvatarGroup: FC<Props> = ({ | ||
avatars, | ||
size, | ||
overlap, | ||
maxCount = 3, | ||
gap = "8px", | ||
containerStyle, | ||
avatarStyle, | ||
}) => { | ||
const visibleAvatars = avatars.slice(0, maxCount); | ||
const remainingAvatars = avatars.slice(maxCount); | ||
const hasRemainingAvatars = remainingAvatars.length > 0; | ||
|
||
const getImageSize = (size: AvatarProps["size"] | number) => { | ||
if (typeof size === "number") { | ||
return overlap ? `${size + 4}px` : `${size}px`; | ||
} | ||
|
||
switch (size) { | ||
case "large": | ||
return overlap ? "44px" : "40px"; | ||
case "small": | ||
return overlap ? "28px" : "24px"; | ||
default: | ||
return overlap ? "36px" : "32px"; | ||
} | ||
}; | ||
|
||
const shouldOverlap = overlap && avatars.length > 3; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "flex-start", | ||
gap: shouldOverlap ? "0" : gap, | ||
...containerStyle, | ||
}} | ||
> | ||
{visibleAvatars.map((avatar, index) => { | ||
const transform = shouldOverlap | ||
? `translateX(-${index * 8}px)` | ||
: undefined; | ||
|
||
return ( | ||
<Tooltip title={avatar.name} key={index}> | ||
<CustomAvatar | ||
style={{ | ||
cursor: "pointer", | ||
transform, | ||
zIndex: index, | ||
border: overlap ? "2px solid #fff" : "none", | ||
width: getImageSize(size), | ||
height: getImageSize(size), | ||
...avatarStyle, | ||
}} | ||
name={avatar?.name} | ||
src={avatar?.src} | ||
size={size} | ||
/> | ||
</Tooltip> | ||
); | ||
})} | ||
|
||
{hasRemainingAvatars && ( | ||
<Tooltip | ||
destroyTooltipOnHide | ||
title={ | ||
<Space direction="vertical"> | ||
{remainingAvatars.map((avatar, index) => { | ||
return ( | ||
<Space key={index}> | ||
<CustomAvatar | ||
name={avatar.name} | ||
src={avatar.src} | ||
size="small" | ||
/> | ||
<Text | ||
style={{ | ||
color: "#fff", | ||
}} | ||
key={avatar.name} | ||
> | ||
{avatar.name} | ||
</Text> | ||
</Space> | ||
); | ||
})} | ||
</Space> | ||
} | ||
> | ||
<Text | ||
className="tertiary" | ||
style={{ | ||
userSelect: "none", | ||
cursor: "pointer", | ||
fontSize: "10px", | ||
lineHeight: "22px", | ||
letterSpacing: "0.5px", | ||
fontWeight: 600, | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
borderRadius: "50%", | ||
width: getImageSize(size), | ||
height: getImageSize(size), | ||
transform: shouldOverlap | ||
? `translateX(-${visibleAvatars.length * 8}px)` | ||
: undefined, | ||
zIndex: shouldOverlap ? visibleAvatars.length : 1, | ||
backgroundColor: "#D9D9D9", | ||
border: overlap ? "2px solid #fff" : "none", | ||
}} | ||
> | ||
+{remainingAvatars.length} | ||
</Text> | ||
</Tooltip> | ||
)} | ||
</div> | ||
); | ||
}; |