-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add solution #1484
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 solution #1484
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { HashRouter } from 'react-router-dom'; | ||
| import App from './App'; | ||
|
|
||
| export const Root = () => ( | ||
| <HashRouter> | ||
| <App /> | ||
| </HashRouter> | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,139 @@ | ||
| export const Pagination = () => {}; | ||
| import classNames from 'classnames'; | ||
| import React from 'react'; | ||
|
|
||
| type Props = { | ||
| total: number; | ||
| perPage: number; | ||
| currentPage?: number; | ||
| onPageChange: (page: number) => void; | ||
| }; | ||
|
|
||
| export const Pagination: React.FC<Props> = ({ | ||
| total, | ||
| perPage, | ||
| currentPage = 1, | ||
| onPageChange, | ||
| }) => { | ||
| const countPage = Math.ceil(total / perPage); | ||
|
|
||
| const disabledPrevLink = currentPage === 1; | ||
| const disabledNextLink = currentPage === countPage; | ||
|
|
||
| return ( | ||
| <ul className="pagination"> | ||
| <li className={classNames('page-item', { disabled: disabledPrevLink })}> | ||
| <a | ||
| data-cy="prevLink" | ||
| className="page-link" | ||
| href="#" | ||
| aria-disabled={disabledPrevLink} | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| if (!disabledPrevLink) { | ||
| onPageChange(currentPage - 1); | ||
| } | ||
| }} | ||
| > | ||
| « | ||
| </a> | ||
| </li> | ||
| {Array.from({ length: countPage }, (_, index) => { | ||
| const count = index + 1; | ||
|
|
||
| return ( | ||
| <li | ||
| className={classNames('page-item', { | ||
| active: currentPage === count, | ||
| })} | ||
| key={count} | ||
| > | ||
| <a | ||
| data-cy="pageLink" | ||
| className="page-link" | ||
| href="#" | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
|
Comment on lines
+53
to
+55
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 |
||
| onPageChange(count); | ||
|
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. This calls |
||
| }} | ||
| > | ||
| {count} | ||
| </a> | ||
| </li> | ||
| ); | ||
| })} | ||
| <li className={classNames('page-item', { disabled: disabledNextLink })}> | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled={disabledNextLink} | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| if (disabledNextLink) { | ||
| return; | ||
| } | ||
|
|
||
| onPageChange(currentPage + 1); | ||
| }} | ||
| > | ||
| » | ||
| </a> | ||
| </li> | ||
| {/* <li className="page-item active"> | ||
| <a data-cy="pageLink" className="page-link" href="#1"> | ||
| 1 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#2"> | ||
| 2 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#3"> | ||
| 3 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#4"> | ||
| 4 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#5"> | ||
| 5 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#6"> | ||
| 6 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#7"> | ||
| 7 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#8"> | ||
| 8 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a data-cy="pageLink" className="page-link" href="#9"> | ||
| 9 | ||
| </a> | ||
| </li> | ||
| <li className="page-item"> | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled="false" | ||
| > | ||
| » | ||
| </a> | ||
| </li> */} | ||
|
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. There's a large block of commented-out HTML (lines 82-136) that serves no purpose in the final implementation. This should be removed to keep the code clean. |
||
| </ul> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { createRoot } from 'react-dom/client'; | ||
|
|
||
| import { App } from './App'; | ||
| import { Root } from './Root'; | ||
|
|
||
| createRoot(document.getElementById('root') as HTMLElement).render(<App />); | ||
| createRoot(document.getElementById('root') as HTMLElement).render(<Root />); |
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
onPageChange(count)callback is called even whencount === currentPage. Per requirement #5, the callback should only be triggered if the page actually changed. Consider adding a condition:if (count !== currentPage) { onPageChange(count); }