({
columns,
data,
}: {
data: Array<Array<string>>
columns: Array<Column>
})
| 169 | } |
| 170 | |
| 171 | function GridVirtualizerDynamic({ |
| 172 | columns, |
| 173 | data, |
| 174 | }: { |
| 175 | data: Array<Array<string>> |
| 176 | columns: Array<Column> |
| 177 | }) { |
| 178 | const parentRef = React.useRef<HTMLDivElement | null>(null) |
| 179 | |
| 180 | const parentOffsetRef = React.useRef(0) |
| 181 | |
| 182 | React.useLayoutEffect(() => { |
| 183 | parentOffsetRef.current = parentRef.current?.offsetTop ?? 0 |
| 184 | }, []) |
| 185 | |
| 186 | const getColumnWidth = (index: number) => columns[index].width |
| 187 | |
| 188 | const virtualizer = useWindowVirtualizer({ |
| 189 | count: data.length, |
| 190 | estimateSize: () => 350, |
| 191 | overscan: 5, |
| 192 | scrollMargin: parentOffsetRef.current, |
| 193 | }) |
| 194 | |
| 195 | const columnVirtualizer = useVirtualizer({ |
| 196 | horizontal: true, |
| 197 | count: columns.length, |
| 198 | getScrollElement: () => parentRef.current, |
| 199 | estimateSize: getColumnWidth, |
| 200 | overscan: 5, |
| 201 | }) |
| 202 | const columnItems = columnVirtualizer.getVirtualItems() |
| 203 | const [before, after] = |
| 204 | columnItems.length > 0 |
| 205 | ? [ |
| 206 | columnItems[0].start, |
| 207 | columnVirtualizer.getTotalSize() - |
| 208 | columnItems[columnItems.length - 1].end, |
| 209 | ] |
| 210 | : [0, 0] |
| 211 | |
| 212 | return ( |
| 213 | <div |
| 214 | ref={parentRef} |
| 215 | style={{ overflowY: 'auto', border: '1px solid #c8c8c8' }} |
| 216 | > |
| 217 | <div |
| 218 | style={{ |
| 219 | height: virtualizer.getTotalSize(), |
| 220 | position: 'relative', |
| 221 | }} |
| 222 | > |
| 223 | {virtualizer.getVirtualItems().map((row) => { |
| 224 | return ( |
| 225 | <div |
| 226 | key={row.key} |
| 227 | data-index={row.index} |
| 228 | ref={virtualizer.measureElement} |
nothing calls this directly
no test coverage detected