-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create common component for pagination
- Loading branch information
Showing
2 changed files
with
86 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'react'; | ||
import {Pagination} from 'react-bootstrap'; | ||
|
||
interface Props { | ||
setOffset: (...args: any[]) => void; | ||
setActivePage: (...args: any[]) => void; | ||
offsetValue: number; | ||
maxPages: number; | ||
activePage: number; | ||
} | ||
export const PaginationBarComponent: React.FunctionComponent<Props> = ( | ||
props | ||
) => { | ||
const {setOffset, setActivePage, offsetValue, maxPages, activePage} = props; | ||
|
||
const previousTenPagination = () => { | ||
if (offsetValue > 0) { | ||
setOffset(offsetValue - 20); | ||
setActivePage(activePage - 1); | ||
} | ||
}; | ||
|
||
const nextTenPagination = () => { | ||
if (activePage !== maxPages) { | ||
setOffset(offsetValue + 20); | ||
setActivePage(activePage + 1); | ||
} | ||
}; | ||
|
||
const items = []; | ||
const handlePaginationByPageNumber = (pageNumber: number) => { | ||
setOffset((pageNumber - 1) * 20); | ||
setActivePage(pageNumber); | ||
}; | ||
|
||
let startPage; | ||
let endPage; | ||
if (maxPages <= 9) { | ||
startPage = 1; | ||
endPage = maxPages; | ||
} else if (maxPages - activePage <= 4) { | ||
startPage = maxPages - 8; | ||
endPage = maxPages; | ||
} else if (activePage <= 5) { | ||
startPage = 1; | ||
endPage = 9; | ||
} else { | ||
startPage = activePage - 4; | ||
endPage = activePage + 4; | ||
} | ||
|
||
for (let pageNumber = startPage; pageNumber <= endPage; pageNumber++) { | ||
items.push( | ||
<Pagination.Item | ||
key={pageNumber} | ||
active={pageNumber === activePage} | ||
onClick={() => handlePaginationByPageNumber(pageNumber)} | ||
> | ||
{pageNumber} | ||
</Pagination.Item> | ||
); | ||
} | ||
|
||
return ( | ||
<Pagination> | ||
<Pagination.First onClick={() => handlePaginationByPageNumber(1)} /> | ||
<Pagination.Prev onClick={previousTenPagination} /> | ||
{startPage !== 1 && <Pagination.Ellipsis />} | ||
<Pagination>{items}</Pagination> | ||
{endPage !== maxPages && <Pagination.Ellipsis />} | ||
<Pagination.Next onClick={nextTenPagination} /> | ||
<Pagination.Last onClick={() => handlePaginationByPageNumber(maxPages)} /> | ||
</Pagination> | ||
); | ||
}; | ||
|
||
export default PaginationBarComponent; |
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