-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add task solution #1473
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?
add task solution #1473
Changes from 1 commit
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,80 @@ | ||
| export const Pagination = () => {}; | ||
| import React from 'react'; | ||
| import classNames from 'classnames'; | ||
| import { getNumbers } from '../../utils'; | ||
|
|
||
| type Props = { | ||
| total: number; | ||
| perPage: number; | ||
| currentPage: number; | ||
|
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 task description states that |
||
| onPageChange: (page: number) => void; | ||
| }; | ||
|
|
||
| export const Pagination: React.FC<Props> = ({ | ||
| total, | ||
| perPage, | ||
| currentPage, | ||
| onPageChange, | ||
| }) => { | ||
| const pagesCount = Math.ceil(total / perPage); | ||
| const pages = getNumbers(1, pagesCount); | ||
|
|
||
| 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 ? 'true' : 'false'} | ||
| onClick={() => { | ||
| if (currentPage !== 1) { | ||
| onPageChange(currentPage - 1); | ||
| } | ||
| }} | ||
|
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. It's a good practice to prevent the default browser navigation for |
||
| > | ||
| « | ||
| </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={() => { | ||
| if (page !== currentPage) { | ||
| onPageChange(page); | ||
| } | ||
| }} | ||
| > | ||
| {page} | ||
| </a> | ||
| </li> | ||
| ))} | ||
|
|
||
| <li | ||
| className={classNames('page-item', { | ||
| disabled: currentPage === pagesCount, | ||
| })} | ||
| > | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled={currentPage === pagesCount ? 'true' : 'false'} | ||
| onClick={() => { | ||
| if (currentPage !== pagesCount) { | ||
| onPageChange(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 state for
perPageandcurrentPageis currently managed locally. According to the requirements, this state should be synchronized with the URL query parameters (e.g.,?page=2&perPage=5) using React Router. On page load, the initial state should also be read from these URL parameters. You can use theuseSearchParamshook fromreact-router-domto manage this.