Skip to content
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

Invalidate grid before rendering table when dimensions change #3888

Merged
merged 3 commits into from
Jan 13, 2020
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
11 changes: 11 additions & 0 deletions packages/table/src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,10 @@ export class Table extends AbstractComponent2<ITableProps, ITableState, ITableSn
enableColumnInteractionBar,
} = this.props;
const { horizontalGuides, numFrozenColumnsClamped, numFrozenRowsClamped, verticalGuides } = this.state;
if (!this.gridDimensionsMatchProps()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should go into componentDidUpdate or another lifecycle method... I'm poking around at different places to move this check

// Ensure we're rendering the correct number of rows & columns
this.invalidateGrid();
}
this.validateGrid();

const classes = classNames(
Expand Down Expand Up @@ -993,6 +997,13 @@ export class Table extends AbstractComponent2<ITableProps, ITableState, ITableSn
}
}

private gridDimensionsMatchProps() {
const { children, numRows } = this.props;
return (
this.grid != null && this.grid.numCols === React.Children.count(children) && this.grid.numRows === numRows
);
}

// Hotkeys
// =======

Expand Down
18 changes: 18 additions & 0 deletions packages/table/test/tableTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,24 @@ describe("<Table>", function(this) {
expect(onCompleteRenderSpy.callCount, "call count on update").to.equal(2);
});

it("does not try to render cells that no longer exist", () => {
const onCompleteRenderSpy = sinon.spy();
let numRows = 2;
const cellRenderer = (rowIndex: number) => {
if (rowIndex >= numRows) {
throw new Error(`There is no row with index ${rowIndex}`);
}
return <Cell>Row ${rowIndex}</Cell>;
};
const table = mount(
<Table numRows={numRows} onCompleteRender={onCompleteRenderSpy} renderMode={RenderMode.NONE}>
<Column cellRenderer={cellRenderer} />
</Table>,
);
numRows = 1;
expect(() => table.setProps({ numRows })).does.not.throw();
});

it("triggers immediately on mount with RenderMode.BATCH_ON_UPDATE", () => {
const onCompleteRenderSpy = sinon.spy();
mount(
Expand Down