({
rows,
columns,
}: {
rows: Array<number>
columns: Array<number>
})
| 149 | } |
| 150 | |
| 151 | function GridVirtualizerDynamic({ |
| 152 | rows, |
| 153 | columns, |
| 154 | }: { |
| 155 | rows: Array<number> |
| 156 | columns: Array<number> |
| 157 | }) { |
| 158 | const parentRef = React.useRef<HTMLDivElement>(null) |
| 159 | |
| 160 | const rowVirtualizer = useVirtualizer({ |
| 161 | count: rows.length, |
| 162 | getScrollElement: () => parentRef.current, |
| 163 | estimateSize: () => 50, |
| 164 | paddingStart: 200, |
| 165 | paddingEnd: 200, |
| 166 | indexAttribute: 'data-row-index', |
| 167 | }) |
| 168 | |
| 169 | const columnVirtualizer = useVirtualizer({ |
| 170 | horizontal: true, |
| 171 | count: columns.length, |
| 172 | getScrollElement: () => parentRef.current, |
| 173 | estimateSize: () => 50, |
| 174 | paddingStart: 200, |
| 175 | paddingEnd: 200, |
| 176 | indexAttribute: 'data-column-index', |
| 177 | }) |
| 178 | |
| 179 | const [show, setShow] = React.useState(true) |
| 180 | |
| 181 | const halfWay = Math.floor(rows.length / 2) |
| 182 | |
| 183 | return ( |
| 184 | <> |
| 185 | <button onClick={() => setShow((old) => !old)}>Toggle</button> |
| 186 | <button onClick={() => rowVirtualizer.scrollToIndex(halfWay)}> |
| 187 | Scroll to index {halfWay} |
| 188 | </button> |
| 189 | <button onClick={() => rowVirtualizer.scrollToIndex(rows.length - 1)}> |
| 190 | Scroll to index {rows.length - 1} |
| 191 | </button> |
| 192 | {show ? ( |
| 193 | <div |
| 194 | ref={parentRef} |
| 195 | className="List" |
| 196 | style={{ |
| 197 | height: `400px`, |
| 198 | width: `500px`, |
| 199 | overflow: 'auto', |
| 200 | }} |
| 201 | > |
| 202 | <div |
| 203 | style={{ |
| 204 | height: `${rowVirtualizer.getTotalSize()}px`, |
| 205 | width: `${columnVirtualizer.getTotalSize()}px`, |
| 206 | position: 'relative', |
| 207 | }} |
| 208 | > |
nothing calls this directly
no test coverage detected