2023-11-01 09:54:11 +00:00
import React from "react" ;
// ui
import { Tooltip } from "../tooltip" ;
// types
import { TAvatarSize , getSizeInfo , isAValidNumber } from "./avatar" ;
type Props = {
/ * *
* The children of the avatar group .
* These should ideally should be ` Avatar ` components
* /
children : React.ReactNode ;
/ * *
* The maximum number of avatars to display .
* If the number of children exceeds this value , the additional avatars will be replaced by a count of the remaining avatars .
* @default 2
* /
max? : number ;
/ * *
* Whether to show the tooltip or not
* @default true
* /
showTooltip? : boolean ;
/ * *
* The size of the avatars
* Possible values : "sm" , "md" , "base" , "lg"
* @default "md"
* /
size? : TAvatarSize ;
} ;
export const AvatarGroup : React.FC < Props > = ( props ) = > {
const { children , max = 2 , showTooltip = true , size = "md" } = props ;
// calculate total length of avatars inside the group
const totalAvatars = React . Children . toArray ( children ) . length ;
2023-11-06 15:13:54 +00:00
// if avatars are equal to max + 1, then we need to show the last avatar as well, if avatars are more than max + 1, then we need to show the count of the remaining avatars
const maxAvatarsToRender = totalAvatars <= max + 1 ? max + 1 : max ;
2023-11-01 09:54:11 +00:00
// slice the children to the maximum number of avatars
2023-11-06 15:13:54 +00:00
const avatars = React . Children . toArray ( children ) . slice ( 0 , maxAvatarsToRender ) ;
2023-11-01 09:54:11 +00:00
// assign the necessary props from the AvatarGroup component to the Avatar components
const avatarsWithUpdatedProps = avatars . map ( ( avatar ) = > {
const updatedProps : Partial < Props > = {
showTooltip ,
size ,
} ;
return React . cloneElement ( avatar as React . ReactElement , updatedProps ) ;
} ) ;
// get size details based on the size prop
const sizeInfo = getSizeInfo ( size ) ;
return (
< div className = { ` flex ${ sizeInfo . spacing } ` } >
{ avatarsWithUpdatedProps . map ( ( avatar , index ) = > (
2023-12-10 10:18:10 +00:00
< div key = { index } className = "rounded-full ring-1 ring-custom-background-100" >
2023-11-01 09:54:11 +00:00
{ avatar }
< / div >
) ) }
2023-11-06 15:13:54 +00:00
{ maxAvatarsToRender < totalAvatars && (
2023-12-10 10:18:10 +00:00
< Tooltip tooltipContent = { ` ${ totalAvatars } total ` } disabled = { ! showTooltip } >
2023-11-01 09:54:11 +00:00
< div
className = { ` ${
! isAValidNumber ( size ) ? sizeInfo . avatarSize : ""
2023-12-10 10:18:10 +00:00
} grid place - items - center rounded - full bg - custom - primary - 10 text - [ 9 px ] text - custom - primary - 100 ring - 1 ring - custom - background - 100 ` }
2023-11-01 09:54:11 +00:00
style = {
isAValidNumber ( size )
? {
width : ` ${ size } px ` ,
height : ` ${ size } px ` ,
}
: { }
}
>
+ { totalAvatars - max }
< / div >
< / Tooltip >
) }
< / div >
) ;
} ;