Skip to content

Commit e6504e7

Browse files
fix(virtual-core): fix total size calculation for single item in multi-lane (#967)
1 parent b143776 commit e6504e7

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/virtual-core/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ export class Virtualizer<
10461046
} else {
10471047
const endByLane = Array<number | null>(this.options.lanes).fill(null)
10481048
let endIndex = measurements.length - 1
1049-
while (endIndex > 0 && endByLane.some((val) => val === null)) {
1049+
while (endIndex >= 0 && endByLane.some((val) => val === null)) {
10501050
const item = measurements[endIndex]!
10511051
if (endByLane[item.lane] === null) {
10521052
endByLane[item.lane] = item.end
@@ -1162,7 +1162,7 @@ function calculateRange({
11621162
// Expand backward until we include all lanes' visible items
11631163
// closer to the top
11641164
const startPerLane = Array(lanes).fill(scrollOffset + outerSize)
1165-
while (startIndex > 0 && startPerLane.some((pos) => pos >= scrollOffset)) {
1165+
while (startIndex >= 0 && startPerLane.some((pos) => pos >= scrollOffset)) {
11661166
const item = measurements[startIndex]!
11671167
startPerLane[item.lane] = item.start
11681168
startIndex--

packages/virtual-core/tests/index.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ test('should return empty items for empty scroll element', () => {
1616
})
1717
expect(virtualizer.getVirtualItems()).toEqual([])
1818
})
19+
20+
test('should return correct total size with one item and multiple lanes', () => {
21+
const virtualizer = new Virtualizer({
22+
count: 1,
23+
lanes: 2,
24+
estimateSize: () => 50,
25+
getScrollElement: () => null,
26+
scrollToFn: vi.fn(),
27+
observeElementRect: vi.fn(),
28+
observeElementOffset: vi.fn(),
29+
})
30+
expect(virtualizer.getTotalSize()).toBe(50)
31+
})

0 commit comments

Comments
 (0)