Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## NEXT VERSION

- feat: add the ability to pass function in `estimatedRowHeight` to determine the initial height of rows

## v1.11.3 (2020-08-24)

- fix: remove propTypes for Column.key
Expand Down
16 changes: 12 additions & 4 deletions src/BaseTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
renderElement,
normalizeColumns,
getScrollbarSize as defaultGetScrollbarSize,
getEstimatedTotalRowsHeight,
isObjectEqual,
callOrReturn,
hasChildren,
Expand Down Expand Up @@ -120,6 +121,7 @@ class BaseTable extends React.PureComponent {
this._mainRowHeightMap = {};
this._leftRowHeightMap = {};
this._rightRowHeightMap = {};
this._getEstimatedTotalRowsHeight = memoize(getEstimatedTotalRowsHeight);
this._getRowHeight = this._getRowHeight.bind(this);
this._updateRowHeights = debounce(() => {
this._isResetting = true;
Expand Down Expand Up @@ -185,7 +187,9 @@ class BaseTable extends React.PureComponent {
const { rowHeight, estimatedRowHeight } = this.props;

if (estimatedRowHeight) {
return this.table ? this.table.getTotalRowsHeight() : this._data.length * estimatedRowHeight;
return this.table
? this.table.getTotalRowsHeight()
: this._getEstimatedTotalRowsHeight(this._data, estimatedRowHeight);
}
return this._data.length * rowHeight;
}
Expand Down Expand Up @@ -716,7 +720,7 @@ class BaseTable extends React.PureComponent {
[`${classPrefix}--has-frozen-rows`]: frozenData.length > 0,
[`${classPrefix}--has-frozen-columns`]: this.columnManager.hasFrozenColumns(),
[`${classPrefix}--disabled`]: disabled,
[`${classPrefix}--dynamic`]: estimatedRowHeight > 0,
[`${classPrefix}--dynamic`]: !!estimatedRowHeight,
});
return (
<div ref={this._setContainerRef} className={cls} style={containerStyle}>
Expand Down Expand Up @@ -785,7 +789,10 @@ class BaseTable extends React.PureComponent {
// for dynamic row height
_getRowHeight(rowIndex) {
const { estimatedRowHeight, rowKey } = this.props;
return this._rowHeightMap[this._data[rowIndex][rowKey]] || estimatedRowHeight;
return (
this._rowHeightMap[this._data[rowIndex][rowKey]] ||
callOrReturn(estimatedRowHeight, { rowData: this._data[rowIndex], rowIndex })
);
}

_getIsResetting() {
Expand Down Expand Up @@ -1103,8 +1110,9 @@ BaseTable.propTypes = {
rowHeight: PropTypes.number,
/**
* Estimated row height, the real height will be measure dynamically according to the content
* The callback is of the shape of `({ rowData, rowIndex }) => number`
*/
estimatedRowHeight: PropTypes.number,
estimatedRowHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]),
/**
* The height of the table header, set to 0 to hide the header, could be an array to render multi headers.
*/
Expand Down
10 changes: 7 additions & 3 deletions src/GridTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FixedSizeGrid, VariableSizeGrid } from 'react-window';
import memoize from 'memoize-one';

import Header from './TableHeader';
import { getEstimatedTotalRowsHeight } from './utils';

/**
* A wrapper of the Grid for internal only
Expand All @@ -23,6 +24,7 @@ class GridTable extends React.PureComponent {
if (!this.props.estimatedRowHeight) return;
this.bodyRef && this.bodyRef.resetAfterColumnIndex(0, false);
});
this._getEstimatedTotalRowsHeight = memoize(getEstimatedTotalRowsHeight);

this.renderRow = this.renderRow.bind(this);
}
Expand Down Expand Up @@ -59,7 +61,9 @@ class GridTable extends React.PureComponent {
const { data, rowHeight, estimatedRowHeight } = this.props;

if (estimatedRowHeight) {
return (this.innerRef && this.innerRef.clientHeight) || data.length * estimatedRowHeight;
return (
(this.innerRef && this.innerRef.clientHeight) || this._getEstimatedTotalRowsHeight(data, estimatedRowHeight)
);
}
return data.length * rowHeight;
}
Expand Down Expand Up @@ -114,7 +118,7 @@ class GridTable extends React.PureComponent {
width={width}
height={Math.max(height - headerHeight - frozenRowsHeight, 0)}
rowHeight={estimatedRowHeight ? getRowHeight : rowHeight}
estimatedRowHeight={estimatedRowHeight}
estimatedRowHeight={typeof estimatedRowHeight === 'function' ? undefined : estimatedRowHeight}
rowCount={data.length}
overscanRowCount={overscanRowCount}
columnWidth={estimatedRowHeight ? this._getBodyWidth : bodyWidth}
Expand Down Expand Up @@ -198,7 +202,7 @@ GridTable.propTypes = {
headerWidth: PropTypes.number.isRequired,
bodyWidth: PropTypes.number.isRequired,
rowHeight: PropTypes.number.isRequired,
estimatedRowHeight: PropTypes.number,
estimatedRowHeight: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
getRowHeight: PropTypes.func,
columns: PropTypes.arrayOf(PropTypes.object).isRequired,
data: PropTypes.array.isRequired,
Expand Down
2 changes: 1 addition & 1 deletion src/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ TableRow.propTypes = {
rowRenderer: PropTypes.oneOfType([PropTypes.func, PropTypes.element]),
cellRenderer: PropTypes.func,
expandIconRenderer: PropTypes.func,
estimatedRowHeight: PropTypes.number,
estimatedRowHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]),
getIsResetting: PropTypes.func,
onRowHover: PropTypes.func,
onRowExpand: PropTypes.func,
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,9 @@ export function removeClassName(el, className) {
el.className = el.className.replace(new RegExp(`(?:^|\\s)${className}(?!\\S)`, 'g'), '');
}
}

export function getEstimatedTotalRowsHeight(data, estimatedRowHeight) {
return typeof estimatedRowHeight === 'function'
? data.reduce((height, rowData, rowIndex) => height + estimatedRowHeight({ rowData, rowIndex }), 0)
: data.length * estimatedRowHeight;
}
9 changes: 8 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,15 @@ declare module 'react-base-table' {
rowHeight?: number;
/**
* Estimated row height, the real height will be measure dynamically according to the content
* The callback is of the shape of `({ rowData, rowIndex }) => number`
*/
estimatedRowHeight?: number;
estimatedRowHeight?: CallOrReturn<
number,
{
rowData: T;
rowIndex: number;
}
>;
/**
* The height of the table header, set to 0 to hide the header, could be an array to render multi headers.
*/
Expand Down