Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from bryanmylee/plugins/usePagination
`usePagination` plugin
- Loading branch information
Showing
5 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { BodyRow } from '$lib/bodyRows'; | ||
import type { NewTablePropSet, TablePlugin } from '$lib/types/TablePlugin'; | ||
import { derived, writable, type Readable, type Updater, type Writable } from 'svelte/store'; | ||
|
||
export interface PaginationConfig { | ||
initialPageIndex?: number; | ||
initialPageSize?: number; | ||
} | ||
|
||
export interface PaginationState<Item> { | ||
pageSize: Writable<number>; | ||
pageIndex: Writable<number>; | ||
pageCount: Readable<number>; | ||
prePaginatedRows: Readable<BodyRow<Item>[]>; | ||
hasPreviousPage: Readable<boolean>; | ||
hasNextPage: Readable<boolean>; | ||
} | ||
|
||
const MIN_PAGE_SIZE = 1; | ||
|
||
export const usePageStore = ({ items, initialPageSize, initialPageIndex }: PageStoreConfig) => { | ||
const pageSize = writable(initialPageSize); | ||
const updatePageSize = (fn: Updater<number>) => { | ||
pageSize.update(($pageSize) => { | ||
const newPageSize = fn($pageSize); | ||
return Math.max(newPageSize, MIN_PAGE_SIZE); | ||
}); | ||
}; | ||
const setPageSize = (newPageSize: number) => updatePageSize(() => newPageSize); | ||
|
||
const pageCount = derived([pageSize, items], ([$pageSize, $items]) => { | ||
const $pageCount = Math.ceil($items.length / $pageSize); | ||
pageIndex.update(($pageIndex) => { | ||
if ($pageIndex >= $pageCount) { | ||
return $pageCount - 1; | ||
} | ||
return $pageIndex; | ||
}); | ||
return $pageCount; | ||
}); | ||
|
||
const pageIndex = writable(initialPageIndex); | ||
|
||
const hasPreviousPage = derived(pageIndex, ($pageIndex) => { | ||
return $pageIndex > 0; | ||
}); | ||
const hasNextPage = derived([pageIndex, pageCount], ([$pageIndex, $pageCount]) => { | ||
return $pageIndex < $pageCount - 1; | ||
}); | ||
|
||
return { | ||
pageSize: { | ||
subscribe: pageSize.subscribe, | ||
update: updatePageSize, | ||
set: setPageSize, | ||
}, | ||
pageCount, | ||
pageIndex, | ||
hasPreviousPage, | ||
hasNextPage, | ||
}; | ||
}; | ||
|
||
export interface PageStoreConfig { | ||
items: Readable<unknown[]>; | ||
initialPageSize?: number; | ||
initialPageIndex?: number; | ||
} | ||
|
||
export const usePagination = | ||
<Item>({ initialPageIndex = 0, initialPageSize = 10 }: PaginationConfig = {}): TablePlugin< | ||
Item, | ||
PaginationState<Item>, | ||
Record<string, never>, | ||
NewTablePropSet<never> | ||
> => | ||
() => { | ||
const prePaginatedRows = writable<BodyRow<Item>[]>([]); | ||
const paginatedRows = writable<BodyRow<Item>[]>([]); | ||
const { pageSize, pageCount, pageIndex, hasPreviousPage, hasNextPage } = usePageStore({ | ||
items: prePaginatedRows, | ||
initialPageIndex, | ||
initialPageSize, | ||
}); | ||
const pluginState: PaginationState<Item> = { | ||
pageSize, | ||
pageIndex, | ||
prePaginatedRows, | ||
pageCount, | ||
hasPreviousPage, | ||
hasNextPage, | ||
}; | ||
|
||
const transformRowsFn = derived([pageSize, pageIndex], ([$pageSize, $pageIndex]) => { | ||
return (rows: BodyRow<Item>[]) => { | ||
prePaginatedRows.set(rows); | ||
const startIdx = $pageIndex * $pageSize; | ||
const _paginatedRows = rows.slice(startIdx, startIdx + $pageSize); | ||
paginatedRows.set(_paginatedRows); | ||
return _paginatedRows; | ||
}; | ||
}); | ||
|
||
return { | ||
pluginState, | ||
transformRowsFn, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters