Skip to content

Commit 2130d99

Browse files
fix: force a pagination hook trigger when initializing non-CMS pagination (#774)
1 parent ca0af8f commit 2130d99

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

.changeset/funny-pugs-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@finsweet/attributes-list': patch
3+
---
4+
5+
fix: force a pagination hook trigger when initializing non-CMS pagination

packages/list/src/components/List.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,17 @@ export class List {
806806
* Adds a hook.
807807
* @param key
808808
* @param callback
809+
* @param options.forceTrigger Whether to trigger the hook immediately after adding it.
809810
*/
810-
addHook(key: HookKey, callback: HookCallback) {
811+
addHook(key: HookKey, callback: HookCallback, { forceTrigger }: { forceTrigger?: boolean } = {}) {
811812
const hook = this.hooks[key];
812813

813814
hook.callbacks.push(callback);
814815

816+
if (forceTrigger) {
817+
this.triggerHook(key);
818+
}
819+
815820
return () => {
816821
hook.callbacks = hook.callbacks.filter((cb) => cb !== callback);
817822
};

packages/list/src/load/infinite.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ export const initInfiniteMode = (list: List) => {
1818
const thresholdCoefficient = getInfiniteThreshold(list);
1919

2020
// Add hook
21-
list.addHook('pagination', (items) => {
22-
const paginatedItems = items.slice(0, list.itemsPerPage.value);
23-
return paginatedItems;
24-
});
21+
list.addHook(
22+
'pagination',
23+
(items) => {
24+
const paginatedItems = items.slice(0, list.itemsPerPage.value);
25+
return paginatedItems;
26+
},
27+
{ forceTrigger: !list.paginationNextCMSElement.value && !list.paginationPreviousCMSElement.value } // Force a render if the list is static
28+
);
2529

2630
// Init
2731
loadPaginatedCMSItems(list);

packages/list/src/load/more.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ import { loadPaginatedCMSItems } from './load';
1313
*/
1414
export const initMoreMode = (list: List) => {
1515
// Add hook
16-
list.addHook('pagination', (items) => {
17-
const paginatedItems = items.slice(0, list.itemsPerPage.value);
18-
return paginatedItems;
19-
});
16+
list.addHook(
17+
'pagination',
18+
(items) => {
19+
const paginatedItems = items.slice(0, list.itemsPerPage.value);
20+
return paginatedItems;
21+
},
22+
{ forceTrigger: !list.paginationNextCMSElement.value && !list.paginationPreviousCMSElement.value } // Force a render if the list is static
23+
);
2024

2125
// Init
2226
loadPaginatedCMSItems(list);

packages/list/src/load/pagination.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@ export const initPaginationMode = async (list: List) => {
2929
if (!paginationWrapperElement) return;
3030

3131
// Init hook
32-
list.addHook('pagination', (items) => {
33-
const $itemsPerPage = itemsPerPage.value;
32+
list.addHook(
33+
'pagination',
34+
(items) => {
35+
const $itemsPerPage = itemsPerPage.value;
3436

35-
const start = (currentPage.value - 1) * $itemsPerPage;
36-
const end = start + $itemsPerPage;
37+
const start = (currentPage.value - 1) * $itemsPerPage;
38+
const end = start + $itemsPerPage;
3739

38-
const paginatedItems = items.slice(start, end);
40+
const paginatedItems = items.slice(start, end);
3941

40-
return paginatedItems;
41-
});
42+
return paginatedItems;
43+
},
44+
{ forceTrigger: !list.paginationNextCMSElement.value && !list.paginationPreviousCMSElement.value } // Force a render if the list is static
45+
);
4246

4347
const currentPageCleanup = watch(
4448
list.currentPage,

0 commit comments

Comments
 (0)