Skip to content

Commit

Permalink
[paginationStore] Clamp page between valid values
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Jun 21, 2024
1 parent cc27658 commit 6adc3f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thin-roses-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

[paginationStore] Clamp page between valid values
5 changes: 4 additions & 1 deletion packages/svelte-ux/src/lib/stores/paginationStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { writable } from 'svelte/store';
import { clamp } from '../utils/number.js';

export type PaginationProps = {
page?: number;
Expand Down Expand Up @@ -40,8 +41,10 @@ export default function paginationStore(props?: PaginationProps) {
};
}

function createState(page: number, perPage: number, total: number) {
function createState(_page: number, perPage: number, total: number) {
const totalPages = Math.ceil(total / perPage);
// Do not allow page to exceed bounds (ex. call nextPage() when on last page)
const page = clamp(_page, 1, totalPages);

return {
page,
Expand Down

0 comments on commit 6adc3f2

Please sign in to comment.