@@ -17,92 +17,75 @@ import { Button as HeadlessButton } from '@qwik-ui/primitives';
17
17
* size: 'sm' | 'md' | 'lg'
18
18
* paginationRange (see https://mui.com/material-ui/react-pagination/#pagination-ranges)
19
19
*
20
- * EVENTS
21
- * onPageChange
22
- *
23
20
*/
24
- export interface IPaginationProps extends IGetPaginationItemsOptions {
21
+ export interface PaginationProps extends PaginationOptions {
25
22
pages : number ;
26
23
page : number ;
27
24
onPaging$ : PropFunction < ( index : number ) => void > ;
28
- RenderItem ?: Component < IRenderPaginationItemProps > ;
25
+ RenderItem ?: Component < PaginationButtonProps > ;
29
26
RenderDivider ?: Component < object > ;
30
27
}
31
28
32
- export interface IRenderPaginationItemProps {
29
+ export interface PaginationOptions {
30
+ boundaryCount ?: number ;
31
+ siblingCount ?: number ;
32
+ hidePrevButton ?: boolean ;
33
+ hideNextButton ?: boolean ;
34
+ showFirstButton ?: boolean ;
35
+ showLastButton ?: boolean ;
36
+ activeClass ?: string ;
37
+ defaultClass ?: string ;
38
+ labels ?: PaginationButtonLabels ;
39
+ }
40
+
41
+ export interface PaginationButtonProps {
33
42
onClick$ : PropFunction < ( ) => void > ;
34
43
disabled ?: boolean ;
35
44
'aria-label' : string ;
36
45
'aria-current' ?: boolean ;
37
- value : TPaginationItemValue ;
38
46
key ?: string | number ;
39
47
activeClass ?: string ;
40
48
defaultClass ?: string ;
41
- labels ?: TPaginationLabels ;
49
+ value : PaginationButtonValue ;
50
+ labels ?: PaginationButtonLabels ;
42
51
}
43
52
44
- export type TPaginationLabels = {
45
- first ?: string ;
46
- last ?: string ;
47
- next ?: string ;
48
- prev ?: string ;
49
- } ;
50
-
51
- export type TPaginationItemValue =
53
+ export type PaginationButtonValue =
52
54
| 'prev'
53
55
| 'next'
54
- | number
55
- | 'start-ellipsis'
56
- | 'end-ellipsis'
57
56
| 'first'
58
57
| 'last'
58
+ // | 'divider'
59
+ // | 'start-ellipsis'
60
+ // | 'end-ellipsis'
61
+ | number
59
62
| string ;
60
63
64
+ export interface PaginationButtonLabels {
65
+ first ?: string ;
66
+ last ?: string ;
67
+ next ?: string ;
68
+ prev ?: string ;
69
+ }
70
+
61
71
const range = ( start : number , end : number ) => {
62
72
const length = end - start + 1 ;
63
73
return Array . from ( { length } , ( _ , i ) => start + i ) ;
64
74
} ;
65
75
66
- export type TPaginationItem =
67
- | 'first'
68
- | 'last'
69
- | 'prev'
70
- | 'next'
71
- | 'divider'
72
- | number
73
- | string ;
74
-
75
- export interface IGetPaginationItems {
76
- page : number ;
77
- count : number ;
78
- options : IGetPaginationItemsOptions ;
79
- }
80
-
81
- export interface IGetPaginationItemsOptions {
82
- boundaryCount ?: number ;
83
- siblingCount ?: number ;
84
- hidePrevButton ?: boolean ;
85
- hideNextButton ?: boolean ;
86
- showFirstButton ?: boolean ;
87
- showLastButton ?: boolean ;
88
- activeClass ?: string ;
89
- defaultClass ?: string ;
90
- labels ?: TPaginationLabels ;
91
- }
92
-
93
- export const getPaginationItems = (
94
- page : IGetPaginationItems [ 'page' ] ,
95
- count : IGetPaginationItems [ 'count' ] ,
96
- labels : TPaginationLabels | undefined ,
76
+ export const getPaginationButtons = (
77
+ page : number ,
78
+ count : number ,
79
+ labels : PaginationButtonLabels | undefined ,
97
80
{
98
81
boundaryCount = 1 ,
99
82
siblingCount = 1 ,
100
83
hidePrevButton,
101
84
hideNextButton,
102
85
showFirstButton,
103
86
showLastButton,
104
- } : IGetPaginationItems [ 'options' ]
105
- ) : TPaginationItem [ ] => {
87
+ } : PaginationOptions
88
+ ) : PaginationButtonValue [ ] => {
106
89
const startPages = range ( 1 , Math . min ( boundaryCount , count ) ) ;
107
90
const endPages = range (
108
91
Math . max ( count - boundaryCount + 1 , boundaryCount + 1 ) ,
@@ -152,14 +135,14 @@ export const getPaginationItems = (
152
135
return items ;
153
136
} ;
154
137
155
- export const RenderPaginationItem = component$ (
138
+ export const PaginationButton = component$ (
156
139
( {
157
140
'aria-label' : ariaLabel ,
158
141
disabled,
159
142
onClick$,
160
143
key,
161
144
value,
162
- } : IRenderPaginationItemProps ) => {
145
+ } : PaginationButtonProps ) => {
163
146
return (
164
147
< HeadlessButton
165
148
onClick$ = { onClick$ }
@@ -188,7 +171,7 @@ export const PaginationDivider = component$(() => {
188
171
*/
189
172
export const Pagination = component$ (
190
173
( {
191
- RenderItem = RenderPaginationItem ,
174
+ RenderItem = PaginationButton ,
192
175
RenderDivider = PaginationDivider ,
193
176
onPaging$,
194
177
page,
@@ -197,17 +180,37 @@ export const Pagination = component$(
197
180
defaultClass,
198
181
labels,
199
182
...rest
200
- } : IPaginationProps ) => {
201
- const pagi = getPaginationItems ( page , pages , labels , rest ) ;
202
-
183
+ } : PaginationProps ) => {
203
184
const _onPaging$ = $ ( ( page : number ) => {
204
185
if ( page < 1 || page > pages ) return ;
205
186
onPaging$ ( page ) ;
206
187
} ) ;
207
188
189
+ const itemClickHandler = $ ( ( item : PaginationButtonValue ) =>
190
+ _onPaging$ (
191
+ ( ( ) => {
192
+ switch ( item ) {
193
+ case 'first' :
194
+ return 1 ;
195
+ case 'prev' :
196
+ return page - 1 ;
197
+ case 'next' :
198
+ return page + 1 ;
199
+ case 'last' :
200
+ return pages ;
201
+ default :
202
+ if ( typeof item === 'number' ) return item ;
203
+ return page ;
204
+ }
205
+ } ) ( )
206
+ )
207
+ ) ;
208
+
209
+ const items = getPaginationButtons ( page , pages , labels , rest ) ;
210
+
208
211
return (
209
212
< >
210
- { pagi . map ( ( item , i ) => {
213
+ { items . map ( ( item , i ) => {
211
214
return (
212
215
< >
213
216
{ item === 'divider' ? (
@@ -218,25 +221,7 @@ export const Pagination = component$(
218
221
defaultClass = { defaultClass }
219
222
key = { i }
220
223
labels = { labels }
221
- onClick$ = { ( ) =>
222
- _onPaging$ (
223
- ( ( ) => {
224
- switch ( item ) {
225
- case 'first' :
226
- return 1 ;
227
- case 'prev' :
228
- return page - 1 ;
229
- case 'next' :
230
- return page + 1 ;
231
- case 'last' :
232
- return pages ;
233
- default :
234
- if ( typeof item === 'number' ) return item ;
235
- return page ;
236
- }
237
- } ) ( )
238
- )
239
- }
224
+ onClick$ = { ( ) => itemClickHandler ( item ) }
240
225
disabled = {
241
226
( [ 'prev' , 'first' ] . includes ( item . toString ( ) ) &&
242
227
page === 1 ) ||
0 commit comments