import { useMemo, useState } from 'react'; import { IconChevronLeft, IconChevronRight, IconChevronsLeft, IconChevronsRight } from '@tabler/icons-react'; /** * Улучшенный компонент пагинации * Добавлено: Jump to Page, иконки, улучшенная доступность */ function Pagination({ currentPage, totalPages, totalItems, pageSize, onPageChange }) { const [jumpToPage, setJumpToPage] = useState(''); // Хуки должны вызываться безусловно (до любого return) const pages = useMemo(() => { if (totalPages <= 1) return []; const result = []; let start = Math.max(1, currentPage - 2); let end = Math.min(totalPages, currentPage + 2); if (currentPage <= 3) { end = Math.min(totalPages, 5); } if (currentPage >= totalPages - 2) { start = Math.max(1, totalPages - 4); } if (start > 1) result.push({ type: 'ellipsis', key: 'start-ellipsis' }); for (let p = start; p <= end; p++) { result.push({ type: 'page', page: p, key: p }); } if (end < totalPages) result.push({ type: 'ellipsis', key: 'end-ellipsis' }); return result; }, [currentPage, totalPages]); // Ранний return после всех хуков if (totalPages <= 1) return null; const startItem = (currentPage - 1) * pageSize + 1; const endItem = Math.min(currentPage * pageSize, totalItems); const handleJumpToPage = (e) => { e.preventDefault(); const pageNum = parseInt(jumpToPage, 10); if (pageNum >= 1 && pageNum <= totalPages) { onPageChange(pageNum); setJumpToPage(''); } }; return (
Показано {startItem} - {endItem} из {totalItems}
{/* Jump to page */} {totalPages > 10 && (
setJumpToPage(e.target.value)} placeholder={currentPage.toString()} className="form-control form-control-sm" style={{ width: '60px' }} aria-label="Перейти на страницу" />
)} {/* Pagination controls */}
); } export default Pagination;