Skip to content

Commit

Permalink
Merge branch 'feature/#11/component-avatar' of https://github.com/Tea…
Browse files Browse the repository at this point in the history
…m-TenTen/LinkHub-FE into feature/#11/component-avatar
  • Loading branch information
dudwns committed Oct 30, 2023
2 parents 290f2d1 + 701c20e commit 31213e9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CSSProperties } from 'react'
import Image from 'next/image'

export interface AvatarProps {
src: string
width: number
height: number
alt: string
style?: CSSProperties
}

const Avatar = ({ src, width, height, alt }: AvatarProps) => {
return (
<div className="inline-block">
<Image
src={src}
width={width}
height={height}
alt={alt}
className="rounded-full border border-gray-300 object-cover"
/>
</div>
)
}

export default Avatar
59 changes: 59 additions & 0 deletions src/components/AvatarGroup/AvatarGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react'
import { AvatarProps } from '../Avatar/Avatar'
import {
DEFAULT_LIMIT,
DEFAULT_SIZE,
MARGIN_LEFT_DIVIDE,
PADDING_LEFT_DIVIDE,
} from './constants'

export interface AvatarGroupProps {
children: React.ReactNode
size?: number
limit?: number
}

const AvatarGroup = ({
children,
size = DEFAULT_SIZE,
limit = DEFAULT_LIMIT,
}: AvatarGroupProps) => {
const allAvatars = React.Children.toArray(children)
const avatars = React.Children.toArray(children)
.filter((element): element is React.ReactElement<AvatarProps> => {
return true
})
.slice(0, limit)
.map((avatar, index, avatars) => {
return React.cloneElement(avatar, {
style: {
marginLeft: -size / MARGIN_LEFT_DIVIDE,
zIndex: avatars.length - index,
},
})
})

return (
<div className="flex items-center gap-0.5">
<div
className="flex"
style={{ paddingLeft: size / PADDING_LEFT_DIVIDE }}>
{avatars.map((avatar) => (
<div
className="flex"
key={avatar.key}
style={avatar.props.style}>
{avatar}
</div>
))}
</div>
{allAvatars.length > limit && (
<div className="text-xs font-normal leading-4 text-gray-600">
+{allAvatars.length - limit}
</div>
)}
</div>
)
}

export default AvatarGroup
4 changes: 4 additions & 0 deletions src/components/AvatarGroup/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const MARGIN_LEFT_DIVIDE = 7
export const PADDING_LEFT_DIVIDE = 7
export const DEFAULT_SIZE = 60
export const DEFAULT_LIMIT = 3

0 comments on commit 31213e9

Please sign in to comment.