Skip to content

Commit e9c6fb3

Browse files
authored
chore: tweak row height measurement (#184)
1 parent a56ee2a commit e9c6fb3

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## NEXT VERSION
44

5+
- fix: horizontal scrollbar in flex mode with dynamic row height
6+
- chore: tweak row height measurement
7+
58
## v1.10.2 (2020-06-26)
69

710
- fix: regression of expansion with frozen columns

src/BaseTable.js

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class BaseTable extends React.PureComponent {
8888
this._handleColumnResizeStart = this._handleColumnResizeStart.bind(this);
8989
this._handleColumnResizeStop = this._handleColumnResizeStop.bind(this);
9090
this._handleColumnSort = this._handleColumnSort.bind(this);
91-
this._handleRowHeightMeasured = this._handleRowHeightMeasured.bind(this);
91+
this._handleFrozenRowHeightChange = this._handleFrozenRowHeightChange.bind(this);
92+
this._handleRowHeightChange = this._handleRowHeightChange.bind(this);
9293

9394
this._getLeftTableContainerStyle = memoize(getContainerStyle);
9495
this._getRightTableContainerStyle = memoize(getContainerStyle);
@@ -118,20 +119,13 @@ class BaseTable extends React.PureComponent {
118119
this._rightRowHeightMap = {};
119120
this._getRowHeight = this._getRowHeight.bind(this);
120121
this._updateRowHeights = debounce(() => {
121-
if (
122-
Object.keys(this._rowHeightMapBuffer).some(key => this._rowHeightMapBuffer[key] !== this._rowHeightMap[key])
123-
) {
124-
this._isResetting = true;
125-
this._rowHeightMap = { ...this._rowHeightMap, ...this._rowHeightMapBuffer };
126-
this.resetAfterRowIndex(this._resetIndex, false);
127-
this._rowHeightMapBuffer = {};
128-
this._resetIndex = null;
129-
this.forceUpdateTable();
130-
this._isResetting = false;
131-
} else {
132-
this._rowHeightMapBuffer = {};
133-
this._resetIndex = null;
134-
}
122+
this._isResetting = true;
123+
this._rowHeightMap = { ...this._rowHeightMap, ...this._rowHeightMapBuffer };
124+
this.resetAfterRowIndex(this._resetIndex, false);
125+
this._rowHeightMapBuffer = {};
126+
this._resetIndex = null;
127+
this.forceUpdateTable();
128+
this._isResetting = false;
135129
}, 0);
136130

137131
this._scroll = { scrollLeft: 0, scrollTop: 0 };
@@ -216,23 +210,13 @@ class BaseTable extends React.PureComponent {
216210
* Reset cached row heights after a specific rowIndex, should be used only in dynamic mode(estimatedRowHeight is provided)
217211
*/
218212
resetAfterRowIndex(rowIndex = 0, shouldForceUpdate = true) {
213+
if (!this.props.estimatedRowHeight) return;
214+
219215
this.table && this.table.resetAfterRowIndex(rowIndex, shouldForceUpdate);
220216
this.leftTable && this.leftTable.resetAfterRowIndex(rowIndex, shouldForceUpdate);
221217
this.rightTable && this.rightTable.resetAfterRowIndex(rowIndex, shouldForceUpdate);
222218
}
223219

224-
/**
225-
* Reset cached row heights, should be used only in dynamic mode(estimatedRowHeight is provided)
226-
*/
227-
resetRowHeightCache() {
228-
this._resetIndex = null;
229-
this._rowHeightMap = {};
230-
this._rowHeightMapBuffer = {};
231-
this._mainRowHeightMap = {};
232-
this._leftRowHeightMap = {};
233-
this._rightRowHeightMap = {};
234-
}
235-
236220
/**
237221
* Scroll to the specified offset.
238222
* Useful for animating position changes.
@@ -334,6 +318,7 @@ class BaseTable extends React.PureComponent {
334318
[this._prefixClass('row--customized')]: rowRenderer,
335319
});
336320

321+
const hasFrozenColumns = this.columnManager.hasFrozenColumns();
337322
const rowProps = {
338323
...extraProps,
339324
role: 'row',
@@ -356,8 +341,8 @@ class BaseTable extends React.PureComponent {
356341
expandIconRenderer: this.renderExpandIcon,
357342
onRowExpand: this._handleRowExpand,
358343
// for fixed table, we need to sync the hover state across the inner tables
359-
onRowHover: this.columnManager.hasFrozenColumns() ? this._handleRowHover : null,
360-
onRowHeightMeasured: this._handleRowHeightMeasured,
344+
onRowHover: hasFrozenColumns ? this._handleRowHover : null,
345+
onRowHeightChange: hasFrozenColumns ? this._handleFrozenRowHeightChange : this._handleRowHeightChange,
361346
};
362347

363348
return <TableRow {...rowProps} />;
@@ -688,7 +673,7 @@ class BaseTable extends React.PureComponent {
688673

689674
const _data = expandColumnKey ? this._flattenOnKeys(data, this.getExpandedRowKeys(), this.props.rowKey) : data;
690675
if (this._data !== _data) {
691-
this.resetRowHeightCache();
676+
this._resetRowHeightCache();
692677
this._data = _data;
693678
}
694679
// should be after `this._data` assigned
@@ -977,32 +962,44 @@ class BaseTable extends React.PureComponent {
977962
onColumnSort({ column, key, order });
978963
}
979964

980-
_handleRowHeightMeasured(rowKey, size, rowIndex, frozen) {
981-
if (this._resetIndex === null) this._resetIndex = rowIndex;
982-
else if (this._resetIndex > rowIndex) this._resetIndex = rowIndex;
965+
_handleFrozenRowHeightChange(rowKey, size, rowIndex, frozen) {
966+
if (!frozen) {
967+
this._mainRowHeightMap[rowKey] = size;
968+
} else if (frozen === FrozenDirection.RIGHT) {
969+
this._rightRowHeightMap[rowKey] = size;
970+
} else {
971+
this._leftRowHeightMap[rowKey] = size;
972+
}
983973

984-
if (this.columnManager.hasFrozenColumns()) {
985-
if (!frozen) {
986-
this._mainRowHeightMap[rowKey] = size;
987-
} else if (frozen === FrozenDirection.RIGHT) {
988-
this._rightRowHeightMap[rowKey] = size;
989-
} else {
990-
this._leftRowHeightMap[rowKey] = size;
991-
}
974+
const height = Math.max(
975+
this._mainRowHeightMap[rowKey] || 0,
976+
this._leftRowHeightMap[rowKey] || 0,
977+
this._rightRowHeightMap[rowKey] || 0
978+
);
992979

993-
this._rowHeightMapBuffer[rowKey] = Math.max(
994-
this._mainRowHeightMap[rowKey] || 0,
995-
this._leftRowHeightMap[rowKey] || 0,
996-
this._rightRowHeightMap[rowKey] || 0
997-
);
998-
} else {
999-
if (!this._rowHeightMap[rowKey] || this._rowHeightMap[rowKey] !== size) {
1000-
this._rowHeightMapBuffer[rowKey] = size;
1001-
}
980+
if (this._rowHeightMap[rowKey] !== height) {
981+
this._handleRowHeightChange(rowKey, height, rowIndex);
1002982
}
983+
}
984+
985+
_handleRowHeightChange(rowKey, size, rowIndex) {
986+
if (this._resetIndex === null) this._resetIndex = rowIndex;
987+
else if (this._resetIndex > rowIndex) this._resetIndex = rowIndex;
1003988

989+
this._rowHeightMapBuffer[rowKey] = size;
1004990
this._updateRowHeights();
1005991
}
992+
993+
_resetRowHeightCache() {
994+
if (!this.props.estimatedRowHeight) return;
995+
996+
this._resetIndex = null;
997+
this._rowHeightMapBuffer = {};
998+
this._rowHeightMap = {};
999+
this._mainRowHeightMap = {};
1000+
this._leftRowHeightMap = {};
1001+
this._rightRowHeightMap = {};
1002+
}
10061003
}
10071004

10081005
BaseTable.Column = Column;

src/TableRow.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ class TableRow extends React.PureComponent {
1616

1717
this._setRef = this._setRef.bind(this);
1818
this._handleExpand = this._handleExpand.bind(this);
19-
this._measureHeight = this._measureHeight.bind(this);
2019
}
2120

2221
componentDidMount() {
23-
this.props.estimatedRowHeight && this.props.rowIndex >= 0 && this._measureHeight();
22+
this.props.estimatedRowHeight && this.props.rowIndex >= 0 && this._measureHeight(true);
2423
}
2524

2625
componentDidUpdate(prevProps, prevState) {
@@ -32,7 +31,7 @@ class TableRow extends React.PureComponent {
3231
this.state.measured &&
3332
prevState.measured
3433
) {
35-
this.setState({ measured: false }, this._measureHeight);
34+
this.setState({ measured: false }, () => this._measureHeight());
3635
}
3736
}
3837

@@ -58,7 +57,7 @@ class TableRow extends React.PureComponent {
5857
getIsResetting,
5958
onRowHover,
6059
onRowExpand,
61-
onRowHeightMeasured,
60+
onRowHeightChange,
6261
...rest
6362
} = this.props;
6463
/* eslint-enable no-unused-vars */
@@ -113,14 +112,15 @@ class TableRow extends React.PureComponent {
113112
onRowExpand && onRowExpand({ expanded, rowData, rowIndex, rowKey });
114113
}
115114

116-
_measureHeight() {
117-
if (this.ref) {
118-
const { rowKey, onRowHeightMeasured, rowIndex, columns } = this.props;
119-
const height = this.ref.getBoundingClientRect().height;
120-
this.setState({ measured: true }, () => {
121-
onRowHeightMeasured(rowKey, height, rowIndex, columns[0] && !columns[0].__placeholder__ && columns[0].frozen);
122-
});
123-
}
115+
_measureHeight(initialMeasure) {
116+
if (!this.ref) return;
117+
118+
const { style, rowKey, onRowHeightChange, rowIndex, columns } = this.props;
119+
const height = this.ref.getBoundingClientRect().height;
120+
this.setState({ measured: true }, () => {
121+
if (initialMeasure || height !== style.height)
122+
onRowHeightChange(rowKey, height, rowIndex, columns[0] && !columns[0].__placeholder__ && columns[0].frozen);
123+
});
124124
}
125125

126126
_getEventHandlers(handlers = {}) {
@@ -187,7 +187,7 @@ TableRow.propTypes = {
187187
getIsResetting: PropTypes.func,
188188
onRowHover: PropTypes.func,
189189
onRowExpand: PropTypes.func,
190-
onRowHeightMeasured: PropTypes.func,
190+
onRowHeightChange: PropTypes.func,
191191
tagName: PropTypes.elementType,
192192
};
193193

0 commit comments

Comments
 (0)