Skip to content

Commit 146fa2d

Browse files
committed
Adjustable starting pages
1 parent 8afb94c commit 146fa2d

3 files changed

Lines changed: 97 additions & 12 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { calculatePagination } from '../index'
2+
3+
it('should return no pagination if not enough items are given', () => {
4+
const page = 0
5+
const size = 5
6+
const total = 2
7+
const pagination = calculatePagination(page, size, total, 5, 0)
8+
9+
expect(pagination).toBeNull()
10+
})
11+
it('should return only a few pages', () => {
12+
const page = 1
13+
const size = 5
14+
const total = 15
15+
const pagination = calculatePagination(page, size, total, 5, 0)
16+
17+
expect(pagination).not.toBeNull()
18+
expect(pagination?.pages).toHaveLength(3)
19+
20+
expect(pagination?.pages[0].number).toBe(0)
21+
expect(pagination?.pages[0].isCurrent).toBeFalsy()
22+
expect(pagination?.pages[1].number).toBe(1)
23+
expect(pagination?.pages[1].isCurrent).toBeTruthy()
24+
expect(pagination?.pages[2].number).toBe(2)
25+
expect(pagination?.pages[2].isCurrent).toBeFalsy()
26+
})
27+
it('should return the maximum number of pages', () => {
28+
const page = 4
29+
const size = 5
30+
const total = 50
31+
const maxPages = 5
32+
const pagination = calculatePagination(page, size, total, maxPages, 0)
33+
34+
expect(pagination).not.toBeNull()
35+
expect(pagination?.pages).toHaveLength(5)
36+
37+
expect(pagination?.pages[0].number).toBe(2)
38+
expect(pagination?.pages[0].isCurrent).toBeFalsy()
39+
expect(pagination?.pages[1].number).toBe(3)
40+
expect(pagination?.pages[1].isCurrent).toBeFalsy()
41+
expect(pagination?.pages[2].number).toBe(4)
42+
expect(pagination?.pages[2].isCurrent).toBeTruthy()
43+
expect(pagination?.pages[3].number).toBe(5)
44+
expect(pagination?.pages[3].isCurrent).toBeFalsy()
45+
expect(pagination?.pages[4].number).toBe(6)
46+
expect(pagination?.pages[4].isCurrent).toBeFalsy()
47+
})
48+
it('should disable the previous link', () => {
49+
const page = 0
50+
const size = 5
51+
const total = 10
52+
const pagination = calculatePagination(page, size, total, 5, 0)
53+
54+
expect(pagination).not.toBeNull()
55+
56+
expect(pagination?.previous.number).toBe(0)
57+
expect(pagination?.previous.isDisabled).toBeTruthy()
58+
59+
expect(pagination?.next.number).toBe(1)
60+
expect(pagination?.next.isDisabled).toBeFalsy()
61+
62+
expect(pagination?.pages[0].isCurrent).toBeTruthy()
63+
expect(pagination?.pages[1].isCurrent).toBeFalsy()
64+
})
65+
it('should disable the next link', () => {
66+
const page = 1
67+
const size = 5
68+
const total = 10
69+
const pagination = calculatePagination(page, size, total, 5, 0)
70+
71+
expect(pagination).not.toBeNull()
72+
73+
expect(pagination?.previous.number).toBe(0)
74+
expect(pagination?.previous.isDisabled).toBeFalsy()
75+
76+
expect(pagination?.next.number).toBe(1)
77+
expect(pagination?.next.isDisabled).toBeTruthy()
78+
79+
expect(pagination?.pages[0].isCurrent).toBeFalsy()
80+
expect(pagination?.pages[1].isCurrent).toBeTruthy()
81+
})
File renamed without changes.

src/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const calculateVisiblePages = (
22
page: number,
3-
total: number,
3+
firstPage: number,
4+
lastPage: number,
45
maxPages: number
56
): number[] => {
67
const range = Math.floor(maxPages / 2)
7-
let lowerBound = 1
8+
let lowerBound = firstPage
89
let lowerBoundRest = 0
9-
let upperBound = total
10+
let upperBound = lastPage
1011
let upperBoundRest = 0
1112

1213
if (page - range >= 1) {
@@ -15,20 +16,22 @@ const calculateVisiblePages = (
1516
lowerBoundRest = (page - range - 1) * -1
1617
}
1718

18-
if (page + range <= total) {
19+
if (page + range <= lastPage) {
1920
upperBound = page + range
2021
} else {
21-
upperBoundRest = (total - range - page) * -1
22+
upperBoundRest = (lastPage - range - page) * -1
2223
}
2324

2425
if (upperBoundRest > 0) {
2526
lowerBound =
26-
lowerBound - upperBoundRest > 0 ? lowerBound - upperBoundRest : 1
27+
lowerBound - upperBoundRest > 0 ? lowerBound - upperBoundRest : firstPage
2728
}
2829

2930
if (lowerBoundRest > 0) {
3031
upperBound =
31-
upperBound + lowerBoundRest < total ? upperBound + lowerBoundRest : total
32+
upperBound + lowerBoundRest < lastPage
33+
? upperBound + lowerBoundRest
34+
: lastPage
3235
}
3336

3437
const pages = []
@@ -44,7 +47,8 @@ const calculatePagination = (
4447
page: number,
4548
size: number,
4649
total: number,
47-
maxPages = 5
50+
maxPages = 5,
51+
firstPage = 1
4852
): {
4953
previous: {
5054
number: number
@@ -63,20 +67,20 @@ const calculatePagination = (
6367
return null
6468
}
6569

66-
const lastPage = Math.ceil(total / size)
67-
const isCurrentTheFirstPage = page === 1
70+
const lastPage = Math.ceil(total / size) + (firstPage - 1)
71+
const isCurrentTheFirstPage = page === firstPage
6872
const isCurrentTheLastPage = page === lastPage
6973

7074
return {
7175
previous: {
72-
number: isCurrentTheFirstPage ? 1 : page - 1,
76+
number: isCurrentTheFirstPage ? firstPage : page - 1,
7377
isDisabled: isCurrentTheFirstPage,
7478
},
7579
next: {
7680
number: isCurrentTheLastPage ? lastPage : page + 1,
7781
isDisabled: isCurrentTheLastPage,
7882
},
79-
pages: calculateVisiblePages(page, lastPage, maxPages).map(
83+
pages: calculateVisiblePages(page, firstPage, lastPage, maxPages).map(
8084
(visiblePage) => {
8185
return {
8286
number: visiblePage,

0 commit comments

Comments
 (0)