diff --git a/src/components/Catalog/Catalog.tsx b/src/components/Catalog/Catalog.tsx index 6604937..a70d69c 100644 --- a/src/components/Catalog/Catalog.tsx +++ b/src/components/Catalog/Catalog.tsx @@ -105,6 +105,38 @@ export const Catalog: React.FC = ({ items, title, isFiltered }) => { updateURL(sortOption, option); }; + const generatePageNumbers = (currentPage: number, totalPages: number) => { + const delta = 2; + const range: number[] = []; + + range.push(1); + + for ( + let i = Math.max(2, currentPage - delta); + i <= Math.min(totalPages - 1, currentPage + delta); + i++ + ) { + range.push(i); + } + + if (totalPages > 1) { + range.push(totalPages); + } + + let result: (number | '...')[] = []; + let lastPage = 0; + + for (let i of range) { + if (i - lastPage > 1) { + result.push('...'); + } + result.push(i); + lastPage = i; + } + + return result; + }; + return ( <>
@@ -128,6 +160,7 @@ export const Catalog: React.FC = ({ items, title, isFiltered }) => {
))} + {itemsPerPage !== 'all' && items.length > itemsPerPage && (
+
- {[...Array(totalPages)].map((_, index) => ( - - ))} + {generatePageNumbers(currentPage, totalPages).map( + (page, index) => + page === '...' ? ( + + ... + + ) : ( + + ), + )}
+