diff --git a/package-lock.json b/package-lock.json index bc04a7602..7fed12e13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,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", @@ -1183,10 +1183,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.9.12", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.9.12.tgz", - "integrity": "sha512-/OcmxMa34lYLFlGx7Ig926W1U1qjrnXbjFJ2TzUcDaLmED+A5se652NcWwGOidXRuMAOYLPU2jNYBEkKyXrFJA==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", diff --git a/package.json b/package.json index f412fefe6..617386d19 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/App.tsx b/src/App.tsx index 189a990c8..642456d71 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,109 +1,39 @@ -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 [currentPage, setCurrentPage] = useState(1); + const [perPage, setPerPage] = useState(5); + const visibleItems = items.slice( + (currentPage - 1) * perPage, + (currentPage - 1) * perPage + perPage, + ); + return (

Items with Pagination

- -

- Page 1 (items 1 - 5 of 42) -

- -
-
- -
- - -
- {/* Move this markup to Pagination */} - + { + setPerPage(value); + setCurrentPage(1); + }} + />
); diff --git a/src/components/Pagination/Pagination.tsx b/src/components/Pagination/Pagination.tsx index e417a09fc..a18737f33 100644 --- a/src/components/Pagination/Pagination.tsx +++ b/src/components/Pagination/Pagination.tsx @@ -1 +1,81 @@ -export const Pagination = () => {}; +import React from 'react'; + +type PaginationProps = { + total: number; + perPage: number; + currentPage?: number; + onPageChange: (page: number) => void; + onPerPageChange: (perPage: number) => void; +}; + +export const Pagination: React.FC = ({ + total, + perPage, + currentPage = 1, + onPageChange, + onPerPageChange, +}) => { + const totalPages = Math.ceil(total / perPage); + const pages = Array.from({ length: totalPages }, (_, i) => i + 1); + const firstItem = (currentPage - 1) * perPage + 1; + const lastItem = Math.min(firstItem + perPage - 1, total); + + return ( + <> + +
+ Page {currentPage} (items {firstItem} - {lastItem} of {total}) +
+ + + ); +};