-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Change 1 #1516
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?
Change 1 #1516
Changes from all commits
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,86 @@ | ||
| export const Pagination = () => {}; | ||
| import React from 'react'; | ||
| import cn from 'classnames'; | ||
| import { getNumbers } from '../../utils'; | ||
|
|
||
| interface PaginationType { | ||
| total: number; | ||
| perPage: number; | ||
| currentPage?: number; | ||
| onPageChange: (page: number) => void; | ||
| } | ||
|
|
||
| export const Pagination = ({ | ||
|
Comment on lines
+9
to
+12
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 |
||
| total, | ||
| perPage, | ||
| currentPage = 1, | ||
| onPageChange, | ||
| }: PaginationType) => { | ||
| const pages = getNumbers(1, Math.ceil(total / perPage)); | ||
|
|
||
| return ( | ||
| <> | ||
| <ul className="pagination"> | ||
| <li className={cn(`page-item ${currentPage === 1 ? `disabled` : ``}`)}> | ||
| <a | ||
| data-cy="prevLink" | ||
| className="page-link" | ||
| href="#prev" | ||
| aria-disabled={currentPage === 1} | ||
| onClick={e => { | ||
| e.preventDefault(); | ||
|
|
||
| if (currentPage > 1) { | ||
| onPageChange(currentPage - 1); | ||
| } | ||
| }} | ||
| > | ||
| « | ||
| </a> | ||
| </li> | ||
| {pages.map(page => ( | ||
| <li | ||
| key={page} | ||
| className={cn(`page-item ${currentPage === page ? 'active' : ''}`)} | ||
| > | ||
| <a | ||
| data-cy="pageLink" | ||
| className="page-link" | ||
| href={`#${page}`} | ||
| onClick={e => { | ||
| e.preventDefault(); | ||
|
|
||
| if (page !== currentPage) { | ||
| onPageChange(page); | ||
| } | ||
| }} | ||
| > | ||
| {page} | ||
| </a> | ||
| </li> | ||
| ))} | ||
|
|
||
| <li | ||
| className={cn( | ||
| `page-item ${currentPage === pages.length ? `disabled` : ``}`, | ||
| )} | ||
| > | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled={currentPage === pages.length} | ||
| onClick={e => { | ||
| e.preventDefault(); | ||
|
|
||
| if (currentPage < pages.length) { | ||
| 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.
onPageChange(currentPage - 1)is executed even whencurrentPage === 1. According to checklist items #5, #7 and #9, the prev link must be disabled on the first page and the callback should only fire when the page actually changes. Add a guard so that whencurrentPage === 1the handler does nothing and does not callonPageChange.