-
Notifications
You must be signed in to change notification settings - Fork 1
[FE-Feat] Avatar 컴포넌트 기본 레이아웃 작성 #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8977448
feat: Avatar 컴포넌트 기본 기능 작성
dioo1461 e51b5c2
feat: Avatar 컴포넌트 구현
dioo1461 1f7249b
refactor: 잠재적인 문제 제거
dioo1461 44d3865
chore: 해당 storybook의 배경색을 dark로 변경
dioo1461 2d977d3
feat: AvatarItem 컴포넌트에 이미지 주입 기능 추가
dioo1461 df9083f
feat: 에러 처리 및 기본 이미지 fallback으로 AvatarItem 개선
dioo1461 8ccc2ed
refactor: 불필요한 let 을 const로 변경
dioo1461 2424147
Merge branch 'dev' into feature/fe/avatar-component
dioo1461 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,51 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
|
|
||
| import Avatar from '.'; | ||
|
|
||
| const meta: Meta = { | ||
| title: 'Components/Avatar', | ||
| component: Avatar, | ||
| parameters: { | ||
| backgrounds: { | ||
| default: 'dark', | ||
| }, | ||
| }, | ||
| argTypes: { | ||
| size: { | ||
| control: { type: 'radio', options: ['sm', 'lg'] }, | ||
| }, | ||
| imageUrls: { | ||
| control: false, | ||
| }, | ||
| }, | ||
| } satisfies Meta<typeof Avatar>; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof Avatar>; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| size: 'sm', | ||
| imageUrls: [ | ||
| 'https://picsum.photos/200', | ||
| 'https://picsum.photos/201', | ||
| 'https://picsum.photos/202', | ||
| 'https://picsum.photos/203', | ||
| 'https://picsum.photos/204', | ||
| 'https://picsum.photos/205', | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| export const FetchError: Story = { | ||
| args: { | ||
| size: 'sm', | ||
| imageUrls: [ | ||
| 'https://hi.com/', | ||
| 'https://hi.com/', | ||
| 'https://hi.com/', | ||
| 'https://hi.com/', | ||
| ], | ||
| }, | ||
| }; |
This file contains hidden or 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,27 @@ | ||
| import { vars } from '@/theme/index.css'; | ||
|
|
||
| import { Flex } from '../Flex'; | ||
| import type { Typo } from '../Text'; | ||
| import { Text } from '../Text'; | ||
| import type { Size } from '.'; | ||
| import { avatarItemStyle } from './index.css'; | ||
|
|
||
| interface AvatarCountProps { | ||
| size: Size; | ||
| count: number; | ||
| } | ||
|
|
||
| const AvatarCount = ({ size, count }: AvatarCountProps) => ( | ||
| <Flex | ||
| align='center' | ||
| className={avatarItemStyle({ size })} | ||
| justify='center' | ||
| > | ||
| <Text color={vars.color.Ref.Netural[500]} typo={getTypo(size)}>{count}</Text> | ||
| </Flex> | ||
| ); | ||
hamo-o marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const getTypo = (size: Size): Typo => | ||
| size === 'sm' ? 'caption' : 't3'; | ||
|
|
||
| export default AvatarCount; | ||
This file contains hidden or 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,34 @@ | ||
| import { useState } from 'react'; | ||
|
|
||
| import type { Size } from '.'; | ||
| import { avatarItemStyle } from './index.css'; | ||
|
|
||
| interface AvatarItemProps { | ||
| src: string; | ||
| size?: Size; | ||
| alt?: string; | ||
| } | ||
|
|
||
| const AvatarItem = ({ src, size = 'sm', alt }: AvatarItemProps) => { | ||
| const [imgSrc, setImgSrc] = useState(src); | ||
| const [hasError, setHasError] = useState(false); | ||
| const fallbackSrc = 'https://picsum.photos/id/200/200/200'; | ||
| const handleError = () => { | ||
| if (!hasError) { | ||
| setImgSrc(fallbackSrc); | ||
| setHasError(true); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <img | ||
| alt={alt || `Avatar image ${imgSrc}`} | ||
| className={avatarItemStyle({ size })} | ||
| loading='lazy' | ||
| onError={handleError} | ||
| src={imgSrc} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default AvatarItem; |
This file contains hidden or 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 hidden or 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,37 @@ | ||
| import AvatarCount from './AvatarCount'; | ||
| import AvatarItem from './AvatarItem'; | ||
| import { avatarContainerStyle } from './index.css'; | ||
|
|
||
| export type Size = 'sm' | 'lg'; | ||
|
|
||
| interface AvatarProps { | ||
| size: Size; | ||
| imageUrls: string[]; | ||
| } | ||
|
Comment on lines
+7
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding prop validation for imageUrls. Add validation to ensure imageUrls is not empty and contains valid URLs. interface AvatarProps {
size: Size;
- imageUrls: string[];
+ imageUrls: string[] & { length: number };
}
|
||
|
|
||
| const MAX_IMAGE_COUNT = 4; | ||
|
|
||
| const Avatar = ({ size, imageUrls }: AvatarProps) => { | ||
| const ENTIRE_LENGTH = imageUrls.length; | ||
| const limitedUrls = imageUrls.slice(0, MAX_IMAGE_COUNT); | ||
|
|
||
| return ( | ||
| <div className={avatarContainerStyle}> | ||
| {limitedUrls.map((url, index) => ( | ||
| <AvatarItem | ||
| key={`${index}-${url}`} | ||
| size={size} | ||
| src={url} | ||
| /> | ||
| ))} | ||
| {ENTIRE_LENGTH > MAX_IMAGE_COUNT && ( | ||
| <AvatarCount | ||
| count={ENTIRE_LENGTH} | ||
| size={size} | ||
| /> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Avatar; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.