-
Notifications
You must be signed in to change notification settings - Fork 1.7k
solution #1438
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?
solution #1438
Changes from all commits
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,92 @@ | ||
| export const Pagination = () => {}; | ||
| import classNames from 'classnames'; | ||
| import React from 'react'; | ||
|
|
||
| type Props = { | ||
| total: number; | ||
| perPage: number; | ||
| currentPage?: number; | ||
| onPageChange: (value: number) => void; | ||
| }; | ||
|
|
||
| export const Pagination: React.FC<Props> = ({ | ||
| total, | ||
| perPage, | ||
| currentPage = 1, | ||
| onPageChange, | ||
| }) => { | ||
| const pages = Array.from( | ||
| { length: Math.ceil(total / perPage) }, | ||
| (_, i) => i + 1, | ||
| ); | ||
|
|
||
| const totalPages = Math.ceil(total / perPage); | ||
|
|
||
| return ( | ||
| <ul className="pagination"> | ||
| <li | ||
| className={classNames('page-item', { | ||
| disabled: currentPage <= 1, | ||
| })} | ||
| > | ||
| <a | ||
| data-cy="prevLink" | ||
| className="page-link" | ||
| href="#prev" | ||
| aria-disabled={currentPage <= 1} | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| if (currentPage > 1) { | ||
| onPageChange(currentPage - 1); | ||
| } | ||
| }} | ||
| > | ||
| « | ||
| </a> | ||
| </li> | ||
|
|
||
| {pages.map(page => { | ||
| return ( | ||
| <li | ||
| className={classNames('page-item', { | ||
| active: page === currentPage, | ||
| })} | ||
| key={page} | ||
| > | ||
| <a | ||
| data-cy="pageLink" | ||
| className="page-link" | ||
| href={`#${page}`} | ||
| onClick={() => { | ||
| if (page !== currentPage) { | ||
| onPageChange(page); | ||
| } | ||
| }} | ||
| > | ||
| {page} | ||
| </a> | ||
| </li> | ||
| ); | ||
| })} | ||
| <li | ||
| className={classNames('page-item', { | ||
| disabled: currentPage >= totalPages, | ||
| })} | ||
| > | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled={currentPage === pages[pages.length - 1]} | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| if (currentPage !== pages[pages.length - 1]) { | ||
| onPageChange(currentPage + 1); | ||
| } | ||
|
Comment on lines
+79
to
+84
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. Your logic for disabling the 'next' link is correct. For consistency, consider using the |
||
| }} | ||
| > | ||
| » | ||
| </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 manage state in the URL. Currently,
perPageandcurrentPageare stored in local component state usinguseState. This state should be synchronized with the URL query parameters (e.g.,?page=...&perPage=...) so that the values are applied on page load and the URL reflects the current pagination state.