Skip to content

[dashboard] (pagination) make sure next page is selectable #12809

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
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions components/dashboard/src/Pagination/getPagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@
import { getPaginationNumbers } from "./getPagination";

test("getPagination", () => {
const totalNumberOfPages = 15;
const currentPage = 1;
expect(getPaginationNumbers(15, 1)).toStrictEqual([1, 2, 3, "...", 15]);

expect(getPaginationNumbers(totalNumberOfPages, currentPage)).toStrictEqual([1, 2, 3, "...", 15]);

expect(getPaginationNumbers(37, 4)).toStrictEqual([1, "...", 3, 4, 5, "...", 37]);
expect(getPaginationNumbers(37, 4)).toStrictEqual([1, 2, 3, 4, 5, "...", 37]);

// navigating 7 --> 4
// ensure there is a selectable page next to current
expect(getPaginationNumbers(28, 7)).toStrictEqual([1, "...", 6, 7, 8, "...", 28]);
expect(getPaginationNumbers(28, 6)).toStrictEqual([1, "...", 5, 6, 7, "...", 28]);
expect(getPaginationNumbers(28, 5)).toStrictEqual([1, "...", 4, 5, 6, "...", 28]);
// ellipsis should not replace a single page number
expect(getPaginationNumbers(28, 4)).toStrictEqual([1, 2, 3, 4, 5, "...", 28]);

// ensure there is a selectable page next to current
expect(getPaginationNumbers(28, 24)).toStrictEqual([1, "...", 23, 24, 25, "...", 28]);
expect(getPaginationNumbers(28, 25)).toStrictEqual([1, "...", 24, 25, 26, 27, 28]);

expect(getPaginationNumbers(5, 1)).toStrictEqual([1, 2, 3, 4, 5]);

expect(getPaginationNumbers(9, 2)).toStrictEqual([1, 2, 3, "...", 9]);
expect(getPaginationNumbers(9, 3)).toStrictEqual([1, 2, 3, 4, "...", 9]);
expect(getPaginationNumbers(6, 3)).toStrictEqual([1, 2, 3, 4, 5, 6]);
expect(getPaginationNumbers(7, 3)).toStrictEqual([1, 2, 3, 4, "...", 7]);
expect(getPaginationNumbers(7, 3)).toStrictEqual([1, 2, 3, 4, "...", 7]);

expect(getPaginationNumbers(10, 8)).toStrictEqual([1, "...", 7, 8, 9, 10]);
expect(getPaginationNumbers(10, 7)).toStrictEqual([1, "...", 6, 7, 8, 9, 10]);
});
12 changes: 9 additions & 3 deletions components/dashboard/src/Pagination/getPagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

export function getPaginationNumbers(totalNumberOfPages: number, currentPage: number) {
const adjacentToCurrentPage = 1; // This is the number(s) we see next to the currentPage
const numberOfPagesToShowOnTheSide = 3;
const totalNumbersShownInPagination = 6;
let calculatedPagination: number[] = [];

Expand All @@ -24,16 +25,21 @@ export function getPaginationNumbers(totalNumberOfPages: number, currentPage: nu
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"
const showLeftEllipsis =
currentPage > numberOfPagesToShowOnTheSide + adjacentToCurrentPage &&
toTheLeftOfCurrent > minimumAmountInBetweenToShowEllipsis; // e.g. 1 ... 5 6 7"

if (showRightEllipsis && !showLeftEllipsis) {
let leftSideNumbers = 3;
let leftSideNumbers = Math.max(numberOfPagesToShowOnTheSide, currentPage + adjacentToCurrentPage);
let leftPageNumbersAsArray = pageNumbersAsArray(1, leftSideNumbers);
return [...leftPageNumbersAsArray, "...", totalNumberOfPages];
}

if (showLeftEllipsis && !showRightEllipsis) {
let rightSideNumbers = 3;
let rightSideNumbers = Math.max(
numberOfPagesToShowOnTheSide,
totalNumberOfPages - currentPage + adjacentToCurrentPage,
);
let rightPageNumbersAsArray = pageNumbersAsArray(totalNumberOfPages - rightSideNumbers, totalNumberOfPages);
return [1, "...", ...rightPageNumbersAsArray];
}
Expand Down