-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Pagination is added #1497
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
f4d0e2f
f37059f
50aa67d
881f457
6061375
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,59 @@ | ||
| export const Pagination = () => {}; | ||
| import classNames from "classnames"; | ||
| import React from "react"; | ||
|
|
||
| interface PaginationProps { | ||
| total: number, | ||
| perPage: number, | ||
| currentPage: number | ||
| onPageChange: (page: number) => void | ||
| } | ||
|
|
||
| export const Pagination: React.FC<PaginationProps> = ({ total, perPage, currentPage, onPageChange }) => { | ||
| const pageNumber = []; | ||
|
|
||
| for (let i = 1; i <= Math.ceil(total / perPage); i++) { | ||
| pageNumber.push(i); | ||
| } | ||
|
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. The info text shows 'of total' (literal text) instead of 'of 42' (the actual total number). Should be: |
||
| return ( | ||
| <div> | ||
| <ul className="pagination"> | ||
| <li className={classNames('page-item', {'disabled': currentPage === 1})}> | ||
| <a | ||
| href="#prev" | ||
| data-cy="prevLink" | ||
| aria-disabled={currentPage === 1 ? 'true' : 'false'} | ||
|
|
||
| onClick={() => { | ||
| if (currentPage !== 1) | ||
| onPageChange(currentPage - 1) | ||
| } | ||
|
|
||
| } | ||
| >«</a> | ||
| </li> | ||
| {pageNumber.map(page => ( | ||
| <li key={page} className={classNames('page-item', {'active' : page === currentPage})}> | ||
| <a | ||
| data-cy="pageLink" | ||
| href={`#${page}`} | ||
|
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. Clicking on the already-active page triggers onPageChange. According to requirement #5, the callback should only be triggered if the page was actually changed. Add a condition to check if page !== currentPage before calling onPageChange. |
||
| onClick={() => onPageChange(page)}>{page}</a> | ||
| </li> | ||
| ))} | ||
| <li className={classNames('page-item', {'disabled' : currentPage === pageNumber.length})}> | ||
| <a | ||
| data-cy="nextLink" | ||
| href="#next" | ||
| aria-disabled={currentPage === pageNumber.length? 'true' : 'false'} | ||
| onClick={() => { | ||
| if (currentPage !== pageNumber.length) { | ||
| onPageChange(currentPage + 1) | ||
| } | ||
| } | ||
| } | ||
| >»</a> | ||
| </li> | ||
|
|
||
| </ul> | ||
| </div> | ||
| ) | ||
| } | ||
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.
The value
42is hardcoded here. Sincetotalis passed to the Pagination component (line 47) and is defined in the items array generation (line 7), consider using it here for better reusability. Thetotalcould be extracted as a constant or computed from items.length.