Skip to content

Commit

Permalink
feat(useOffsetPagination): new function (vitest-dev#1104)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
okxiaoliang4 and antfu committed Feb 8, 2022
1 parent fbca4a6 commit b4b7e8e
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ Collection of essential Vue Composition Utilities
<a href="https://www.npmjs.com/package/@vueuse/core" target="__blank"><img src="https://img.shields.io/npm/v/@vueuse/core?color=a1b858&label=" alt="NPM version"></a>
<a href="https://www.npmjs.com/package/@vueuse/core" target="__blank"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/@vueuse/core?color=50a36f&label="></a>
<a href="https://vueuse.org" target="__blank"><img src="https://img.shields.io/static/v1?label=&message=docs%20%26%20demos&color=1e8a7a" alt="Docs & Demos"></a>
<img alt="Function Count" src="https://img.shields.io/badge/-185%20functions-13708a">
<img alt="Function Count" src="https://img.shields.io/badge/-187%20functions-13708a">
<br>
<a href="https://github.com/vueuse/vueuse" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/vueuse/vueuse?style=social"></a>
</p>
Expand Down
5 changes: 3 additions & 2 deletions packages/components/index.ts
@@ -1,7 +1,7 @@
export * from '../core/onClickOutside/component'
export * from '../core/onClickOutside/directive'
export * from '../core/onLongPress/component'
export * from '../core/onLongPress/directive'
export * from '../core/onLongpress/component'
export * from '../core/onLongpress/directive'
export * from '../core/useActiveElement/component'
export * from '../core/useBattery/component'
export * from '../core/useBrowserLocation/component'
Expand All @@ -25,6 +25,7 @@ export * from '../core/useMouseInElement/component'
export * from '../core/useMousePressed/component'
export * from '../core/useNetwork/component'
export * from '../core/useNow/component'
export * from '../core/useOffsetPagination/component'
export * from '../core/useOnline/component'
export * from '../core/usePageLeave/component'
export * from '../core/usePointer/component'
Expand Down
3 changes: 2 additions & 1 deletion packages/core/index.ts
Expand Up @@ -4,7 +4,7 @@ export * from './computedInject'
export * from './createUnrefFn'
export * from './onClickOutside'
export * from './onKeyStroke'
export * from './onLongPress'
export * from './onLongpress'
export * from './onStartTyping'
export * from './templateRef'
export * from './unrefElement'
Expand Down Expand Up @@ -67,6 +67,7 @@ export * from './useMutationObserver'
export * from './useNavigatorLanguage'
export * from './useNetwork'
export * from './useNow'
export * from './useOffsetPagination'
export * from './useOnline'
export * from './usePageLeave'
export * from './useParallax'
Expand Down
42 changes: 42 additions & 0 deletions packages/core/useOffsetPagination/component.ts
@@ -0,0 +1,42 @@
import { defineComponent, reactive } from 'vue-demi'
import { useOffsetPagination } from '@vueuse/core'
import type { UseOffsetPaginationOptions } from '.'

export const UseOffsetPagination = defineComponent<UseOffsetPaginationOptions>({
name: 'UseOffsetPagination',
props: [
'total',
'page',
'pageSize',
'onPageChange',
'onPageSizeChange',
'onPageCountChange',
] as unknown as undefined,
emits: [
'page-change',
'page-size-change',
'page-count-change',
],
setup(props, { slots, emit }) {
const data = reactive(useOffsetPagination({
...props,
onPageChange(...args) {
props.onPageChange?.(...args)
emit('page-change', ...args)
},
onPageSizeChange(...args) {
props.onPageSizeChange?.(...args)
emit('page-size-change', ...args)
},
onPageCountChange(...args) {
props.onPageCountChange?.(...args)
emit('page-count-change', ...args)
},
}))

return () => {
if (slots.default)
return slots.default(data)
}
},
})
114 changes: 114 additions & 0 deletions packages/core/useOffsetPagination/demo.vue
@@ -0,0 +1,114 @@
<script setup lang="ts">
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import { useOffsetPagination } from '.'
interface User {
id: number
name: string
}
const database = ref([]) as Ref<User[]>
for (let i = 0; i < 80; i++)
database.value.push({ id: i, name: `user ${i}` })
function fetch(page: number, pageSize: number) {
return new Promise<User[]>((resolve, reject) => {
const start = (page - 1) * pageSize
const end = start + pageSize
setTimeout(() => {
resolve(database.value.slice(start, end))
}, 100)
})
}
const data: Ref<User[]> = ref([])
const page = ref(1)
const pageSize = ref(10)
fetchData({
currentPage: page.value,
currentPageSize: pageSize.value,
})
function fetchData({ currentPage, currentPageSize }: { currentPage: number; currentPageSize: number }) {
fetch(currentPage, currentPageSize).then((responseData) => {
data.value = responseData
})
}
const {
currentPage,
currentPageSize,
pageCount,
isFirstPage,
isLastPage,
prev,
next,
} = useOffsetPagination({
total: database.value.length,
page: 1,
pageSize,
onPageChange: fetchData,
onPageSizeChange: fetchData,
})
</script>

<template>
<div class="gap-x-4 gap-y-2 grid-cols-2 inline-grid items-center">
<div opacity="50">
total:
</div>
<div>{{ database.length }}</div>
<div opacity="50">
pageCount:
</div>
<div>{{ pageCount }}</div>
<div opacity="50">
currentPageSize:
</div>
<div>{{ currentPageSize }}</div>
<div opacity="50">
currentPage:
</div>
<div>{{ currentPage }}</div>
<div opacity="50">
isFirstPage:
</div>
<div>{{ isFirstPage }}</div>
<div opacity="50">
isLastPage:
</div>
<div>{{ isLastPage }}</div>
</div>
<div class="my-4">
<button :disabled="isFirstPage" @click="prev">
prev
</button>
<button
v-for="item in pageCount"
:key="item"
:disabled="currentPage === item"
@click="currentPage = item"
>
{{ item }}
</button>
<button :disabled="isLastPage" @click="next">
next
</button>
</div>

<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
</tr>
</thead>
<tr v-for="d in data" :key="d.id">
<td>{{ d.id }}</td>
<td>{{ d.name }}</td>
</tr>
</table>
</template>
134 changes: 134 additions & 0 deletions packages/core/useOffsetPagination/index.md
@@ -0,0 +1,134 @@
---
category: Utilities
---

# useOffsetPagination

Reactive offset pagination.

## Usage

```ts
import { useOffsetPagination } from '@vueuse/core'

function fetchData({ currentPage, currentPageSize }: { currentPage: number; currentPageSize: number }) {
fetch(currentPage, currentPageSize).then((responseData) => {
data.value = responseData
})
}

const {
currentPage,
currentPageSize,
pageCount,
isFirstPage,
isLastPage,
prev,
next,
} = useOffsetPagination({
total: database.value.length,
page: 1,
pageSize,
onPageChange: fetchData,
onPageSizeChange: fetchData,
})
```

## Component

```html
<UseOffsetPagination
v-slot="{
currentPage,
currentPageSize,
next,
prev,
pageCount,
isFirstPage,
isLastPage
}"
:total="database.length"
@page-change="fetchData"
@page-size-change="fetchData"
>
<div class="gap-x-4 gap-y-2 grid-cols-2 inline-grid items-center">
<div opacity="50">
total:
</div>
<div>{{ database.length }}</div>
<div opacity="50">
pageCount:
</div>
<div>{{ pageCount }}</div>
<div opacity="50">
currentPageSize:
</div>
<div>{{ currentPageSize }}</div>
<div opacity="50">
currentPage:
</div>
<div>{{ currentPage }}</div>
<div opacity="50">
isFirstPage:
</div>
<div>{{ isFirstPage }}</div>
<div opacity="50">
isLastPage:
</div>
<div>{{ isLastPage }}</div>
</div>
<div>
<button :disabled="isFirstPage" @click="prev">
prev
</button>
<button :disabled="isLastPage" @click="next">
next
</button>
</div>
</UseOffsetPagination>
```

Component event supported props event callback and event listener.

event listener:
```html
<UseOffsetPagination
v-slot="{
currentPage,
currentPageSize,
next,
prev,
pageCount,
isFirstPage,
isLastPage
}"
:total="database.length"
@page-change="fetchData"
@page-size-change="fetchData"
@page-count-change="onPageCountChange"
>
<!-- your code -->
</UseOffsetPagination>
```

or props event callback:

```html
<UseOffsetPagination
v-slot="{
currentPage,
currentPageSize,
next,
prev,
pageCount,
isFirstPage,
isLastPage
}"
:total="database.length"
:on-page-change="fetchData"
:on-page-size-change="fetchData"
:on-page-count-change="onPageCountChange"
>
<!-- your code -->
</UseOffsetPagination>
```

0 comments on commit b4b7e8e

Please sign in to comment.