diff --git a/src/components/common/Avatar/Avatar.tsx b/src/components/common/Avatar/Avatar.tsx new file mode 100644 index 00000000..9bfc6432 --- /dev/null +++ b/src/components/common/Avatar/Avatar.tsx @@ -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 ( +
+ {alt} +
+ ) +} + +export default Avatar diff --git a/src/components/common/AvatarGroup/AvatarGroup.tsx b/src/components/common/AvatarGroup/AvatarGroup.tsx new file mode 100644 index 00000000..eed1e645 --- /dev/null +++ b/src/components/common/AvatarGroup/AvatarGroup.tsx @@ -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 => { + return true + }) + .slice(0, limit) + .map((avatar, index, avatars) => { + return React.cloneElement(avatar, { + style: { + marginLeft: -size / MARGIN_LEFT_DIVIDE, + zIndex: avatars.length - index, + }, + }) + }) + + return ( +
+
+ {avatars.map((avatar) => ( +
+ {avatar} +
+ ))} +
+ {allAvatars.length > limit && ( +
+ +{allAvatars.length - limit}명 +
+ )} +
+ ) +} + +export default AvatarGroup diff --git a/src/components/common/AvatarGroup/constants/index.ts b/src/components/common/AvatarGroup/constants/index.ts new file mode 100644 index 00000000..2adc2cbb --- /dev/null +++ b/src/components/common/AvatarGroup/constants/index.ts @@ -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 diff --git a/src/components/index.ts b/src/components/index.ts index d813969e..198b3c3f 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,2 +1,4 @@ export { default as Providers } from './Providers/providers' export { default as ThemeButton } from './ThemeButton/themeButton' +export { default as Avatar } from './common/Avatar/Avatar' +export { default as AvatarGroup } from './common/AvatarGroup/AvatarGroup'