-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
increase page resultsand add clclickability
- Loading branch information
Showing
5 changed files
with
142 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { getPaginationNumbers } from "./getPagination"; | ||
import Arrow from "../components/Arrow"; | ||
|
||
interface PaginationProps { | ||
totalResults: number; | ||
totalNumberOfPages: number; | ||
currentPage: number; | ||
setCurrentPage: any; | ||
} | ||
|
||
function Pagination({ totalNumberOfPages, currentPage, setCurrentPage }: PaginationProps) { | ||
const calculatedPagination = getPaginationNumbers(totalNumberOfPages, currentPage); | ||
|
||
const nextPage = () => { | ||
if (currentPage !== totalNumberOfPages) setCurrentPage(currentPage + 1); | ||
}; | ||
const prevPage = () => { | ||
if (currentPage !== 1) setCurrentPage(currentPage - 1); | ||
}; | ||
const getClassnames = (pageNumber: string | number) => { | ||
if (pageNumber === currentPage) { | ||
return "text-gray-500 w-8 text-center rounded-md hover:bg-gray-50 bg-gray-100 disabled pointer-events-none"; | ||
} | ||
if (pageNumber === "...") { | ||
return "text-gray-500 w-8 text-center rounded-md hover:bg-gray-50 disabled pointer-events-none"; | ||
} | ||
return "text-gray-500 w-8 text-center rounded-md hover:bg-gray-50 cursor-pointer"; | ||
}; | ||
|
||
return ( | ||
<nav className="mt-16 mb-16"> | ||
<ul className="flex justify-center space-x-4"> | ||
<li className={`text-gray-400 ${currentPage === 1 ? "disabled" : "cursor-pointer text-gray-500"}`}> | ||
<span onClick={prevPage}> | ||
<Arrow direction={"left"} /> | ||
Previous | ||
</span> | ||
</li> | ||
{calculatedPagination.map((pn, i) => { | ||
if (pn === "...") { | ||
return <li className={getClassnames(pn)}>…</li>; | ||
} | ||
return ( | ||
<li key={i} className={getClassnames(pn)}> | ||
<span onClick={() => setCurrentPage(pn)}>{pn}</span> | ||
</li> | ||
); | ||
})} | ||
<li | ||
className={`text-gray-400 ${ | ||
currentPage === totalNumberOfPages ? "disabled" : "cursor-pointer text-gray-500" | ||
}`} | ||
> | ||
<span onClick={nextPage}> | ||
Next | ||
<Arrow direction={"right"} /> | ||
</span> | ||
</li> | ||
</ul> | ||
</nav> | ||
); | ||
} | ||
|
||
export default Pagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { getPaginationNumbers } from "./getPagination"; | ||
|
||
test("getPagination", () => { | ||
const totalNumberOfPages = 15; | ||
const currentPage = 1; | ||
|
||
expect(getPaginationNumbers(totalNumberOfPages, currentPage)).toStrictEqual([1, 2, 3, "...", 15]); | ||
|
||
expect(getPaginationNumbers(37, 4)).toStrictEqual([1, "...", 3, 4, 5, "...", 37]); | ||
|
||
expect(getPaginationNumbers(28, 7)).toStrictEqual([1, "...", 6, 7, 8, "...", 28]); | ||
|
||
expect(getPaginationNumbers(5, 1)).toStrictEqual([1, 2, 3, 4, 5]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
export function getPaginationNumbers(totalNumberOfPages: number, currentPage: number) { | ||
const adjacentToCurrentPage = 1; // This is the number(s) we see next to the currentPage | ||
const totalNumbersShownInPagination = 6; | ||
let calculatedPagination: number[] = []; | ||
|
||
const pageNumbersAsArray = (startRange: number, endRange: number) => { | ||
return [...Array(endRange + 1).keys()].slice(startRange); | ||
}; | ||
|
||
const minimumAmountInBetweenToShowEllipsis = 2; | ||
// Without ellipsis aka normal case | ||
if (totalNumberOfPages <= totalNumbersShownInPagination) { | ||
return (calculatedPagination = pageNumbersAsArray(1, totalNumberOfPages)); | ||
} | ||
|
||
// Otherwise, we show the ellipses | ||
const toTheRightOfCurrent = Math.min(currentPage + adjacentToCurrentPage, totalNumberOfPages); | ||
const toTheLeftOfCurrent = Math.max(currentPage - adjacentToCurrentPage, 1); | ||
|
||
const showRightEllipsis = toTheRightOfCurrent < totalNumberOfPages - minimumAmountInBetweenToShowEllipsis; // e.g. "1 2 3 ... 7" | ||
const showLeftEllipsis = toTheLeftOfCurrent > minimumAmountInBetweenToShowEllipsis; // e.g. 1 ... 5 6 7" | ||
|
||
if (showRightEllipsis && !showLeftEllipsis) { | ||
let leftSideNumbers = 3; | ||
let leftPageNumbersAsArray = pageNumbersAsArray(1, leftSideNumbers); | ||
return [...leftPageNumbersAsArray, "...", totalNumberOfPages]; | ||
} | ||
|
||
if (showLeftEllipsis && !showRightEllipsis) { | ||
let rightSideNumbers = 3; | ||
let rightPageNumbersAsArray = pageNumbersAsArray(totalNumberOfPages - rightSideNumbers, totalNumberOfPages); | ||
return [1, "...", ...rightPageNumbersAsArray]; | ||
} | ||
|
||
if (showRightEllipsis && showLeftEllipsis) { | ||
let middleNumbers = pageNumbersAsArray(toTheLeftOfCurrent, toTheRightOfCurrent); | ||
return [1, "...", ...middleNumbers, "...", totalNumberOfPages]; | ||
} | ||
|
||
return calculatedPagination; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters