Skip to content

Fix dynamic row height calculation with scaled parent element #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/BaseTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class BaseTable extends React.PureComponent {
}

renderRow({ isScrolling, columns, rowData, rowIndex, style }) {
const { rowClassName, rowRenderer, rowEventHandlers, expandColumnKey, estimatedRowHeight } = this.props;
const { rowClassName, rowRenderer, rowEventHandlers, expandColumnKey, estimatedRowHeight, scale } = this.props;

const rowClass = callOrReturn(rowClassName, { columns, rowData, rowIndex });
const extraProps = callOrReturn(this.props.rowProps, { columns, rowData, rowIndex });
Expand Down Expand Up @@ -368,6 +368,7 @@ class BaseTable extends React.PureComponent {
// for fixed table, we need to sync the hover state across the inner tables
onRowHover: hasFrozenColumns ? this._handleRowHover : null,
onRowHeightChange: hasFrozenColumns ? this._handleFrozenRowHeightChange : this._handleRowHeightChange,
scale,
};

return <TableRow {...rowProps} />;
Expand Down Expand Up @@ -1311,6 +1312,11 @@ BaseTable.propTypes = {
ExpandIcon: PropTypes.elementType,
SortIndicator: PropTypes.elementType,
}),

/**
* Scale factor applied to a parent element
*/
scale: PropTypes.number,
};

export default BaseTable;
10 changes: 8 additions & 2 deletions src/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ class TableRow extends React.PureComponent {
_measureHeight(initialMeasure) {
if (!this.ref) return;

const { style, rowKey, onRowHeightChange, rowIndex, columns } = this.props;
const height = this.ref.getBoundingClientRect().height;
const { style, rowKey, onRowHeightChange, rowIndex, columns, scale } = this.props;
let height = this.ref.getBoundingClientRect().height;

if (scale && scale > 0 && scale < 1) {
height = height / scale;
}

this.setState({ measured: true }, () => {
if (initialMeasure || height !== style.height)
onRowHeightChange(rowKey, height, rowIndex, columns[0] && !columns[0].__placeholder__ && columns[0].frozen);
Expand Down Expand Up @@ -189,6 +194,7 @@ TableRow.propTypes = {
onRowExpand: PropTypes.func,
onRowHeightChange: PropTypes.func,
tagName: PropTypes.elementType,
scale: PropTypes.number,
};

export default TableRow;
35 changes: 26 additions & 9 deletions website/src/examples/dynamic-row-heights.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,33 @@ export default class App extends React.Component {
>
Add item to top
</button>
<Table
fixed
columns={
!!this.state.toggle ? this.columns.slice(0, 4) : this.columns
}
estimatedRowHeight={40}
data={data}
sortBy={sortBy}
onColumnSort={this.onColumnSort}
<input
placeholder="Scale factor"
onChange={e => {
if (parseFloat(e.target.value)) {
this.setState({ scale: parseFloat(e.target.value) })
}
}}
/>
<div
style={
this.state.scale
? { transform: `scale(${this.state.scale})` }
: undefined
}
>
<Table
fixed
columns={
!!this.state.toggle ? this.columns.slice(0, 4) : this.columns
}
estimatedRowHeight={40}
data={data}
sortBy={sortBy}
onColumnSort={this.onColumnSort}
scale={this.state.scale}
/>
</div>
</>
)
}
Expand Down