Skip to content

Commit d5c8a79

Browse files
committed
Add FeathersVuexPagination component
1 parent f72b156 commit d5c8a79

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/FeathersVuexPagination.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { createElement, computed } from '@vue/composition-api'
2+
3+
export default {
4+
name: 'FeathersVuexPagination',
5+
props: {
6+
/**
7+
* An object containing { $limit, and $skip }
8+
*/
9+
value: {
10+
type: Object,
11+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
12+
default: () => null
13+
},
14+
/**
15+
* The `latestQuery` object from the useFind data
16+
*/
17+
latestQuery: {
18+
type: Object,
19+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
20+
default: () => null
21+
}
22+
},
23+
// eslint-disable-next-line
24+
setup(props, context) {
25+
// Total
26+
const pageCount = computed(() => {
27+
const q = props.latestQuery
28+
if (q && q.response) {
29+
return Math.ceil(q.response.total / props.value.$limit)
30+
} else {
31+
return 1
32+
}
33+
})
34+
// Current Page
35+
const currentPage = computed({
36+
set(pageNumber: number) {
37+
if (pageNumber < 1) {
38+
pageNumber = 1
39+
} else if (pageNumber > pageCount.value) {
40+
pageNumber = pageCount.value
41+
}
42+
const $limit = props.value.$limit
43+
const $skip = $limit * (pageNumber - 1)
44+
45+
context.emit('input', { $limit, $skip })
46+
},
47+
get() {
48+
const params = props.value
49+
if (params) {
50+
return params.$skip / params.$limit + 1
51+
} else {
52+
return 1
53+
}
54+
}
55+
})
56+
57+
const canPrev = computed(() => {
58+
return currentPage.value - 1 > 0
59+
})
60+
const canNext = computed(() => {
61+
return currentPage.value + 1 < pageCount.value
62+
})
63+
64+
function toStart(): void {
65+
currentPage.value = 1
66+
}
67+
function toEnd(): void {
68+
currentPage.value = pageCount.value
69+
}
70+
71+
function next(): void {
72+
currentPage.value++
73+
}
74+
function prev(): void {
75+
currentPage.value--
76+
}
77+
78+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
79+
return () => {
80+
if (context.slots.default) {
81+
return context.slots.default({
82+
currentPage: currentPage.value,
83+
pageCount: pageCount.value,
84+
canPrev: canPrev.value,
85+
canNext: canNext.value,
86+
toStart,
87+
toEnd,
88+
prev,
89+
next
90+
})
91+
} else {
92+
return createElement('div', {}, [
93+
createElement('p', `FeathersVuexPagination uses the default slot:`),
94+
createElement('p', `#default="{ currentPage, pageCount }"`)
95+
])
96+
}
97+
}
98+
}
99+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import FeathersVuexFind from './FeathersVuexFind'
77
import FeathersVuexGet from './FeathersVuexGet'
88
import FeathersVuexFormWrapper from './FeathersVuexFormWrapper'
99
import FeathersVuexInputWrapper from './FeathersVuexInputWrapper'
10+
import FeathersVuexPagination from './FeathersVuexPagination'
1011
import makeFindMixin from './make-find-mixin'
1112
import makeGetMixin from './make-get-mixin'
1213
import { globalModels as models } from './service-module/global-models'
@@ -75,6 +76,7 @@ export {
7576
FeathersVuexGet,
7677
FeathersVuexFormWrapper,
7778
FeathersVuexInputWrapper,
79+
FeathersVuexPagination,
7880
FeathersVuex,
7981
makeFindMixin,
8082
makeGetMixin,

0 commit comments

Comments
 (0)