-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathuseVisibleItems.ts
More file actions
29 lines (23 loc) · 803 Bytes
/
useVisibleItems.ts
File metadata and controls
29 lines (23 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { computed, shallowRef, toValue } from 'vue'
import type { MaybeRefOrGetter } from 'vue'
export function useVisibleItems<T>(items: MaybeRefOrGetter<T[]>, limit: number) {
const showAll = shallowRef(false)
const visibleItems = computed(() => {
const list = toValue(items)
return showAll.value ? list : list.slice(0, limit)
})
const hiddenCount = computed(() =>
showAll.value ? 0 : Math.max(0, toValue(items).length - limit),
)
const hasMore = computed(() => !showAll.value && toValue(items).length > limit)
const expand = () => {
showAll.value = true
}
const collapse = () => {
showAll.value = false
}
const toggle = () => {
showAll.value = !showAll.value
}
return { visibleItems, hiddenCount, hasMore, showAll, expand, collapse, toggle }
}