1
+ import { usePagination } from '@/packages/kit-headless/src/components/pagination/use-pagination' ;
1
2
import type { PropFunction } from '@builder.io/qwik' ;
2
3
import { component$ , Slot , useSignal , useTask$ } from '@builder.io/qwik' ;
3
4
@@ -14,50 +15,6 @@ import { component$, Slot, useSignal, useTask$ } from '@builder.io/qwik';
14
15
* square
15
16
*/
16
17
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
-
61
18
export interface PaginationProps {
62
19
class ?: string ;
63
20
selectedPage : number ;
@@ -89,15 +46,12 @@ export const Pagination = component$<PaginationProps>(
89
46
} ) => {
90
47
const visibleItems = useSignal < ( string | number ) [ ] > ( [ ] ) ;
91
48
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
+ ) ;
101
55
102
56
return (
103
57
< nav
0 commit comments