({
count = 200,
overscan,
height = 200,
width = 200,
itemSize,
rangeExtractor,
dynamic,
}: ListProps)
| 30 | } |
| 31 | |
| 32 | function List({ |
| 33 | count = 200, |
| 34 | overscan, |
| 35 | height = 200, |
| 36 | width = 200, |
| 37 | itemSize, |
| 38 | rangeExtractor, |
| 39 | dynamic, |
| 40 | }: ListProps) { |
| 41 | renderer() |
| 42 | |
| 43 | const parentRef = React.useRef<HTMLDivElement>(null) |
| 44 | |
| 45 | const elementRectCallbackRef = React.useRef< |
| 46 | ((rect: { height: number; width: number }) => void) | null |
| 47 | >(null) |
| 48 | |
| 49 | const rowVirtualizer = useVirtualizer({ |
| 50 | count, |
| 51 | getScrollElement: () => parentRef.current, |
| 52 | estimateSize: () => 50, |
| 53 | overscan, |
| 54 | observeElementRect: (_, cb) => { |
| 55 | cb({ height, width }) |
| 56 | elementRectCallbackRef.current = cb |
| 57 | }, |
| 58 | measureElement: () => itemSize ?? 0, |
| 59 | rangeExtractor, |
| 60 | }) |
| 61 | |
| 62 | React.useEffect(() => { |
| 63 | elementRectCallbackRef.current?.({ height, width }) |
| 64 | }, [height, width]) |
| 65 | |
| 66 | const measureElement = dynamic ? rowVirtualizer.measureElement : undefined |
| 67 | |
| 68 | const items = rowVirtualizer.getVirtualItems() |
| 69 | |
| 70 | return ( |
| 71 | <div |
| 72 | ref={parentRef} |
| 73 | style={{ height, width, overflow: 'auto' }} |
| 74 | data-testid="scroller" |
| 75 | > |
| 76 | <div |
| 77 | style={{ |
| 78 | height: rowVirtualizer.getTotalSize(), |
| 79 | width: '100%', |
| 80 | position: 'relative', |
| 81 | }} |
| 82 | > |
| 83 | {items.map((virtualRow) => ( |
| 84 | <div |
| 85 | data-testid={`item-${virtualRow.key}`} |
| 86 | key={virtualRow.key} |
| 87 | data-index={virtualRow.index} |
| 88 | ref={measureElement} |
| 89 | style={{ |
nothing calls this directly
no test coverage detected