Skip to content

Commit 4924a0f

Browse files
authored
Merge pull request #4312 from dialvarezs/fix/pagination-stale-lastpage
fix: make pagination reactive to lastPage changes
2 parents 487aecd + 391d285 commit 4924a0f

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

sites/main-site/src/components/PaginationNav.svelte

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,30 @@
77
88
let { lastPage = 1 }: Props = $props();
99
10-
let pages: number[] = $state([]);
11-
let truncatedPages: number[] = $state([]);
12-
1310
const maxPages = 7;
14-
let truncated = $state(false);
11+
let truncated = $derived(lastPage > maxPages);
1512
16-
function generatePages() {
17-
pages = [];
18-
truncatedPages = [];
19-
truncated = lastPage > maxPages;
13+
let pages = $derived.by(() => {
14+
if (truncated) return [];
15+
return Array.from({ length: lastPage }, (_, i) => i + 1);
16+
});
2017
21-
if (truncated) {
22-
const startIndex = Math.max($currentPage - Math.floor(maxPages / 2), 1);
23-
const endIndex = Math.min(startIndex + maxPages - 1, lastPage);
18+
let truncatedPages = $derived.by(() => {
19+
if (!truncated) return [];
20+
const startIndex = Math.max($currentPage - Math.floor(maxPages / 2), 1);
21+
const endIndex = Math.min(startIndex + maxPages - 1, lastPage);
22+
return Array.from({ length: endIndex - startIndex + 1 }, (_, i) => startIndex + i);
23+
});
2424
25-
for (let i = startIndex; i <= endIndex; i++) {
26-
truncatedPages.push(i);
27-
}
28-
} else {
29-
for (let i = 1; i <= lastPage; i++) {
30-
pages.push(i);
31-
}
25+
// Clamp current page if lastPage shrinks (e.g. after filtering search results)
26+
$effect(() => {
27+
if ($currentPage > lastPage) {
28+
$currentPage = lastPage;
3229
}
33-
}
30+
});
3431
3532
function handlePageChange(page) {
3633
$currentPage = page;
37-
generatePages();
3834
}
3935
4036
function handleKeydown(e, page) {
@@ -43,8 +39,6 @@
4339
handlePageChange(page);
4440
}
4541
}
46-
47-
generatePages();
4842
</script>
4943

5044
<div class="d-flex justify-content-center mt-2">

0 commit comments

Comments
 (0)