-
Notifications
You must be signed in to change notification settings - Fork 0
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 : pageNavigator 생성 * [refactor] : pr반영 --------- Co-authored-by: 김문경 <>
- Loading branch information
1 parent
28b28a8
commit 9965302
Showing
4 changed files
with
84 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Button, Flex } from '@chakra-ui/react'; | ||
import { BiChevronRight, BiChevronLeft } from 'react-icons/bi'; | ||
|
||
import { PageNavigatorProps } from './types'; | ||
|
||
const PageNavigator = ({ currentPage, setCurrentPage, componentLength, itemsPerPage }: PageNavigatorProps) => { | ||
const totalPages = Math.ceil(componentLength / itemsPerPage); | ||
const goToPage = (pageNumber: number) => { | ||
setCurrentPage(pageNumber); | ||
}; | ||
|
||
const goToPreviousPage = () => { | ||
setCurrentPage((prevPage: number) => Math.max(prevPage - 1, 1)); | ||
}; | ||
|
||
const goToNextPage = () => { | ||
setCurrentPage((prevPage: number) => Math.min(prevPage + 1, totalPages)); | ||
}; | ||
|
||
const currentTensDigit = Math.floor((currentPage - 1) / 10) * 10; | ||
return ( | ||
<Flex align="center" justify="center" mt="4"> | ||
<Button | ||
mr="2" | ||
bgColor="transparent" | ||
disabled={currentPage === 1} | ||
leftIcon={<BiChevronLeft />} | ||
onClick={goToPreviousPage} | ||
rounded="xl" | ||
/> | ||
{Array.from({ length: Math.min(totalPages - currentTensDigit, 10) }, (_, index) => { | ||
const pageNumber = currentTensDigit + index + 1; | ||
return ( | ||
<Button | ||
key={pageNumber} | ||
mr="2" | ||
shadow={currentPage === pageNumber ? 'md' : 'none'} | ||
bgColor={currentPage === pageNumber ? 'white' : 'transparent'} | ||
onClick={() => goToPage(pageNumber)} | ||
rounded="xl" | ||
> | ||
{pageNumber} | ||
</Button> | ||
); | ||
})} | ||
<Button | ||
mr="2" | ||
bgColor="transparent" | ||
disabled={currentPage === totalPages} | ||
onClick={goToNextPage} | ||
rightIcon={<BiChevronRight />} | ||
rounded="xl" | ||
/> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default PageNavigator; |
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,6 @@ | ||
export interface PageNavigatorProps { | ||
currentPage: number; | ||
setCurrentPage: (pageNumber: number | ((prevPage: number) => number)) => void; | ||
componentLength: number; | ||
itemsPerPage: number; | ||
} |
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