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

[v4] [table] fix(Table2): more column resizing issues #5154

Merged
merged 1 commit into from
Mar 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/table/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,23 @@ export const Utils = {
const approxCellHeight = approxNumLinesDesired * approxLineHeight;
return approxCellHeight;
},

/**
* Shallow comparison of potentially sparse arrays.
*
* @returns true if the array values are equal
*/
compareSparseArrays(
a: Array<number | null | undefined> | undefined,
b: Array<number | null | undefined> | undefined,
): boolean {
return (
a !== undefined &&
b !== undefined &&
a.length === b.length &&
a.every((aValue, index) => aValue === b[index])
);
},
};

// table is nearly deprecated, let's not block on code coverage
Expand Down
21 changes: 9 additions & 12 deletions packages/table/src/table2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap
columnWidths,
defaultRowHeight,
defaultColumnWidth,
enableRowHeader,
numRows,
rowHeights,
selectedRegions = [] as Region[],
Expand Down Expand Up @@ -308,6 +309,10 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap
syncViewportPosition: this.syncViewportPosition,
});
this.hotkeys = getHotkeysFromProps(props, this.hotkeysImpl);

if (enableRowHeader === false) {
this.didRowHeaderMount = true;
}
}

// Instance methods
Expand Down Expand Up @@ -549,8 +554,8 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap

const shouldInvalidateGrid =
didChildrenChange ||
this.props.columnWidths !== prevState.columnWidths ||
this.props.rowHeights !== prevState.rowHeights ||
!Utils.compareSparseArrays(this.props.columnWidths, prevState.columnWidths) ||
!Utils.compareSparseArrays(this.props.rowHeights, prevState.rowHeights) ||
this.props.numRows !== prevProps.numRows ||
(this.props.forceRerenderOnSelectionChange && this.props.selectedRegions !== prevProps.selectedRegions);

Expand Down Expand Up @@ -1210,11 +1215,7 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap

this.invalidateGrid();
this.setState({ columnWidths });

const { onColumnWidthChanged } = this.props;
if (onColumnWidthChanged != null) {
onColumnWidthChanged(columnIndex, width);
}
this.props.onColumnWidthChanged?.(columnIndex, width);
};

private handleRowHeightChanged = (rowIndex: number, height: number) => {
Expand All @@ -1236,11 +1237,7 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap

this.invalidateGrid();
this.setState({ rowHeights });

const { onRowHeightChanged } = this.props;
if (onRowHeightChanged != null) {
onRowHeightChanged(rowIndex, height);
}
this.props.onRowHeightChanged?.(rowIndex, height);
};

private handleRootScroll = (_event: React.UIEvent<HTMLElement>) => {
Expand Down