PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.2.0
Tableberg – Simple Gutenberg Table Block v1.2.0
1.2.0 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 All 43 releases
tableberg / src / components / PaginationNavigation / index.tsx

index.tsx in Tableberg – Simple Gutenberg Table Block 1.2.0, at src/components/PaginationNavigation/index.tsx

148 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from "@wordpress/i18n";
2 import { useTableStore } from "../../store";
3 import { getTotalPages } from "../../pagination";
4 import { isProAvailable } from "../../pro-status";
5
6 interface PaginationNavigationProps {
7 filteredRowCount?: number;
8 }
9
10 export function PaginationNavigation({
11 filteredRowCount,
12 }: PaginationNavigationProps) {
13 const isPro = isProAvailable();
14 const currentPage = useTableStore(state => state.currentPage);
15 const setCurrentPage = useTableStore(state => state.setCurrentPage);
16 const tableConfig = useTableStore(state => state.table);
17
18 const paginationConfig = tableConfig.pagination!;
19 const { enabled, pageSize, showPageNumbers, showPrevNext } =
20 paginationConfig;
21
22 if (!isPro || !enabled) {
23 return null;
24 }
25
26 const rowCount = filteredRowCount ?? tableConfig.rows;
27 const totalPages = getTotalPages(
28 rowCount,
29 pageSize,
30 tableConfig.headerEnabled,
31 tableConfig.footerEnabled
32 );
33
34 if (totalPages <= 1) {
35 return null;
36 }
37
38 const canGoPrev = currentPage > 0;
39 const canGoNext = currentPage < totalPages - 1;
40
41 const getPageNumbers = () => {
42 const pages: (number | "ellipsis")[] = [];
43 const maxVisiblePages = 5;
44
45 if (totalPages <= maxVisiblePages) {
46 for (let i = 0; i < totalPages; i++) {
47 pages.push(i);
48 }
49 } else {
50 pages.push(0);
51
52 if (currentPage > 2) {
53 pages.push("ellipsis");
54 }
55
56 const start = Math.max(1, currentPage - 1);
57 const end = Math.min(totalPages - 2, currentPage + 1);
58
59 for (let i = start; i <= end; i++) {
60 if (!pages.includes(i)) {
61 pages.push(i);
62 }
63 }
64
65 if (currentPage < totalPages - 3) {
66 pages.push("ellipsis");
67 }
68
69 if (!pages.includes(totalPages - 1)) {
70 pages.push(totalPages - 1);
71 }
72 }
73
74 return pages;
75 };
76
77 const buttonClass = "tableberg-pagination__button";
78
79 return (
80 <div className="tableberg-pagination">
81 {showPrevNext && (
82 <button
83 type="button"
84 disabled={!canGoPrev}
85 onClick={() => setCurrentPage(currentPage - 1)}
86 className={`${buttonClass} ${buttonClass}--arrow`}
87 aria-label={__("Previous Page", "tableberg")}
88 >
89 <svg
90 width="16"
91 height="16"
92 viewBox="0 0 24 24"
93 fill="currentColor"
94 >
95 <path d="M14.6 7L10 12l4.6 5-1.4 1.4L6.8 12l6.4-6.4z" />
96 </svg>
97 </button>
98 )}
99
100 {showPageNumbers &&
101 getPageNumbers().map((page, index) => {
102 if (page === "ellipsis") {
103 return (
104 <span
105 key={`ellipsis-${index}`}
106 className="tableberg-pagination__ellipsis"
107 >
108 ...
109 </span>
110 );
111 }
112
113 const isActive = currentPage === page;
114
115 return (
116 <button
117 key={page}
118 type="button"
119 onClick={() => setCurrentPage(page)}
120 className={`${buttonClass}${isActive ? ` ${buttonClass}--active` : ""}`}
121 >
122 {page + 1}
123 </button>
124 );
125 })}
126
127 {showPrevNext && (
128 <button
129 type="button"
130 disabled={!canGoNext}
131 onClick={() => setCurrentPage(currentPage + 1)}
132 className={`${buttonClass} ${buttonClass}--arrow`}
133 aria-label={__("Next Page", "tableberg")}
134 >
135 <svg
136 width="16"
137 height="16"
138 viewBox="0 0 24 24"
139 fill="currentColor"
140 >
141 <path d="M9.4 7l4.6 5-4.6 5 1.4 1.4 6-6.4-6-6.4z" />
142 </svg>
143 </button>
144 )}
145 </div>
146 );
147 }
148