/
component.ts
54 lines (49 loc) · 1.32 KB
/
component.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { defineComponent, h, toRefs } from 'vue-demi'
import type { UseVirtualListOptions } from '@vueuse/core'
import { useVirtualList } from '@vueuse/core'
export interface UseVirtualListProps {
/**
* data of scrollable list
*
* @default []
*/
list: Array<any>
/**
* useVirtualList's options
*
* @default {}
*/
options: UseVirtualListOptions
/**
* virtualList's height
*
* @default 300px
*/
height: string
}
export const UseVirtualList = defineComponent<UseVirtualListProps>({
name: 'UseVirtualList',
props: [
'list',
'options',
'height',
] as unknown as undefined,
setup(props, { slots, expose }) {
const { list: listRef } = toRefs(props)
const { list, containerProps, wrapperProps, scrollTo } = useVirtualList(listRef, props.options)
expose({ scrollTo })
typeof containerProps.style === 'object' && !Array.isArray(containerProps.style) && (containerProps.style.height = props.height || '300px')
return () => h('div',
{ ...containerProps },
[
h('div',
{ ...wrapperProps.value },
list.value.map((item: any) => h('div',
{ style: { overFlow: 'hidden', height: item.height } },
slots.default ? slots.default(item) : 'Please set content!',
)),
),
],
)
},
})