-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Solution #1472
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 #1472
Changes from 1 commit
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,29 +1,51 @@ | ||
| import React from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import './App.css'; | ||
| import { getNumbers } from './utils'; | ||
| import { Pagination } from './components/Pagination'; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const items = getNumbers(1, 42).map(n => `Item ${n}`); | ||
|
|
||
| export const App: React.FC = () => { | ||
| const perPageOptions = [3, 5, 10, 20] as const; | ||
|
|
||
| type PerPageType = (typeof perPageOptions)[number]; | ||
| const [elementsPerPage, setElementsPerPage] = useState<PerPageType>(5); | ||
| const [currentPage, setCurrentPage] = useState(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. According to the requirements, the current page and items per page should be stored in the URL (e.g., You should use the
|
||
| const firstItemOnPage = elementsPerPage * (currentPage - 1); | ||
| const startItem = firstItemOnPage + 1; | ||
| const endItem = Math.min(firstItemOnPage + elementsPerPage, items.length); | ||
|
|
||
| const visibleItems = items.slice( | ||
| firstItemOnPage, | ||
| firstItemOnPage + elementsPerPage, | ||
| ); | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <h1>Items with Pagination</h1> | ||
|
|
||
|
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 requires the |
||
| <p className="lead" data-cy="info"> | ||
| Page 1 (items 1 - 5 of 42) | ||
| {`Page ${currentPage} (items ${startItem} - ${endItem} of ${items.length})`} | ||
| </p> | ||
|
|
||
| <div className="form-group row"> | ||
| <div className="col-3 col-sm-2 col-xl-1"> | ||
| <select | ||
| value={elementsPerPage} | ||
| onChange={e => { | ||
| setElementsPerPage(Number(e.target.value) as PerPageType); | ||
| setCurrentPage(1); | ||
| }} | ||
| data-cy="perPageSelector" | ||
| id="perPageSelector" | ||
| className="form-control"> | ||
| <option value="3">3</option> | ||
| <option value="5">5</option> | ||
| <option value="10">10</option> | ||
| <option value="20">20</option> | ||
| className="form-control" | ||
| > | ||
| {perPageOptions.map(n => ( | ||
| <option key={n} value={n}> | ||
| {n} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </div> | ||
|
|
||
|
|
@@ -32,78 +54,19 @@ export const App: React.FC = () => { | |
| </label> | ||
| </div> | ||
|
|
||
| {/* Move this markup to Pagination */} | ||
| <ul className="pagination"> | ||
| <li className="page-item disabled"> | ||
| <a | ||
| data-cy="prevLink" | ||
| className="page-link" | ||
| href="#prev" | ||
| aria-disabled="true"> | ||
| « | ||
| </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> | ||
| </ul> | ||
| <Pagination | ||
| perPage={elementsPerPage} | ||
| currentPage={currentPage} | ||
| total={items.length} | ||
| onPageChange={setCurrentPage} | ||
| /> | ||
|
|
||
| <ul> | ||
| <li data-cy="item">Item 1</li> | ||
| <li data-cy="item">Item 2</li> | ||
| <li data-cy="item">Item 3</li> | ||
| <li data-cy="item">Item 4</li> | ||
| <li data-cy="item">Item 5</li> | ||
| {visibleItems.map(item => ( | ||
| <li key={item} data-cy="item"> | ||
| {item} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,76 @@ | ||
| export const Pagination = () => {}; | ||
| import { getNumbers } from '../../utils'; | ||
|
|
||
| interface PaginationProps { | ||
| 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 specifies that the |
||
| total: number; | ||
| onPageChange: (page: number) => void; | ||
| } | ||
|
|
||
| export const Pagination: React.FC<PaginationProps> = ({ | ||
| perPage, | ||
| currentPage, | ||
| total, | ||
|
Comment on lines
+12
to
+13
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. According to the task description, |
||
| onPageChange, | ||
| }) => { | ||
| const totalPages = Math.ceil(total / perPage); | ||
| const pageSelector = getNumbers(1, totalPages).map(page => ( | ||
| <li | ||
|
Comment on lines
+15
to
+18
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 component correctly reads
It's best practice to validate input from external sources like URLs. You should check if |
||
| key={page} | ||
| className={`page-item ${page === currentPage ? 'active' : ''}`} | ||
| > | ||
| <a | ||
| data-cy="pageLink" | ||
| className="page-link" | ||
| href={`#${page}`} | ||
| onClick={() => onPageChange(page)} | ||
|
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 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 action for |
||
| > | ||
| {page} | ||
| </a> | ||
| </li> | ||
| )); | ||
|
|
||
| return ( | ||
| <ul className="pagination"> | ||
| <li className={`page-item ${currentPage === 1 ? 'disabled' : ''}`}> | ||
| <a | ||
| data-cy="prevLink" | ||
| className="page-link" | ||
| href="#prev" | ||
| aria-disabled={currentPage === 1 ? 'true' : 'false'} | ||
| onClick={() => { | ||
| if (currentPage === 1) { | ||
| return; | ||
| } | ||
|
|
||
| onPageChange(currentPage - 1); | ||
| }} | ||
| > | ||
| « | ||
| </a> | ||
| </li> | ||
|
|
||
| {pageSelector} | ||
|
|
||
| <li | ||
| className={`page-item ${currentPage === totalPages ? 'disabled' : ''}`} | ||
|
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 check for disabling the 'next' button should use The same change is needed for the |
||
| > | ||
| <a | ||
| data-cy="nextLink" | ||
| className="page-link" | ||
| href="#next" | ||
| aria-disabled={currentPage === totalPages ? 'true' : 'false'} | ||
| onClick={() => { | ||
| if (currentPage === totalPages) { | ||
| return; | ||
| } | ||
|
|
||
| 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.
According to the requirements, the
currentPageprop should be optional with a default value of 1. Currently, it's defined as a required prop in thePaginationPropsinterface. You can make it optional in the interface (e.g.,currentPage?: number) and then provide a default value within the component.