Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Here is the [working version](https://mate-academy.github.io/react_pagination/)

You a given a list of items and markup for the `Pagination`. Implement the
You a given a list of items and markup for the `Pagination`. Implement the
`Pagination` as a stateless component to show only the items for a current page.

1. The `Pagination` should be used with the next props:
Expand Down Expand Up @@ -32,4 +32,4 @@ You a given a list of items and markup for the `Pagination`. Implement the
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_pagination/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://TarasPidkuimukha.github.io/react_pagination/) and add it to the PR description.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
102 changes: 27 additions & 75 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import React from 'react';
import React, { useState } from 'react';
import './App.css';
import { getNumbers } from './utils';
import { Pagination } from './components/Pagination/Pagination';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const items = getNumbers(1, 42).map(n => `Item ${n}`);
const total = 42;

export const App: React.FC = () => {
const [currentPage, setCurrentPage] = useState(1);
const [perPage, setPerPage] = useState(5);
const startIndex = (currentPage - 1) * perPage;
const endIndex = currentPage * perPage;

return (
<div className="container">
<h1>Items with Pagination</h1>

<p className="lead" data-cy="info">
Page 1 (items 1 - 5 of 42)
{`Page ${currentPage} (items ${(currentPage - 1) * perPage + 1} - ${Math.min(currentPage * perPage, total)} of total)`}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pagination info shows the word 'total' literally instead of the actual number. The requirement specifies the format should be Page 1 (items 1 - 5 of 42) - use the total variable value (42) rather than the word 'total'.

</p>

<div className="form-group row">
<div className="col-3 col-sm-2 col-xl-1">
<select
value={perPage}
data-cy="perPageSelector"
id="perPageSelector"
className="form-control">
className="form-control"
onChange={e => {
setPerPage(Number(e.target.value));
setCurrentPage(1);
}}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -31,79 +44,18 @@ export const App: React.FC = () => {
items per page
</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
total={total}
perPage={perPage}
currentPage={currentPage}
onPageChange={page => setCurrentPage(page)}
/>
<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>
{items.slice(startIndex, endIndex).map(item => (
<li key={item} data-cy="item">
{item}
</li>
))}
</ul>
</div>
);
Expand Down
82 changes: 81 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
export const Pagination = () => {};
import classNames from 'classnames';
import React from 'react';

interface PaginationProps {
total: number;
perPage: number;
currentPage: number;
onPageChange: (page: number) => void;
}

export const Pagination: React.FC<PaginationProps> = ({
total,
perPage,
currentPage = 1,
onPageChange,
Comment on lines +10 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The currentPage prop is marked as optional in requirements but the interface doesn't specify a default value. Consider adding a default in the function signature (e.g., currentPage = 1) to fully implement the optional behavior as specified in the task requirements.

}) => {
const pageNumber = [];

for (let i = 1; i <= Math.ceil(total / perPage); i++) {
pageNumber.push(i);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The info text shows 'of total' (literal text) instead of 'of 42' (the actual total number). Should be: of ${total} to display the value correctly.


return (
<div>
<ul className="pagination">
<li
className={classNames('page-item', { disabled: currentPage === 1 })}
>
<a
href="#prev"
data-cy="prevLink"
aria-disabled={currentPage === 1 ? 'true' : 'false'}
onClick={() => {
if (currentPage !== 1) {
onPageChange(currentPage - 1);
}
}}
Comment on lines +29 to +37
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prev link is missing the « character. The next link correctly uses » on line 77, but the prev link appears to be empty on line 37. Add the « character inside the <a> tag for the prev button.

>«</a>
</li>
{pageNumber.map(page => (
<li
key={page}
className={classNames('page-item', {
active: page === currentPage,
})}
>
<a
data-cy="pageLink"
href={`#${page}`}
onClick={() => {
if (page !== currentPage) {
onPageChange(page);
}
}}
>
{page}
</a>
</li>
))}
<li
className={classNames('page-item', {
disabled: currentPage === pageNumber.length,
})}
>
<a
data-cy="nextLink"
href="#next"
aria-disabled={currentPage === pageNumber.length ? 'true' : 'false'}
onClick={() => {
if (currentPage !== pageNumber.length) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
</div>
);
};
Loading