Fix: avatar group buggy with +0 (#7044)

* fix AvatarGroup, refactor a bit

* handle cases where truncated avatars are 2 digits

* use slice, and add comment

* update comment

* Update packages/ui/components/avatar/AvatarGroup.tsx

* fix classname order issue

---------

Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
Amir FakhrullahandPeer Richelsen authored and GitHub committed 2023-02-11 16:25:36 +00:00
1 parent 1f74837189
commit f87254cdc3
1 file changed
+34 -37
+34 -37
View File
@@ -16,48 +16,45 @@ export type AvatarGroupProps = {
}; };
export const AvatarGroup = function AvatarGroup(props: AvatarGroupProps) { export const AvatarGroup = function AvatarGroup(props: AvatarGroupProps) {
const avatars = props.items.slice(0, 4);
const LENGTH = props.items.length; const LENGTH = props.items.length;
const truncateAfter = props.truncateAfter || 4; const truncateAfter = props.truncateAfter || 4;
/**
* First, filter all the avatars object that have image
* Then, slice it until before `truncateAfter` index
*/
const displayedAvatars = props.items.filter((avatar) => avatar.image).slice(0, truncateAfter);
const numTruncatedAvatars = LENGTH - displayedAvatars.length;
return ( return (
<ul className={classNames("flex items-center", props.className)}> <ul className={classNames("flex items-center", props.className)}>
{avatars.map((item, enumerator) => { {displayedAvatars.map((item, idx) => (
if (item.image != null) { <li key={idx} className="-mr-[4px] inline-block">
if (LENGTH > truncateAfter && enumerator === truncateAfter - 1) { <Avatar
return ( className="border-gray-200"
<li key={enumerator} className="relative -mr-[4px] inline-block "> imageSrc={item.image}
<div className="relative"> title={item.title}
<div className="h-90 relative min-w-full scale-105 transform border-gray-200 "> alt={item.alt || ""}
<Avatar className="" imageSrc={item.image} alt={item.alt || ""} size={props.size} /> accepted={props.accepted}
</div> size={props.size}
</div> href={item.href}
<div />
className={classNames( </li>
"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-white", ))}
props.size === "sm" ? "text-base" : "text-2xl" {numTruncatedAvatars > 0 && (
)}> <li
<span>+{LENGTH - truncateAfter - 1}</span> className={classNames(
</div> "bg-darkgray-300 relative -mr-[4px] mb-1 inline-flex justify-center overflow-hidden rounded-full",
</li> props.size === "sm" ? "min-w-6 h-6" : "min-w-16 h-16"
); )}>
} <span
// Always display the first Four items items className={classNames(
return ( "m-auto px-1 text-center text-white",
<li key={enumerator} className="-mr-[4px] inline-block"> props.size === "sm" ? "text-[12px]" : "text-2xl"
<Avatar )}>
className="border-gray-200" +{numTruncatedAvatars}
imageSrc={item.image} </span>
title={item.title} </li>
alt={item.alt || ""} )}
accepted={props.accepted}
size={props.size}
href={item.href}
/>
</li>
);
}
})}
</ul> </ul>
); );
}; };