-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useItemPagination composable
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
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,44 @@ | ||
import { useOffsetPagination } from '@vueuse/core' | ||
import { computed, toValue, type MaybeRefOrGetter } from 'vue' | ||
|
||
export type ItemPaginationSettings = { | ||
itemsPerPage: number | ||
onPageChange?: () => void | ||
} | ||
|
||
/** | ||
* Composable that provides pagination functionality for an array of items, | ||
* returning the paginated items based on the current page. | ||
* @param {MaybeRefOrGetter<T[]>} items - The array of items to paginate. | ||
* @param {ItemPaginationSettings} settings - The pagination settings, including the number of items per | ||
* page and an optional onPageChange callback. | ||
* | ||
* @example | ||
* const { currentPage, pageCount, paginatedItems, prev, next } = useItemPagination(items, { itemsPerPage: 10 }); | ||
*/ | ||
export function useItemPagination<T>( | ||
items: MaybeRefOrGetter<T[]>, | ||
settings: ItemPaginationSettings | ||
) { | ||
const { currentPage, pageCount, prev, next } = useOffsetPagination({ | ||
page: 1, | ||
pageSize: settings.itemsPerPage, | ||
total: () => toValue(items).length, | ||
onPageChange: settings.onPageChange | ||
}) | ||
|
||
const paginatedItems = computed(() => { | ||
const start = (currentPage.value - 1) * settings.itemsPerPage | ||
const end = start + settings.itemsPerPage | ||
const itemsArray = toValue(items) | ||
return itemsArray.slice(start, end) | ||
}) | ||
|
||
return { | ||
currentPage, | ||
pageCount, | ||
paginatedItems, | ||
prev, | ||
next | ||
} | ||
} |
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