-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feat] Pagination, SearchBar 공통 컴포넌트
- Loading branch information
Showing
14 changed files
with
610 additions
and
28 deletions.
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
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
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
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
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
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,36 @@ | ||
import { useState } from 'react' | ||
import Pagination, { PaginationProps } from '@/components/common/Pagination' | ||
import { Meta, StoryFn } from '@storybook/react' | ||
|
||
export default { | ||
title: 'Common/Pagination', | ||
component: Pagination, | ||
} as Meta | ||
|
||
const Template: StoryFn<PaginationProps> = ({ | ||
pagesCount, | ||
currentPage: initialCurrentPage, | ||
onPageChange, | ||
}: PaginationProps) => { | ||
const [currentPage, setCurrentPage] = useState(initialCurrentPage) | ||
|
||
const handlePageChange = (page: number) => { | ||
setCurrentPage(page) | ||
onPageChange(page) | ||
} | ||
|
||
return ( | ||
<Pagination | ||
pagesCount={pagesCount} | ||
currentPage={currentPage} | ||
onPageChange={handlePageChange} | ||
/> | ||
) | ||
} | ||
|
||
export const Primary = Template.bind({}) | ||
Primary.args = { | ||
pagesCount: 5, | ||
currentPage: 1, | ||
onPageChange: () => {}, | ||
} |
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,101 @@ | ||
import React, { useState, useEffect } from 'react' | ||
|
||
export interface PaginationProps { | ||
pagesCount: number | ||
currentPage: number | ||
onPageChange: (page: number) => void | ||
} | ||
|
||
const Pagination = ({ | ||
pagesCount, | ||
currentPage, | ||
onPageChange, | ||
}: PaginationProps) => { | ||
const [currentPageGroup, setCurrentPageGroup] = useState<number>(1) | ||
const pagesPerGroup = 5 | ||
|
||
const totalGroups = Math.ceil(pagesCount / pagesPerGroup) | ||
|
||
useEffect(() => { | ||
const newPageGroup = Math.ceil(currentPage / pagesPerGroup) | ||
setCurrentPageGroup(newPageGroup) | ||
}, [currentPage]) | ||
|
||
const startPage = (currentPageGroup - 1) * pagesPerGroup + 1 | ||
const endPage = Math.min(currentPageGroup * pagesPerGroup, pagesCount) | ||
|
||
const pages = Array.from( | ||
{ length: endPage - startPage + 1 }, | ||
(_, i) => startPage + i, | ||
) | ||
|
||
const handlePageChange = (page: number) => { | ||
onPageChange(page) | ||
window.scrollTo({ top: 0, behavior: 'smooth' }) // 화면을 최상단으로 부드럽게 스크롤 | ||
} | ||
|
||
const handlePreviousGroup = () => { | ||
if (currentPageGroup > 1) { | ||
const newPageGroup = currentPageGroup - 1 | ||
setCurrentPageGroup(newPageGroup) | ||
handlePageChange((newPageGroup - 1) * pagesPerGroup + 1) | ||
} | ||
} | ||
|
||
const handleNextGroup = () => { | ||
if (currentPageGroup < totalGroups) { | ||
const newPageGroup = currentPageGroup + 1 | ||
setCurrentPageGroup(newPageGroup) | ||
handlePageChange((newPageGroup - 1) * pagesPerGroup + 1) | ||
} | ||
} | ||
|
||
return ( | ||
<nav> | ||
<ul className="flex justify-center items-center gap-3"> | ||
<li className={`page-item ${currentPageGroup === 1 ? 'disabled' : ''}`}> | ||
<button | ||
type="button" | ||
className="text-headline text-gray2 font-semibold" | ||
onClick={handlePreviousGroup} | ||
disabled={currentPageGroup === 1} | ||
> | ||
이전 | ||
</button> | ||
</li> | ||
{pages.map((page) => ( | ||
<li key={page} className={`${page === currentPage ? 'active' : ''}`}> | ||
<button | ||
type="button" | ||
className={`text-headline text-gray2 font-semibold w-8 h-8 border rounded-[3px] ${ | ||
page === currentPage | ||
? 'border-pointcolor1 bg-main4 text-maindark' | ||
: 'border-transparent' | ||
}`} | ||
onClick={() => handlePageChange(page)} | ||
aria-current={page === currentPage ? 'page' : undefined} | ||
> | ||
{page} | ||
</button> | ||
</li> | ||
))} | ||
<li | ||
className={`page-item ${ | ||
currentPageGroup === totalGroups ? 'disabled' : '' | ||
}`} | ||
> | ||
<button | ||
type="button" | ||
className="text-headline text-gray2 font-semibold" | ||
onClick={handleNextGroup} | ||
disabled={currentPageGroup === totalGroups} | ||
> | ||
다음 | ||
</button> | ||
</li> | ||
</ul> | ||
</nav> | ||
) | ||
} | ||
|
||
export default Pagination |
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,19 @@ | ||
import SearchBar, { SearchBarProps } from '@/components/common/SearchBar' | ||
import { Meta, StoryFn } from '@storybook/react' | ||
|
||
export default { | ||
title: 'Common/SearchBar', | ||
component: SearchBar, | ||
} as Meta<SearchBarProps> | ||
|
||
const Template: StoryFn<SearchBarProps> = (args: SearchBarProps) => ( | ||
<SearchBar {...args} /> | ||
) | ||
|
||
export const Primary = Template.bind({}) | ||
Primary.args = { | ||
onSearch: (filter, query) => { | ||
console.log('filter:', filter) | ||
console.log('query:', query) | ||
}, | ||
} |
Oops, something went wrong.