Skip to content

Commit c560b18

Browse files
author
Brian Vaughn
committed
Variable size list and grid no longer call item size getters when count is 0
1 parent 9153d3b commit c560b18

4 files changed

+47
-7
lines changed

src/__tests__/VariableSizeGrid.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { VariableSizeGrid } from '..';
55
const findScrollContainer = rendered => rendered.root.children[0].children[0];
66

77
describe('VariableSizeGrid', () => {
8-
let itemRenderer, defaultProps, onItemsRendered;
8+
let columnWidth, defaultProps, itemRenderer, onItemsRendered, rowHeight;
99

1010
// Use PureComponent to test memoization.
1111
// Pass through to itemRenderer mock for easier test assertions.
@@ -30,21 +30,39 @@ describe('VariableSizeGrid', () => {
3030
<div style={style}>{JSON.stringify(rest, null, 2)}</div>
3131
));
3232
onItemsRendered = jest.fn();
33+
columnWidth = jest.fn(index => 50 + index);
34+
rowHeight = jest.fn(index => 25 + index);
3335
defaultProps = {
3436
children: PureItemRenderer,
3537
columnCount: 10,
36-
columnWidth: index => 50 + index,
38+
columnWidth,
3739
height: 100,
3840
onItemsRendered,
3941
rowCount: 20,
40-
rowHeight: index => 25 + index,
42+
rowHeight,
4143
width: 200,
4244
};
4345
});
4446

4547
// Much of the shared Grid functionality is already tested by VariableSizeGrid tests.
4648
// This test covers functionality that is unique to VariableSizeGrid.
4749

50+
it('should render an empty grid', () => {
51+
ReactTestRenderer.create(
52+
<VariableSizeGrid {...defaultProps} columnCount={0} rowCount={0} />
53+
);
54+
ReactTestRenderer.create(
55+
<VariableSizeGrid {...defaultProps} columnCount={0} />
56+
);
57+
ReactTestRenderer.create(
58+
<VariableSizeGrid {...defaultProps} rowCount={0} />
59+
);
60+
expect(itemRenderer).not.toHaveBeenCalled();
61+
expect(columnWidth).not.toHaveBeenCalled();
62+
expect(rowHeight).not.toHaveBeenCalled();
63+
expect(onItemsRendered).not.toHaveBeenCalled();
64+
});
65+
4866
it('changing item size does not impact the rendered items', () => {
4967
const rendered = ReactTestRenderer.create(
5068
<VariableSizeGrid {...defaultProps} />

src/__tests__/VariableSizeList.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { VariableSizeList } from '..';
55
const findScrollContainer = rendered => rendered.root.children[0].children[0];
66

77
describe('VariableSizeList', () => {
8-
let itemRenderer, defaultProps, onItemsRendered;
8+
let itemRenderer, itemSize, defaultProps, onItemsRendered;
99

1010
// Use PureComponent to test memoization.
1111
// Pass through to itemRenderer mock for easier test assertions.
@@ -21,13 +21,14 @@ describe('VariableSizeList', () => {
2121
itemRenderer = jest.fn(({ style, ...rest }) => (
2222
<div style={style}>{JSON.stringify(rest, null, 2)}</div>
2323
));
24+
itemSize = jest.fn(index => 25 + index);
2425
onItemsRendered = jest.fn();
2526
defaultProps = {
2627
children: PureItemRenderer,
2728
estimatedItemSize: 25,
2829
height: 100,
2930
itemCount: 20,
30-
itemSize: index => 25 + index,
31+
itemSize,
3132
onItemsRendered,
3233
width: 50,
3334
};
@@ -36,6 +37,15 @@ describe('VariableSizeList', () => {
3637
// Much of the shared List functionality is already tested by FixedSizeList tests.
3738
// This test covers functionality that is unique to VariableSizeList.
3839

40+
it('should render an empty list', () => {
41+
ReactTestRenderer.create(
42+
<VariableSizeList {...defaultProps} itemCount={0} />
43+
);
44+
expect(itemSize).not.toHaveBeenCalled();
45+
expect(itemRenderer).not.toHaveBeenCalled();
46+
expect(onItemsRendered).not.toHaveBeenCalled();
47+
});
48+
3949
it('changing itemSize does not impact the rendered items', () => {
4050
const rendered = ReactTestRenderer.create(
4151
<VariableSizeList {...defaultProps} />

src/createGridComponent.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,13 @@ export default function createGridComponent({
501501
_getItemStyleCache = memoizeOne((_, __) => ({}));
502502

503503
_getHorizontalRangeToRender(): [number, number, number, number] {
504-
const { columnCount, overscanCount } = this.props;
504+
const { columnCount, overscanCount, rowCount } = this.props;
505505
const { horizontalScrollDirection, scrollLeft } = this.state;
506506

507+
if (columnCount === 0 || rowCount === 0) {
508+
return [0, 0, 0, 0];
509+
}
510+
507511
const startIndex = getColumnStartIndexForOffset(
508512
this.props,
509513
scrollLeft,
@@ -536,9 +540,13 @@ export default function createGridComponent({
536540
}
537541

538542
_getVerticalRangeToRender(): [number, number, number, number] {
539-
const { rowCount, overscanCount } = this.props;
543+
const { columnCount, rowCount, overscanCount } = this.props;
540544
const { verticalScrollDirection, scrollTop } = this.state;
541545

546+
if (columnCount === 0 || rowCount === 0) {
547+
return [0, 0, 0, 0];
548+
}
549+
542550
const startIndex = getRowStartIndexForOffset(
543551
this.props,
544552
scrollTop,

src/createListComponent.js

+4
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ export default function createListComponent({
416416
const { itemCount, overscanCount } = this.props;
417417
const { scrollDirection, scrollOffset } = this.state;
418418

419+
if (itemCount === 0) {
420+
return [0, 0, 0, 0];
421+
}
422+
419423
const startIndex = getStartIndexForOffset(
420424
this.props,
421425
scrollOffset,

0 commit comments

Comments
 (0)