@@ -141,19 +141,30 @@ export class Pagination extends LitElement {
141
141
142
142
searchParams = new SearchParamsController ( this , ( params ) => {
143
143
const page = parsePage ( params . get ( this . name ) ) ;
144
- if ( this . page !== page ) {
144
+ if ( this . _page !== page ) {
145
145
this . dispatchEvent (
146
146
new CustomEvent < PageChangeDetail > ( "page-change" , {
147
147
detail : { page : page , pages : this . pages } ,
148
148
composed : true ,
149
149
} ) ,
150
150
) ;
151
- this . page = page ;
151
+ this . _page = page ;
152
152
}
153
153
} ) ;
154
154
155
155
@state ( )
156
- page = 1 ;
156
+ private _page = 1 ;
157
+
158
+ @property ( { type : Number } )
159
+ set page ( page : number ) {
160
+ if ( page !== this . _page ) {
161
+ this . setPage ( page ) ;
162
+ }
163
+ }
164
+
165
+ get page ( ) {
166
+ return this . _page ;
167
+ }
157
168
158
169
@property ( { type : String } )
159
170
name = "page" ;
@@ -174,7 +185,7 @@ export class Pagination extends LitElement {
174
185
private pages = 0 ;
175
186
176
187
connectedCallback ( ) {
177
- this . inputValue = `${ this . page } ` ;
188
+ this . inputValue = `${ this . _page } ` ;
178
189
super . connectedCallback ( ) ;
179
190
}
180
191
@@ -186,14 +197,14 @@ export class Pagination extends LitElement {
186
197
const parsedPage = parseFloat (
187
198
this . searchParams . searchParams . get ( this . name ) ?? "1" ,
188
199
) ;
189
- if ( parsedPage != this . page ) {
200
+ if ( parsedPage != this . _page ) {
190
201
const page = parsePage ( this . searchParams . searchParams . get ( this . name ) ) ;
191
202
const constrainedPage = Math . max ( 1 , Math . min ( this . pages , page ) ) ;
192
203
this . onPageChange ( constrainedPage ) ;
193
204
}
194
205
195
- if ( changedProperties . get ( "page" ) && this . page ) {
196
- this . inputValue = `${ this . page } ` ;
206
+ if ( changedProperties . get ( "page" ) && this . _page ) {
207
+ this . inputValue = `${ this . _page } ` ;
197
208
}
198
209
}
199
210
@@ -208,7 +219,7 @@ export class Pagination extends LitElement {
208
219
< li >
209
220
< button
210
221
class ="navButton "
211
- ?disabled =${ this . page === 1 }
222
+ ?disabled =${ this . _page === 1 }
212
223
@click =${ this . onPrev }
213
224
>
214
225
< img class ="chevron " src =${ chevronLeft } / >
@@ -221,7 +232,7 @@ export class Pagination extends LitElement {
221
232
< li >
222
233
< button
223
234
class ="navButton "
224
- ?disabled =${ this . page === this . pages }
235
+ ?disabled =${ this . _page === this . pages }
225
236
@click =${ this . onNext }
226
237
>
227
238
< span class =${ classMap ( { srOnly : this . compact } ) }
@@ -250,7 +261,7 @@ export class Pagination extends LitElement {
250
261
inputmode ="numeric "
251
262
size ="small "
252
263
value =${ this . inputValue }
253
- aria-label =${ msg ( str `Current page, page ${ this . page } ` ) }
264
+ aria-label =${ msg ( str `Current page, page ${ this . _page } ` ) }
254
265
aria-current="page"
255
266
autocomplete="off"
256
267
min="1"
@@ -302,7 +313,7 @@ export class Pagination extends LitElement {
302
313
const middleEnd = middleVisible * 2 - 1 ;
303
314
const endsVisible = 2 ;
304
315
if ( this . pages > middleVisible + middleEnd ) {
305
- const currentPageIdx = pages . indexOf ( this . page ) ;
316
+ const currentPageIdx = pages . indexOf ( this . _page ) ;
306
317
const firstPages = pages . slice ( 0 , endsVisible ) ;
307
318
const lastPages = pages . slice ( - 1 * endsVisible ) ;
308
319
let middlePages = pages . slice ( endsVisible , middleEnd ) ;
@@ -331,7 +342,7 @@ export class Pagination extends LitElement {
331
342
} ;
332
343
333
344
private readonly renderPageButton = ( page : number ) => {
334
- const isCurrent = page === this . page ;
345
+ const isCurrent = page === this . _page ;
335
346
return html `< li aria-current =${ ifDefined ( isCurrent ? "page" : undefined ) } >
336
347
< btrix-navigation-button
337
348
icon
@@ -346,31 +357,35 @@ export class Pagination extends LitElement {
346
357
} ;
347
358
348
359
private onPrev ( ) {
349
- this . onPageChange ( this . page > 1 ? this . page - 1 : 1 ) ;
360
+ this . onPageChange ( this . _page > 1 ? this . _page - 1 : 1 ) ;
350
361
}
351
362
352
363
private onNext ( ) {
353
- this . onPageChange ( this . page < this . pages ? this . page + 1 : this . pages ) ;
364
+ this . onPageChange ( this . _page < this . pages ? this . _page + 1 : this . pages ) ;
354
365
}
355
366
356
367
private onPageChange ( page : number ) {
357
- if ( this . page !== page ) {
358
- this . searchParams . set ( ( params ) => {
359
- if ( page === 1 ) {
360
- params . delete ( this . name ) ;
361
- } else {
362
- params . set ( this . name , page . toString ( ) ) ;
363
- }
364
- return params ;
365
- } ) ;
368
+ if ( this . _page !== page ) {
369
+ this . setPage ( page ) ;
366
370
this . dispatchEvent (
367
371
new CustomEvent < PageChangeDetail > ( "page-change" , {
368
372
detail : { page : page , pages : this . pages } ,
369
373
composed : true ,
370
374
} ) ,
371
375
) ;
372
376
}
373
- this . page = page ;
377
+ this . _page = page ;
378
+ }
379
+
380
+ private setPage ( page : number ) {
381
+ this . searchParams . set ( ( params ) => {
382
+ if ( page === 1 ) {
383
+ params . delete ( this . name ) ;
384
+ } else {
385
+ params . set ( this . name , page . toString ( ) ) ;
386
+ }
387
+ return params ;
388
+ } ) ;
374
389
}
375
390
376
391
private calculatePages ( ) {
0 commit comments