diff --git a/src/App.tsx b/src/App.tsx
index 189a990c8..946a5f872 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,17 +1,22 @@
import React 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] = React.useState(1);
+ const [perPage, setPerPage] = React.useState(5);
+
return (
Items with Pagination
- Page 1 (items 1 - 5 of 42)
+ Page {currentPage} (items {(currentPage - 1) * perPage + 1} -{' '}
+ {Math.min(currentPage * perPage, items.length)} of {items.length})
@@ -19,7 +24,13 @@ export const App: React.FC = () => {
{/* Move this markup to Pagination */}
-
+
+
- - Item 1
- - Item 2
- - Item 3
- - Item 4
- - Item 5
+ {items
+ .slice((currentPage - 1) * perPage, currentPage * perPage)
+ .map(item => {
+ return (
+ -
+ {item}
+
+ );
+ })}
);
diff --git a/src/components/Pagination/Pagination.tsx b/src/components/Pagination/Pagination.tsx
index e417a09fc..b26779141 100644
--- a/src/components/Pagination/Pagination.tsx
+++ b/src/components/Pagination/Pagination.tsx
@@ -1 +1,90 @@
-export const Pagination = () => {};
+import cn from 'classnames';
+import { getNumbers } from '../../utils';
+
+type Props = {
+ total: number;
+ perPage: number;
+ currentPage?: number;
+ onPageChange: (page: number) => void;
+};
+
+export const Pagination = ({
+ total,
+ perPage,
+ currentPage = 1,
+ onPageChange,
+}: Props) => {
+ const pagesCount = Math.ceil(total / perPage);
+ const items = getNumbers(1, pagesCount);
+
+ const handlePrevClick = (event: React.MouseEvent) => {
+ event.preventDefault();
+ if (currentPage && currentPage > 1) {
+ onPageChange(currentPage - 1);
+ }
+ };
+
+ const handleNextClick = (event: React.MouseEvent) => {
+ event.preventDefault();
+ if (currentPage && currentPage < pagesCount) {
+ onPageChange(currentPage + 1);
+ }
+ };
+
+ const handlePageClick = (
+ event: React.MouseEvent,
+ pageNumber: number,
+ ) => {
+ event.preventDefault();
+
+ if (pageNumber !== currentPage) {
+ onPageChange(pageNumber);
+ }
+ };
+
+ return (
+
+ );
+};