Skip to content

Commit

Permalink
fix: prevent returning items when container size is zero (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Oct 3, 2023
1 parent 5e12ecd commit e43e03e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/react/dynamic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
Expand Down Expand Up @@ -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)`,
}}
>
Expand Down
2 changes: 1 addition & 1 deletion packages/react-virtual/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/virtual-core/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
})
15 changes: 9 additions & 6 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit e43e03e

Please sign in to comment.