-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPaginationRange.ts
39 lines (32 loc) · 1.13 KB
/
getPaginationRange.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { range } from './range'
const DOTS = 'dots'
export default function getPaginationRange(
total: number,
activePage: number,
boundaries = 1,
siblings = 1
) {
const totalPageNumbers = siblings * 2 + 3 + boundaries * 2
if (totalPageNumbers >= total) {
return range(1, total)
}
const leftSiblingIndex = Math.max(activePage - siblings, boundaries)
const rightSiblingIndex = Math.min(activePage + siblings, total - boundaries)
const shouldShowLeftDots = leftSiblingIndex > boundaries + 2
const shouldShowRightDots = rightSiblingIndex < total - (boundaries + 1)
if (!shouldShowLeftDots && shouldShowRightDots) {
const leftItemCount = siblings * 2 + boundaries + 2
return [...range(1, leftItemCount), DOTS, ...range(total - (boundaries - 1), total)]
}
if (shouldShowLeftDots && !shouldShowRightDots) {
const rightItemCount = boundaries + 1 + 2 * siblings
return [...range(1, boundaries), DOTS, ...range(total - rightItemCount, total)]
}
return [
...range(1, boundaries),
DOTS,
...range(leftSiblingIndex, rightSiblingIndex),
DOTS,
...range(total - boundaries + 1, total),
]
}