From e43e03e500588b2da36f184ecaba2b8e5a506596 Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Tue, 3 Oct 2023 22:30:00 +0200 Subject: [PATCH] fix: prevent returning items when container size is zero (#598) --- examples/react/dynamic/src/main.tsx | 4 ++-- packages/react-virtual/__tests__/index.test.tsx | 2 +- packages/virtual-core/__tests__/index.test.ts | 12 ++++++++++++ packages/virtual-core/src/index.ts | 15 +++++++++------ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/examples/react/dynamic/src/main.tsx b/examples/react/dynamic/src/main.tsx index 1b765bc4..6539b485 100644 --- a/examples/react/dynamic/src/main.tsx +++ b/examples/react/dynamic/src/main.tsx @@ -74,7 +74,7 @@ function RowVirtualizerDynamic() { top: 0, left: 0, width: '100%', - transform: `translateY(${items[0].start}px)`, + transform: `translateY(${items[0]?.start ?? 0}px)`, }} > {items.map((virtualRow) => ( @@ -131,7 +131,7 @@ const RowVirtualizerDynamicWindow = () => { left: 0, width: '100%', transform: `translateY(${ - items[0].start - virtualizer.options.scrollMargin + items[0] ? items[0].start - virtualizer.options.scrollMargin : 0 }px)`, }} > diff --git a/packages/react-virtual/__tests__/index.test.tsx b/packages/react-virtual/__tests__/index.test.tsx index b85dae62..6455fdf3 100644 --- a/packages/react-virtual/__tests__/index.test.tsx +++ b/packages/react-virtual/__tests__/index.test.tsx @@ -148,7 +148,7 @@ test('should render given dynamic size after scroll', () => { expect(screen.queryByText('Row 6')).toBeInTheDocument() expect(screen.queryByText('Row 7')).not.toBeInTheDocument() - expect(renderer).toHaveBeenCalledTimes(3) + expect(renderer).toHaveBeenCalledTimes(2) }) test('should use rangeExtractor', () => { diff --git a/packages/virtual-core/__tests__/index.test.ts b/packages/virtual-core/__tests__/index.test.ts index 8607c68c..9a1126f4 100644 --- a/packages/virtual-core/__tests__/index.test.ts +++ b/packages/virtual-core/__tests__/index.test.ts @@ -3,3 +3,15 @@ import { Virtualizer } from '../src/index' test('should export the Virtualizer class', () => { expect(Virtualizer).toBeDefined() }) + +test('shoul', () => { + const virtualizer = new Virtualizer({ + count: 100, + getScrollElement: () => null, + estimateSize: () => 50, + scrollToFn: jest.fn(), + observeElementRect: jest.fn(), + observeElementOffset: jest.fn(), + }) + expect(virtualizer.getVirtualItems()).toEqual([]) +}) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 3ac1069d..e60a2910 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -587,13 +587,16 @@ export class Virtualizer< this.calculateRange(), this.options.overscan, this.options.count, + this.getSize(), ], - (rangeExtractor, range, overscan, count) => { - return rangeExtractor({ - ...range, - overscan, - count, - }) + (rangeExtractor, range, overscan, count, outerSize) => { + return outerSize === 0 + ? [] + : rangeExtractor({ + ...range, + overscan, + count, + }) }, { key: process.env.NODE_ENV !== 'production' && 'getIndexes',