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
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
108 changes: 32 additions & 76 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
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
type PageSize = 3 | 5 | 10 | 20;
const items = getNumbers(1, 42).map(n => `Item ${n}`);
const total = items.length;

export const App: React.FC = () => {
const [itemsPerPage, setItemsPerPage] = useState<PageSize>(5);
const [currentPage, setCurrentPage] = useState<number>(1);

const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = Math.min(startIndex + itemsPerPage, total);
const visibleItems = items.slice(startIndex, endIndex);

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 ${startIndex + 1} - ${endIndex} of ${total})`}
</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">
className="form-control"
value={itemsPerPage}
onChange={event => {
setItemsPerPage(Number(event.target.value) as PageSize);
setCurrentPage(1);
}}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -31,79 +46,20 @@ 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={itemsPerPage}
currentPage={currentPage}
onPageChange={(page: number) => {
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>
{visibleItems.map((item: string) => (
<li data-cy="item" key={item}>
{item}
</li>
))}
</ul>
</div>
);
Expand Down
94 changes: 93 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,93 @@
export const Pagination = () => {};
import React from 'react';
import { getNumbers } from '../../utils';
import classNames from 'classnames';

type 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 +12 to +16

Choose a reason for hiding this comment

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

According to the task requirements, currentPage should be an optional prop with a default value of 1. To achieve this, you should first mark it as optional in the PaginationProps type (e.g., currentPage?: number;) and then provide the default value during destructuring in the component signature (e.g., { currentPage = 1, ... }).

}) => {
const pagesCount: number = Math.ceil(total / perPage);
const pagesArr: number[] = getNumbers(1, pagesCount);

const isFirstPage = currentPage === 1;
const isLastPage = currentPage === pagesCount;

return (
<ul className="pagination">
<li
className={classNames('page-item', {
disabled: isFirstPage,
})}
>
<a
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled={isFirstPage}
onClick={event => {
event.preventDefault();
if (!isFirstPage) {
onPageChange(currentPage - 1);
}
}}
>
«
</a>
</li>
{pagesArr.map((page: number) => {
return (
<li
key={page}
className={classNames('page-item', {
active: currentPage === page,
})}
>
<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={event => {
event.preventDefault();

if (page !== currentPage) {
onPageChange(page);
}
}}
>
{page}
</a>
</li>
);
})}
<li
className={classNames('page-item', {
disabled: isLastPage,
})}
>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={isLastPage}
onClick={event => {
event.preventDefault();
if (!isLastPage) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
);
};
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"compilerOptions": {
"jsx": "react"
},
"extends": "@mate-academy/students-ts-config",
"include": [
"src"
Expand Down