-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] Improve pagination behavior #11800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+142
−54
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.