Skip to content
Open
Show file tree
Hide file tree
Changes from all 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://biconn.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
123 changes: 48 additions & 75 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
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 items = getNumbers(1, 42).map(n => n);
// export const items = getNumbers(1, 42).map(n => `Item ${n}`);

export const App: React.FC = () => {
const [page, setPage] = useState<number>(1);
const [itemPerPage, setItemPerPage] = useState<number>(5);

const handlePageChange = (argPage: number) => {
setPage(argPage);
};

const handlePerPageChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
e.preventDefault();
setItemPerPage(Number(e.target.value));
setPage(1);
};

const start = itemPerPage * (page - 1) + 1;
const end = Math.min(itemPerPage * page, items.length);

const pageStart = itemPerPage * (page - 1);
const pageEnd = itemPerPage * page;

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

<p className="lead" data-cy="info">
Page 1 (items 1 - 5 of 42)
Page {page} (items {start} - {end} of {items.length})
</p>

<div className="form-group row">
<div className="col-3 col-sm-2 col-xl-1">
<select
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"
value={itemPerPage}
onChange={e => handlePerPageChange(e)}
>
<option value={3}>3</option>
<option value={5}>5</option>
<option value={10}>10</option>
<option value={20}>20</option>
</select>
</div>

Expand All @@ -33,77 +57,26 @@ export const App: React.FC = () => {
</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={items.length} // total number of items to paginate
perPage={itemPerPage} // number of items per page
currentPage={page} /* optional with 1 by default */
onPageChange={handlePageChange}
/>

<ul>
<li data-cy="item">Item 1</li>
{items
.filter(item => item > pageStart && item <= pageEnd)
.map(item => (
<li data-cy="item" key={item}>
Item {item}
</li>
))}
{/* <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>
<li data-cy="item">Item 5</li> */}
</ul>
</div>
);
Expand Down
85 changes: 84 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
export const Pagination = () => {};
// import { useState } from 'react';
import { getNumbers } from '../../utils';

interface PaginationType {
total: number;
perPage: number;
currentPage?: number;
onPageChange: (value: number) => void;
}

export const Pagination = ({
total,
perPage,
currentPage = 1,
onPageChange,
Comment on lines 11 to 15

Choose a reason for hiding this comment

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

Provide a default for currentPage when destructuring props. The component uses currentPage directly without a default; set currentPage = 1 in the parameter list (or handle default in the interface) so the component works standalone and follows the spec.

}: PaginationType) => {
const lastPageNumber = Math.ceil(total / perPage);
const countOfPages = getNumbers(1, Math.ceil(total / perPage));

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={e => {
e.preventDefault();
if (currentPage > 1) {
onPageChange(currentPage - 1);
}
}}
>
«
</a>
</li>

{countOfPages.map(item => (
<li
className={`page-item ${item === currentPage ? 'active' : ''}`}
key={item}
>
<a
data-cy="pageLink"
className="page-link"
href="#1"

Choose a reason for hiding this comment

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

This href is static and points to #1 for all page links. While e.preventDefault() stops the browser from navigating, it's better practice to make this dynamic (e.g., href={#${item}}) or use a non-navigational placeholder like href="#". This improves semantics and accessibility.

onClick={e => {
e.preventDefault();

if (item !== currentPage) {
onPageChange(item);
}
}}
>
{item}
</a>
</li>
))}

<li
className={`page-item ${currentPage === lastPageNumber ? 'disabled' : ''}`}
>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={currentPage === lastPageNumber ? 'true' : 'false'}
onClick={e => {
e.preventDefault();

if (currentPage < lastPageNumber) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
</>
);
};