import { __ } from "@wordpress/i18n"; import { useTableStore } from "../../store"; import { getTotalPages } from "../../pagination"; import { isProAvailable } from "../../pro-status"; interface PaginationNavigationProps { filteredRowCount?: number; } export function PaginationNavigation({ filteredRowCount, }: PaginationNavigationProps) { const isPro = isProAvailable(); const currentPage = useTableStore(state => state.currentPage); const setCurrentPage = useTableStore(state => state.setCurrentPage); const tableConfig = useTableStore(state => state.table); const paginationConfig = tableConfig.pagination!; const { enabled, pageSize, showPageNumbers, showPrevNext } = paginationConfig; if (!isPro || !enabled) { return null; } const rowCount = filteredRowCount ?? tableConfig.rows; const totalPages = getTotalPages( rowCount, pageSize, tableConfig.headerEnabled, tableConfig.footerEnabled ); if (totalPages <= 1) { return null; } const canGoPrev = currentPage > 0; const canGoNext = currentPage < totalPages - 1; const getPageNumbers = () => { const pages: (number | "ellipsis")[] = []; const maxVisiblePages = 5; if (totalPages <= maxVisiblePages) { for (let i = 0; i < totalPages; i++) { pages.push(i); } } else { pages.push(0); if (currentPage > 2) { pages.push("ellipsis"); } const start = Math.max(1, currentPage - 1); const end = Math.min(totalPages - 2, currentPage + 1); for (let i = start; i <= end; i++) { if (!pages.includes(i)) { pages.push(i); } } if (currentPage < totalPages - 3) { pages.push("ellipsis"); } if (!pages.includes(totalPages - 1)) { pages.push(totalPages - 1); } } return pages; }; const buttonClass = "tableberg-pagination__button"; return (
{showPrevNext && ( )} {showPageNumbers && getPageNumbers().map((page, index) => { if (page === "ellipsis") { return ( ... ); } const isActive = currentPage === page; return ( ); })} {showPrevNext && ( )}
); }