Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 3d3dad6

Browse files
committed
fix(Table): don't use virtual list for small tables
Only use a virtualized list to display tables with large (>1000) amount of table data. Closes DCOS-51574
1 parent b9df717 commit 3d3dad6

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/Table/Table.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class Table extends React.Component {
271271
}
272272

273273
getTBody(columns, data, sortBy, itemHeight) {
274-
const buildRowOptions = this.props.buildRowOptions;
274+
const { buildRowOptions, virtualizeDataThreshold } = this.props;
275275
let childToMeasure;
276276

277277
if (itemHeight === 0 && data.length) {
@@ -290,13 +290,32 @@ class Table extends React.Component {
290290
return <tbody>{this.getEmptyRowCell(columns)}</tbody>;
291291
}
292292

293+
const sortedData = sortData(columns, data, sortBy);
294+
295+
if (data.length < virtualizeDataThreshold) {
296+
// Do not use virtual list for "small" data sets
297+
return (
298+
<tbody>
299+
{sortedData.map((item, index) => {
300+
return this.getRowCells(
301+
columns,
302+
sortBy,
303+
buildRowOptions,
304+
item,
305+
index
306+
);
307+
})}
308+
</tbody>
309+
);
310+
}
311+
293312
return (
294313
<VirtualList
295314
className="table-virtual-list"
296315
container={this.container}
297316
itemBuffer={70}
298317
itemHeight={itemHeight}
299-
items={sortData(columns, data, sortBy)}
318+
items={sortedData}
300319
renderBufferItem={this.getBufferItem.bind(this, columns)}
301320
renderItem={this.getRowCells.bind(
302321
this,
@@ -336,7 +355,8 @@ Table.defaultProps = {
336355
return {};
337356
},
338357
sortBy: {},
339-
emptyMessage: "No data"
358+
emptyMessage: "No data",
359+
virtualizeDataThreshold: 1000
340360
};
341361

342362
Table.propTypes = {
@@ -405,7 +425,10 @@ Table.propTypes = {
405425
sortBy: PropTypes.shape({
406426
order: PropTypes.oneOf(["asc", "desc"]),
407427
prop: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
408-
})
428+
}),
429+
430+
// Optional cutoff at which to begin rendering a virtualized list
431+
virtualizeDataThreshold: PropTypes.number
409432
};
410433

411434
module.exports = Table;

src/Table/__tests__/Table-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ describe("Table", function() {
101101
"tr"
102102
);
103103

104-
expect(tableRows[2].children[0].textContent).toEqual("Zach");
105-
expect(tableRows[6].children[0].textContent).toEqual("Francis");
104+
expect(tableRows[1].children[0].textContent).toEqual("Zach");
105+
expect(tableRows[5].children[0].textContent).toEqual("Francis");
106106
});
107107

108108
it("should sort the data ascending on initial mount", function() {
@@ -122,8 +122,8 @@ describe("Table", function() {
122122
"tr"
123123
);
124124

125-
expect(tableRows[2].children[0].textContent).toEqual("Francis");
126-
expect(tableRows[6].children[0].textContent).toEqual("Zach");
125+
expect(tableRows[1].children[0].textContent).toEqual("Francis");
126+
expect(tableRows[5].children[0].textContent).toEqual("Zach");
127127
});
128128
});
129129
});

0 commit comments

Comments
 (0)