Skip to content

Commit d7a8e70

Browse files
committed
refactor(pagination): move logic into custom hook
1 parent 3baba9d commit d7a8e70

File tree

2 files changed

+64
-53
lines changed

2 files changed

+64
-53
lines changed

packages/kit-headless/src/components/pagination/pagination.tsx

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { usePagination } from '@/packages/kit-headless/src/components/pagination/use-pagination';
12
import type { PropFunction } from '@builder.io/qwik';
23
import { component$, Slot, useSignal, useTask$ } from '@builder.io/qwik';
34

@@ -14,50 +15,6 @@ import { component$, Slot, useSignal, useTask$ } from '@builder.io/qwik';
1415
* square
1516
*/
1617

17-
// example generatePaginationArray(20, 6, 1, 1);
18-
function generatePaginationArray(
19-
count: number,
20-
defaultPage: number,
21-
siblingCount = 1,
22-
boundaryCount = 1,
23-
) {
24-
const pageCount = Math.min(count, Math.max(1, count));
25-
const page = Math.min(Math.max(1, defaultPage), pageCount);
26-
27-
const range = (start: number, end: number) =>
28-
Array.from({ length: end - start + 1 }, (_, i) => start + i);
29-
30-
const leftBoundary = range(1, Math.min(boundaryCount, pageCount));
31-
const rightBoundary = range(pageCount - boundaryCount + 1, pageCount);
32-
const innerRange = range(
33-
Math.max(page - siblingCount, boundaryCount + 1),
34-
Math.min(page + siblingCount, pageCount - boundaryCount),
35-
);
36-
37-
const buttons = [...leftBoundary, ...innerRange, ...rightBoundary];
38-
39-
// Add "..." for gaps between buttons
40-
const paginationArray = buttons.reduce(
41-
(result: (number | string)[], button: number, index, array) => {
42-
if (index === 0) {
43-
result.push(button);
44-
} else {
45-
const prevButton = array[index - 1];
46-
if (button - prevButton === 2) {
47-
result.push(prevButton + 1);
48-
} else if (button - prevButton > 2) {
49-
result.push('...');
50-
}
51-
result.push(button);
52-
}
53-
return result;
54-
},
55-
[],
56-
);
57-
58-
return paginationArray;
59-
}
60-
6118
export interface PaginationProps {
6219
class?: string;
6320
selectedPage: number;
@@ -89,15 +46,12 @@ export const Pagination = component$<PaginationProps>(
8946
}) => {
9047
const visibleItems = useSignal<(string | number)[]>([]);
9148

92-
useTask$(({ track }) => {
93-
track(() => [selectedPage, totalPages, siblingCount, boundaryCount]);
94-
visibleItems.value = generatePaginationArray(
95-
totalPages,
96-
selectedPage,
97-
siblingCount,
98-
boundaryCount,
99-
);
100-
});
49+
visibleItems.value = usePagination(
50+
totalPages,
51+
selectedPage,
52+
siblingCount,
53+
boundaryCount,
54+
);
10155

10256
return (
10357
<nav
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// example: usePagination(20, 6, 1, 1);
2+
3+
export function usePagination(
4+
count: number,
5+
defaultPage: number,
6+
siblingCount = 1,
7+
boundaryCount = 1,
8+
) {
9+
const pageCount = Math.min(count, Math.max(1, count));
10+
const page = Math.min(Math.max(1, defaultPage), pageCount);
11+
12+
const range = (start: number, end: number) =>
13+
Array.from({ length: end - start + 1 }, (_, i) => start + i);
14+
15+
const leftBoundary = range(1, Math.min(boundaryCount, pageCount));
16+
const rightBoundary = range(pageCount - boundaryCount + 1, pageCount);
17+
const innerRange = range(
18+
Math.max(page - siblingCount, boundaryCount + 1),
19+
Math.min(page + siblingCount, pageCount - boundaryCount),
20+
);
21+
22+
const buttons = [...leftBoundary, ...innerRange, ...rightBoundary];
23+
24+
// Add "..." for gaps between buttons
25+
const paginationArray = buttons.reduce(
26+
(result: (number | string)[], button: number, index, array) => {
27+
if (index === 0) {
28+
result.push(button);
29+
} else {
30+
const prevButton = array[index - 1];
31+
if (button - prevButton === 2) {
32+
result.push(prevButton + 1);
33+
} else if (button - prevButton > 2) {
34+
result.push('...');
35+
}
36+
result.push(button);
37+
}
38+
return result;
39+
},
40+
[],
41+
);
42+
43+
return paginationArray;
44+
}
45+
46+
/*
47+
useTask$(({ track }) => {
48+
track(() => [selectedPage, totalPages, siblingCount, boundaryCount]);
49+
50+
visibleItems.value = generatePaginationArray(
51+
totalPages,
52+
selectedPage,
53+
siblingCount,
54+
boundaryCount,
55+
);
56+
});
57+
*/

0 commit comments

Comments
 (0)