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
@@ -1,8 +1,8 @@
# React Pagination

> Here is the [working version](https://mate-academy.github.io/react_pagination/)
> Here is the [working version](https://vl-tsr.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
12 changes: 7 additions & 5 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
105 changes: 31 additions & 74 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
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 [itemsPerPage, setItemsForPage] = useState(5);
const [currentPage, setCurrentPage] = useState(1);

const fromItems = (currentPage - 1) * itemsPerPage;
const toItems = fromItems + itemsPerPage;

const currentPageItems = items.slice(fromItems, toItems);

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 {fromItems + 1} -{' '}
{fromItems + currentPageItems.length} of {items.length})
</p>

<div className="form-group row">
<div className="col-3 col-sm-2 col-xl-1">
<select
value={itemsPerPage}
onChange={e => {
setCurrentPage(1);
setItemsForPage(+e.target.value);
}}
data-cy="perPageSelector"
id="perPageSelector"
className="form-control">
className="form-control"
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -32,78 +48,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
total={items.length}
perPage={itemsPerPage}
currentPage={currentPage}
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>
{currentPageItems.map(item => (
<li key={item} data-cy="item">
{item}
</li>
))}
</ul>
</div>
);
Expand Down
89 changes: 88 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
export const Pagination = () => {};
import cn from 'classnames';

type Props = {
total: number;
perPage: number;
currentPage: number;
onPageChange: React.Dispatch<React.SetStateAction<number>>;
};

export const Pagination = ({
total,
perPage,
currentPage,
onPageChange,
}: Props) => {
const pagesCount = Math.ceil(total / perPage);

const paginationItems: number[] = [];

for (let i = 0; i < pagesCount; i++) {
paginationItems.push(i + 1);
}

const isPreviousDisabled = currentPage === 1;
const isNextDisabled = currentPage === pagesCount;

return (
<ul className="pagination">
<li className={cn('page-item', isPreviousDisabled && 'disabled')}>
<a
onClick={() => {
if (isPreviousDisabled) {
return;
}

onPageChange(prev => prev - 1);
}}
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled={isPreviousDisabled}
>
«
</a>
</li>

{paginationItems.map(item => (
<li
key={item}
className={cn('page-item', currentPage === item && 'active')}
>
<a
onClick={() => {
if (currentPage === item) {
return;
}

onPageChange(item);
}}
data-cy="pageLink"
className="page-link"
href={`#${item}`}
>
{item}
</a>
</li>
))}

<li className={cn('page-item', isNextDisabled && 'disabled')}>
<a
onClick={() => {
if (isNextDisabled) {
return;
}

onPageChange(prev => prev + 1);
}}
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={isNextDisabled}
>
»
</a>
</li>
</ul>
);
};
Loading