-
-
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 (#4889)
* feat(app-crm): add custom avatar group * chore(app-crm): code structure * fix(app-crm): avatar align * fix(app-crm): avatar align * fix(app-crm): use 6px instead of 4px
- Loading branch information
1 parent
e3b6ef0
commit 8a3b85b
Showing
3 changed files
with
185 additions
and
56 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
141 changes: 141 additions & 0 deletions
141
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,141 @@ | ||
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 shouldOverlap = overlap && avatars.length > 3; | ||
|
||
const getImageSize = (size: AvatarProps["size"] | number) => { | ||
if (typeof size === "number") { | ||
return shouldOverlap ? `${size + 4}px` : `${size}px`; | ||
} | ||
|
||
switch (size) { | ||
case "large": | ||
return shouldOverlap ? "44px" : "40px"; | ||
case "small": | ||
return shouldOverlap ? "28px" : "24px"; | ||
default: | ||
return shouldOverlap ? "36px" : "32px"; | ||
} | ||
}; | ||
|
||
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: shouldOverlap | ||
? "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> | ||
); | ||
}; |