-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add pagination #1523
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?
feat: add pagination #1523
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,89 @@ | ||
| export const Pagination = () => {}; | ||
| import cn from 'classnames'; | ||
| import React from 'react'; | ||
| interface Props { | ||
| total: number; | ||
| perPage: number; | ||
| currentPage?: number; | ||
| onPageChange: (selectedPage: number) => void; | ||
| } | ||
|
|
||
| export const Pagination: React.FC<Props> = ({ | ||
| total, | ||
| perPage = 5, | ||
| currentPage = 1, | ||
| onPageChange, | ||
| }) => { | ||
| const totalPages: number = Math.ceil(total / perPage); | ||
| const pages: number[] = Array.from({ length: totalPages }, (_, i) => i + 1); | ||
|
|
||
| return ( | ||
| <ul className="pagination"> | ||
| <li | ||
| className={cn('page-item', { | ||
| disabled: currentPage <= 1, | ||
| })} | ||
| > | ||
| <a | ||
| data-cy="prevLink" | ||
| className={cn('page-link')} | ||
| href="#prev" | ||
| aria-disabled={currentPage === 1} | ||
| onClick={e => { | ||
| e.preventDefault(); | ||
| if (currentPage > 1) { | ||
| onPageChange(currentPage - 1); | ||
| } | ||
| }} | ||
| > | ||
| « | ||
| </a> | ||
| </li> | ||
| {pages.map(num => { | ||
| return ( | ||
| <li | ||
| key={num} | ||
| className={cn('page-item', { | ||
| active: currentPage === num, | ||
| })} | ||
| > | ||
| <a | ||
| data-cy="pageLink" | ||
| className={cn('page-link', { | ||
| active: currentPage === num, | ||
| })} | ||
| href={`#${num}`} | ||
| onClick={e => { | ||
| e.preventDefault(); | ||
| if (num !== currentPage) { | ||
| onPageChange(num); | ||
| } | ||
| }} | ||
| > | ||
| {num} | ||
| </a> | ||
| </li> | ||
| ); | ||
| })} | ||
|
Comment on lines
+16
to
+66
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 App manages pagination state correctly, but there is no use of React Router to sync |
||
| <li | ||
| className={cn('page-item', { | ||
| disabled: currentPage === totalPages, | ||
| })} | ||
| > | ||
| <a | ||
| data-cy="nextLink" | ||
| className={cn('page-link')} | ||
| href="#next" | ||
| aria-disabled={currentPage === totalPages} | ||
| onClick={e => { | ||
| e.preventDefault(); | ||
| if (currentPage < totalPages) { | ||
| onPageChange(currentPage + 1); | ||
| } | ||
| }} | ||
| > | ||
| » | ||
| </a> | ||
| </li> | ||
| </ul> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| import { createRoot } from 'react-dom/client'; | ||
|
|
||
| // eslint-disable-next-line import/no-extraneous-dependencies | ||
| import { BrowserRouter } from 'react-router'; | ||
| import { App } from './App'; | ||
|
|
||
| createRoot(document.getElementById('root') as HTMLElement).render(<App />); | ||
| createRoot(document.getElementById('root') as HTMLElement).render( | ||
| <BrowserRouter> | ||
| <App /> | ||
| </BrowserRouter>, | ||
| ); |
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
Propsinterface markscurrentPageas required, but the task statescurrentPageis optional with a default of 1. Make this prop optional (currentPage?: number) to match the requirements and your default parameter value.