Skip to content

Commit

Permalink
[table] fix: invalidate grid before rendering table when dimensions c…
Browse files Browse the repository at this point in the history
…hange (#3888)
  • Loading branch information
Nifdee authored and adidahiya committed Jan 13, 2020
1 parent b9fe249 commit a0617fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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()) {
// 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

1 comment on commit a0617fe

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

[table] fix: invalidate grid before rendering table when dimensions change (#3888)

Previews: documentation | landing | table

Please sign in to comment.