Skip to content
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

fix(Pagination): add conditions for 2 siblings edge case #1024

Merged
merged 2 commits into from
Jun 23, 2024
Merged
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
22 changes: 19 additions & 3 deletions packages/radix-vue/src/Pagination/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export function getRange(currentPage: number, pageCount: number, siblingCount: n
const leftSiblingIndex = Math.max(currentPage - siblingCount, firstPageIndex)
const rightSiblingIndex = Math.min(currentPage + siblingCount, lastPageIndex)

const showLeftEllipsis = leftSiblingIndex > firstPageIndex + 2
const showRightEllipsis = rightSiblingIndex < lastPageIndex - 2

if (showEdges) {
/**
* `2 * siblingCount + 5` explanation:
Expand All @@ -41,18 +38,37 @@ export function getRange(currentPage: number, pageCount: number, siblingCount: n

const itemCount = totalPageNumbers - 2 // 2 stands for one ellipsis and either first or last page

const showLeftEllipsis
// default condition
= leftSiblingIndex > firstPageIndex + 2
// if the current page is towards the end of the list
&& Math.abs(lastPageIndex - itemCount - firstPageIndex + 1) > 2
// if the current page is towards the middle of the list
&& Math.abs(leftSiblingIndex - firstPageIndex) > 2

const showRightEllipsis
// default condition
= rightSiblingIndex < lastPageIndex - 2
// if the current page is towards the start of the list
&& Math.abs(lastPageIndex - itemCount) > 2
// if the current page is towards the middle of the list
&& Math.abs(lastPageIndex - rightSiblingIndex) > 2

if (!showLeftEllipsis && showRightEllipsis) {
const leftRange = range(1, itemCount)

return [...leftRange, ELLIPSIS, lastPageIndex]
}

if (showLeftEllipsis && !showRightEllipsis) {
const rightRange = range(lastPageIndex - itemCount + 1, lastPageIndex)

return [firstPageIndex, ELLIPSIS, ...rightRange]
}

if (showLeftEllipsis && showRightEllipsis) {
const middleRange = range(leftSiblingIndex, rightSiblingIndex)

return [firstPageIndex, ELLIPSIS, ...middleRange, ELLIPSIS, lastPageIndex]
}

Expand Down
Loading