@angelineuniverse/design
Version:
Angeline Design UI Library
56 lines (48 loc) • 1.94 kB
text/typescript
function range(start: number, end: number) {
let length = end - start + 1;
return Array.from({ length }, (_, idx) => idx + start);
}
export const dots = "......";
export function paginateController(totalPage: number,currentPage:number,siblingCount = 1) {
// Pages count is determined as siblingCount + firstPage + lastPage + currentPage + 2*DOTS
const totalPageNumbers = siblingCount + 5;
/*
If the number of pages is less than the page numbers we want to show in our
paginationComponent, we return the range [1..totalPage]
*/
if (totalPageNumbers >= totalPage) {
return range(1, totalPage);
}
const leftSiblingIndex = Math.max(currentPage - siblingCount, 1);
const rightSiblingIndex = Math.min(
currentPage + siblingCount,
totalPage
);
/*
We do not want to show dots if there is only one position left
after/before the left/right page count as that would lead to a change if our Pagination
component size which we do not want
*/
const shouldShowLeftDots = leftSiblingIndex > 2;
const shouldShowRightDots = rightSiblingIndex < totalPage - 2;
const firstPageIndex = 1;
const lastPageIndex = totalPage;
if (!shouldShowLeftDots && shouldShowRightDots) {
let leftItemCount = 3 + 2 * siblingCount;
let leftRange = range(1, leftItemCount);
return [...leftRange, dots, totalPage];
}
if (shouldShowLeftDots && !shouldShowRightDots) {
let rightItemCount = 3 + 2 * siblingCount;
let rightRange = range(
totalPage - rightItemCount + 1,
totalPage
);
return [firstPageIndex, dots, ...rightRange];
}
if (shouldShowLeftDots && shouldShowRightDots) {
let middleRange = range(leftSiblingIndex, rightSiblingIndex);
return [firstPageIndex, dots, ...middleRange, dots, lastPageIndex];
}
return true;
}