-
Notifications
You must be signed in to change notification settings - Fork 0
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
[FEAT] Pagination 컴포넌트, 스토리북 구현 #22
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed32b3d
chore : arrow-left/right svg 추가
keemsebin 8ce96d4
feat : Pagination 컴포넌트 구현
keemsebin d49fbaf
feat : Pagination 스토리북 설정
keemsebin 822f602
style : Pagination css 적용
keemsebin a8187f2
style : Pagination outline 제거
keemsebin e05bdc0
style : Icon size 수정
keemsebin 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 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,40 @@ | ||
import { useState } from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { Pagination } from './Pagination'; | ||
|
||
const meta = { | ||
title: 'components/common/Pagination', | ||
component: Pagination, | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof Pagination>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Pagination>; | ||
|
||
export const Basic: Story = { | ||
args: { | ||
currentPage: 1, | ||
totalPages: 2, | ||
className: '', | ||
}, | ||
argTypes: { | ||
currentPage: { control: { type: 'number' } }, | ||
totalPages: { control: { type: 'number' } }, | ||
className: { control: { type: 'text' } }, | ||
}, | ||
render: (args) => { | ||
const [currentPage, setCurrentPage] = useState<number>(1); | ||
|
||
const handlePageChange = (page: number) => { | ||
setCurrentPage(page); | ||
}; | ||
return ( | ||
<Pagination | ||
currentPage={currentPage} | ||
totalPages={args.totalPages} | ||
onPageChange={handlePageChange} | ||
/> | ||
); | ||
}, | ||
}; |
This file contains 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,109 @@ | ||
import { cn } from '@/shared/lib/core'; | ||
|
||
import { Icon } from '../Icon'; | ||
|
||
type WrapperProps = { | ||
/** | ||
* additional className. | ||
*/ | ||
className: string; | ||
} & React.HTMLAttributes<HTMLElement>; | ||
|
||
function PaginationWrapper({ className, ...props }: WrapperProps) { | ||
return ( | ||
<nav | ||
role="navigation" | ||
aria-label="pagination" | ||
className={cn('flex cursor-pointer items-center justify-center gap-1', className)} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
type PaginationItemProps = { | ||
/** | ||
* page number. | ||
*/ | ||
page: number; | ||
/** | ||
* whether the page is active. | ||
*/ | ||
isActive: boolean; | ||
/** | ||
* function to be called when the page is clicked | ||
*/ | ||
onClick: () => void; | ||
}; | ||
|
||
function PaginationItem({ page, isActive, onClick }: PaginationItemProps) { | ||
return ( | ||
<button | ||
onClickCapture={onClick} | ||
className={`size-9 rounded px-3 py-1 font-semibold ${ | ||
isActive ? 'bg-primary-100 text-primary-300 shadow-sm' : 'hover:bg-gray-100' | ||
}`} | ||
> | ||
{page} | ||
</button> | ||
); | ||
} | ||
|
||
type Props = { | ||
/** | ||
* current page number. | ||
*/ | ||
currentPage: number; | ||
/** | ||
* total page number. | ||
*/ | ||
totalPages: number; | ||
/** | ||
* function to be called when page is changed. | ||
*/ | ||
onPageChange: (page: number) => void; | ||
/** | ||
* additional className. | ||
*/ | ||
className?: string; | ||
}; | ||
|
||
export function Pagination({ currentPage, totalPages, onPageChange, className = '' }: Props) { | ||
const handlePrevious = () => { | ||
if (currentPage > 1) onPageChange(currentPage - 1); | ||
}; | ||
|
||
const handleNext = () => { | ||
if (currentPage < totalPages) onPageChange(currentPage + 1); | ||
}; | ||
|
||
return ( | ||
<PaginationWrapper className={className}> | ||
{totalPages >= 3 && currentPage > 1 && ( | ||
<Icon | ||
name="arrowLeft" | ||
size={35} | ||
className="size-9 rounded-md px-2 py-1 hover:bg-gray-100" | ||
onClickCapture={handlePrevious} | ||
/> | ||
)} | ||
|
||
{Array.from({ length: totalPages }, (_, index) => ( | ||
<PaginationItem | ||
key={index} | ||
page={index + 1} | ||
isActive={index + 1 === currentPage} | ||
onClick={() => onPageChange(index + 1)} | ||
/> | ||
))} | ||
|
||
{totalPages >= 3 && currentPage < totalPages && ( | ||
<Icon | ||
name="arrowRight" | ||
size={35} | ||
className="size-9 rounded-md px-2 py-1 hover:bg-gray-100" | ||
onClickCapture={handleNext} | ||
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. 캡쳐링 단계에서 실행하는 이유는 무엇인가요? 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. 네비게이션 아이콘 클릭 시 의도하지 않은 부모 요소의 클릭 이벤트를 방지하기 위해 사용했어요. |
||
/> | ||
)} | ||
</PaginationWrapper> | ||
); | ||
} |
This file contains 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 @@ | ||
export { Pagination } from './Pagination'; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저번 pagination구현 시 남긴 리뷰와 동일하지만, 현재는 최대로 커버할 수 있는 page가 5개이네용. 피드에서도 무한스크롤을 적용하셔서 현재는 우선순위가 떨어지는 것 같고, 저희 sprint 일정이 가능해지면 챙기면 좋아보여요