Skip to content

Commit 022141e

Browse files
committed
add getter/setter for page in pagination component
1 parent 714bc2b commit 022141e

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

frontend/src/components/ui/pagination.ts

+39-24
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,30 @@ export class Pagination extends LitElement {
141141

142142
searchParams = new SearchParamsController(this, (params) => {
143143
const page = parsePage(params.get(this.name));
144-
if (this.page !== page) {
144+
if (this._page !== page) {
145145
this.dispatchEvent(
146146
new CustomEvent<PageChangeDetail>("page-change", {
147147
detail: { page: page, pages: this.pages },
148148
composed: true,
149149
}),
150150
);
151-
this.page = page;
151+
this._page = page;
152152
}
153153
});
154154

155155
@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+
}
157168

158169
@property({ type: String })
159170
name = "page";
@@ -174,7 +185,7 @@ export class Pagination extends LitElement {
174185
private pages = 0;
175186

176187
connectedCallback() {
177-
this.inputValue = `${this.page}`;
188+
this.inputValue = `${this._page}`;
178189
super.connectedCallback();
179190
}
180191

@@ -186,14 +197,14 @@ export class Pagination extends LitElement {
186197
const parsedPage = parseFloat(
187198
this.searchParams.searchParams.get(this.name) ?? "1",
188199
);
189-
if (parsedPage != this.page) {
200+
if (parsedPage != this._page) {
190201
const page = parsePage(this.searchParams.searchParams.get(this.name));
191202
const constrainedPage = Math.max(1, Math.min(this.pages, page));
192203
this.onPageChange(constrainedPage);
193204
}
194205

195-
if (changedProperties.get("page") && this.page) {
196-
this.inputValue = `${this.page}`;
206+
if (changedProperties.get("page") && this._page) {
207+
this.inputValue = `${this._page}`;
197208
}
198209
}
199210

@@ -208,7 +219,7 @@ export class Pagination extends LitElement {
208219
<li>
209220
<button
210221
class="navButton"
211-
?disabled=${this.page === 1}
222+
?disabled=${this._page === 1}
212223
@click=${this.onPrev}
213224
>
214225
<img class="chevron" src=${chevronLeft} />
@@ -221,7 +232,7 @@ export class Pagination extends LitElement {
221232
<li>
222233
<button
223234
class="navButton"
224-
?disabled=${this.page === this.pages}
235+
?disabled=${this._page === this.pages}
225236
@click=${this.onNext}
226237
>
227238
<span class=${classMap({ srOnly: this.compact })}
@@ -250,7 +261,7 @@ export class Pagination extends LitElement {
250261
inputmode="numeric"
251262
size="small"
252263
value=${this.inputValue}
253-
aria-label=${msg(str`Current page, page ${this.page}`)}
264+
aria-label=${msg(str`Current page, page ${this._page}`)}
254265
aria-current="page"
255266
autocomplete="off"
256267
min="1"
@@ -302,7 +313,7 @@ export class Pagination extends LitElement {
302313
const middleEnd = middleVisible * 2 - 1;
303314
const endsVisible = 2;
304315
if (this.pages > middleVisible + middleEnd) {
305-
const currentPageIdx = pages.indexOf(this.page);
316+
const currentPageIdx = pages.indexOf(this._page);
306317
const firstPages = pages.slice(0, endsVisible);
307318
const lastPages = pages.slice(-1 * endsVisible);
308319
let middlePages = pages.slice(endsVisible, middleEnd);
@@ -331,7 +342,7 @@ export class Pagination extends LitElement {
331342
};
332343

333344
private readonly renderPageButton = (page: number) => {
334-
const isCurrent = page === this.page;
345+
const isCurrent = page === this._page;
335346
return html`<li aria-current=${ifDefined(isCurrent ? "page" : undefined)}>
336347
<btrix-navigation-button
337348
icon
@@ -346,31 +357,35 @@ export class Pagination extends LitElement {
346357
};
347358

348359
private onPrev() {
349-
this.onPageChange(this.page > 1 ? this.page - 1 : 1);
360+
this.onPageChange(this._page > 1 ? this._page - 1 : 1);
350361
}
351362

352363
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);
354365
}
355366

356367
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);
366370
this.dispatchEvent(
367371
new CustomEvent<PageChangeDetail>("page-change", {
368372
detail: { page: page, pages: this.pages },
369373
composed: true,
370374
}),
371375
);
372376
}
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+
});
374389
}
375390

376391
private calculatePages() {

0 commit comments

Comments
 (0)