-
Notifications
You must be signed in to change notification settings - Fork 1.7k
paginator #1521
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?
paginator #1521
Changes from all commits
39c3d79
80b74f6
587f319
66bd539
084b54f
a100682
6513e8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,78 @@ | ||
| export const Pagination = () => {}; | ||
| import React from 'react'; | ||
| import classNames from 'classnames'; | ||
| import { getNumbers } from '../../utils'; | ||
|
|
||
| interface Props { | ||
| total: number; | ||
| perPage: number; | ||
| currentPage?: number; | ||
| onPageChange: (page: number) => void; | ||
| } | ||
|
|
||
|
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 requirements explicitly state using React Router to persist |
||
| export const Pagination: React.FC<Props> = ({ | ||
| total, | ||
| perPage, | ||
| currentPage = 1, | ||
| onPageChange, | ||
| }) => { | ||
| const totalPages = Math.ceil(total / perPage); | ||
|
|
||
| const pages = getNumbers(1, totalPages); | ||
|
|
||
| const handlePageClick = ( | ||
| event: React.MouseEvent<HTMLAnchorElement>, | ||
| page: number, | ||
| ) => { | ||
|
Comment on lines
+23
to
+25
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. Although the pagination itself is implemented correctly, the requirement to persist |
||
| event.preventDefault(); | ||
| if (page >= 1 && page <= totalPages && page !== currentPage) { | ||
| onPageChange(page); | ||
| } | ||
| }; | ||
|
|
||
| const isFirstPage = currentPage === 1; | ||
| const isLastPage = currentPage === totalPages; | ||
|
|
||
| return ( | ||
| <ul className="pagination"> | ||
| <li className={classNames('page-item', { disabled: isFirstPage })}> | ||
| <a | ||
| data-cy="prevLink" | ||
| className="page-link" | ||
| href="#prev" | ||
| aria-disabled={isFirstPage ? 'true' : 'false'} | ||
| onClick={e => handlePageClick(e, currentPage - 1)} | ||
| > | ||
| « | ||
| </a> | ||
| </li> | ||
|
|
||
| {pages.map(page => ( | ||
| <li | ||
| key={page} | ||
| className={classNames('page-item', { active: currentPage === page })} | ||
| > | ||
| <a | ||
| data-cy="pageLink" | ||
| className="page-link" | ||
| href={`#${page}`} | ||
| onClick={e => handlePageClick(e, page)} | ||
| > | ||
| {page} | ||
| </a> | ||
| </li> | ||
| ))} | ||
|
|
||
| <li className={classNames('page-item', { disabled: isLastPage })}> | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled={isLastPage ? 'true' : 'false'} | ||
| onClick={e => handlePageClick(e, currentPage + 1)} | ||
| > | ||
| » | ||
| </a> | ||
| </li> | ||
| </ul> | ||
| ); | ||
| }; | ||
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 task requires using React Router to save
?page=2&perPage=7in the URL and apply them on page load (checklist items #13 and #15), but this component currently managescurrentPageandperPageonly in local state without any router integration. Consider initializing these values from the URL query parameters and updating the URL whenever they change.