Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/Pagination/Pagination.tsx
2500 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { getPaginationNumbers } from "./getPagination";
8
import PaginationNavigationButton from "./PaginationNavigationButton";
9
10
interface PaginationProps {
11
totalNumberOfPages: number;
12
currentPage: number;
13
setPage: (page: number) => void;
14
}
15
16
function Pagination(props: PaginationProps) {
17
const { totalNumberOfPages, setPage } = props;
18
if (totalNumberOfPages <= 1 || props.currentPage < 1) {
19
return <></>;
20
}
21
const currentPage = Math.min(totalNumberOfPages, props.currentPage);
22
const calculatedPagination = getPaginationNumbers(totalNumberOfPages, currentPage);
23
24
const nextPage = () => {
25
if (currentPage !== totalNumberOfPages) setPage(currentPage + 1);
26
};
27
const prevPage = () => {
28
if (currentPage !== 1) setPage(currentPage - 1);
29
};
30
const getClassnames = (pageNumber: string | number) => {
31
if (pageNumber === currentPage) {
32
return "font-semibold text-gray-500 dark:text-gray-400 max-h-9 max-w-8 flex items-center justify-center rounded-md hover:bg-gray-50 dark:hover:bg-gray-800 dark:bg-gray-700 bg-gray-100 disabled pointer-events-none px-3 py-2";
33
}
34
if (pageNumber === "...") {
35
return "font-semibold text-gray-500 dark:text-gray-400 max-h-9 max-w-8 flex items-center justify-center rounded-md hover:bg-gray-50 dark:hover:bg-gray-800 disabled pointer-events-none px-3 py-2";
36
}
37
return "font-semibold text-gray-500 dark:text-gray-400 max-h-9 max-w-8 flex items-center justify-center rounded-md hover:bg-gray-50 dark:hover:bg-gray-800 cursor-pointer px-3 py-2";
38
};
39
40
return (
41
<nav className="mt-16 mb-16">
42
<ul className="flex justify-center items-center space-x-4">
43
<PaginationNavigationButton
44
isDisabled={currentPage === 1}
45
onClick={prevPage}
46
label={"Previous"}
47
arrowDirection={"left"}
48
/>
49
{calculatedPagination.map((pn, i) => {
50
if (pn === "...") {
51
return (
52
<li key={i} className={getClassnames(pn)}>
53
&#8230;
54
</li>
55
);
56
}
57
return (
58
<li key={i} className={getClassnames(pn)} onClick={() => typeof pn === "number" && setPage(pn)}>
59
<span>{pn}</span>
60
</li>
61
);
62
})}
63
<PaginationNavigationButton
64
isDisabled={currentPage === totalNumberOfPages}
65
onClick={nextPage}
66
label={"Next"}
67
arrowDirection={"right"}
68
/>
69
</ul>
70
</nav>
71
);
72
}
73
74
export default Pagination;
75
76