@ozen-ui/kit
Version:
React component library
42 lines (41 loc) • 2.09 kB
JavaScript
import { __read, __spreadArray } from "tslib";
import { ELLIPSIS } from '../../constants';
import { range } from '../range';
import { calculateSiblingIndexes } from './calculateSiblingIndexes';
import { canHaveDotsOrSiblings } from './canHaveDotsOrSiblings';
import { shouldShowDots } from './shouldShowDots';
var FIRST_PAGE_INDEX = 1;
export var createPaginationRange = function (_a) {
var page = _a.page, pageSize = _a.pageSize, totalCount = _a.totalCount, siblingCount = _a.siblingCount;
var totalPageCount = Math.ceil(totalCount / pageSize);
var lastPageIndex = totalPageCount;
var itemsToShow = 3 + 2 * siblingCount;
if (!canHaveDotsOrSiblings(siblingCount, totalPageCount)) {
return range(FIRST_PAGE_INDEX, lastPageIndex);
}
var _b = calculateSiblingIndexes({ page: page, siblingCount: siblingCount, totalPageCount: totalPageCount }), leftSiblingStartIndex = _b.leftSiblingStartIndex, rightSiblingEndIndex = _b.rightSiblingEndIndex;
var _c = shouldShowDots({
leftSiblingStartIndex: leftSiblingStartIndex,
rightSiblingEndIndex: rightSiblingEndIndex,
totalPageCount: totalPageCount,
}), shouldShowLeftDots = _c.shouldShowLeftDots, shouldShowRightDots = _c.shouldShowRightDots;
if (!shouldShowLeftDots && shouldShowRightDots) {
var leftRange = range(1, itemsToShow);
return __spreadArray(__spreadArray([], __read(leftRange), false), [ELLIPSIS, totalPageCount], false);
}
if (shouldShowLeftDots && !shouldShowRightDots) {
var rightRange = range(totalPageCount - itemsToShow + 1, totalPageCount);
return __spreadArray([FIRST_PAGE_INDEX, ELLIPSIS], __read(rightRange), false);
}
if (shouldShowLeftDots && shouldShowRightDots) {
var middleRange = range(leftSiblingStartIndex, rightSiblingEndIndex);
return __spreadArray(__spreadArray([
FIRST_PAGE_INDEX,
ELLIPSIS
], __read(middleRange), false), [
ELLIPSIS,
lastPageIndex,
], false);
}
return range(FIRST_PAGE_INDEX, lastPageIndex);
};