/
VirtualGrid.tsx
132 lines (130 loc) · 5.4 KB
/
VirtualGrid.tsx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { createVirtualizer } from '../table/virtual/createVirtualizer'
import { Atom, JSXSlot, NullAtom, atom, classNames, toCSSPx } from '@cn-ui/reactive'
import { Accessor, For, JSXElement, Show } from 'solid-js'
import { useAutoResize } from '../table/hook/useAutoResize'
import { Key } from '@solid-primitives/keyed'
import { TransitionGroup } from 'solid-transition-group'
export interface VirtualGridProps<T> {
each: T[][]
colSize?: number
reverse?: boolean
fallback?: JSXSlot
estimateHeight?: number
estimateWidth?: number
/** 标识其 Key 值的属性,为了保证渲染的高效 */
getItemKey?: (index: number) => number | string
children: (
item: T,
index: Accessor<number>,
context: {
itemClass: Atom<string>
itemRef: Atom<HTMLDivElement | null>
}
) => JSXElement
transitionName?: string
overscan?: number
}
export function VirtualGrid<T>(props: VirtualGridProps<T>) {
const tableContainerRef = NullAtom<HTMLDivElement>(null)
const virtualizer = createVirtualizer({
get count() {
return props.each.length
},
getItemKey: props.getItemKey,
estimateSize(index) {
return props.estimateWidth ?? 24
},
measureElement:
typeof window !== 'undefined' && navigator.userAgent.indexOf('Firefox') === -1 ? (element) => element?.getBoundingClientRect().height : undefined,
getScrollElement: () => tableContainerRef(),
overscan: props.overscan ?? 3,
gridSize: () => props.each.length
})
const colVirtualizer = createVirtualizer({
get count() {
return props.colSize ?? props.each[0].length
},
getItemKey: props.getItemKey,
estimateSize(index) {
return props.estimateHeight ?? 24
},
getScrollElement: () => tableContainerRef(),
horizontal: true,
get overscan() {
return props.overscan ?? Math.min(20, Math.floor(Math.sqrt(props.each.length)))
},
gridSize: () => props.each.length
})
const { height, width } = useAutoResize(() => tableContainerRef()?.parentElement!)
const CoreList = (
<Key by="key" each={virtualizer.getVirtualItems()}>
{(virtualRow) => {
const itemClass = atom('')
const itemRef = NullAtom<HTMLDivElement>(null)
const context = { itemClass, itemRef }
const row = props.each[virtualRow().index]
return (
<Show when={row}>
<div
class={classNames('cn-virtual-list-item absolute w-full', itemClass())}
data-index={virtualRow().index} //needed for dynamic row height measurement
ref={(node) => {
itemRef(node)
queueMicrotask(() => {
colVirtualizer.measureElement(node)
})
}}
style={{
[props.reverse ? 'bottom' : 'top']: `${virtualRow().start}px`
}}
>
<For each={colVirtualizer.getVirtualItems()}>
{(item) => {
return (
<div
class="absolute"
data-index={item.index}
ref={(node) => {
queueMicrotask(() => {
colVirtualizer.measureElement(node)
})
}}
style={{
left: `${item.start}px`
}}
>
{props.children(row[item.index], () => virtualRow().index, context)}
</div>
)
}}
</For>
</div>
</Show>
)
}}
</Key>
)
return (
<div
ref={tableContainerRef}
class={classNames('cn-virtual-list', props.reverse ? 'cn-virtual-list-reverse flex flex-col-reverse' : 'cn-virtual-list-normal')}
style={{
overflow: 'auto',
position: 'relative',
height: toCSSPx(height(), '400px'),
width: toCSSPx(width(), '400px') //should be a fixed height
}}
>
<div
class="cn-virtual-list-container flex-none"
style={{
width: `${colVirtualizer.getTotalSize()}px`,
height: `${virtualizer.getTotalSize()}px`,
position: 'relative' //needed for absolute positioning of rows
}}
>
{props.transitionName ? <TransitionGroup name={props.transitionName}>{CoreList}</TransitionGroup> : CoreList}
</div>
</div>
)
}