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 lifecycle bugs scrolling + resizing #5151

Merged
merged 2 commits into from
Mar 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 8 additions & 46 deletions packages/table/src/table2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import {
resizeRowsByApproximateHeight,
resizeRowsByTallestCell,
} from "./resizeRows";
import { getHotkeysFromProps, isSelectionModeEnabled } from "./table2Utils";
import { compareChildren, getHotkeysFromProps, isSelectionModeEnabled } from "./table2Utils";
import { TableBody } from "./tableBody";
import { TableHotkeys } from "./tableHotkeys";
import type { TableProps, TablePropsDefaults, TablePropsWithDefaults } from "./tableProps";
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap
}

const newChildrenArray = React.Children.toArray(children) as Array<React.ReactElement<ColumnProps>>;
const didChildrenChange = newChildrenArray !== state.childrenArray;
const didChildrenChange = !compareChildren(newChildrenArray, state.childrenArray);
const numCols = newChildrenArray.length;

let newColumnWidths = columnWidths;
Expand Down Expand Up @@ -537,41 +537,15 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap
this.didCompletelyMount = false;
}

public getSnapshotBeforeUpdate() {
const { viewportRect } = this.state;

if (viewportRect === undefined) {
return {};
}

const grid = this.validateGrid();
const tableBottom = grid.getCumulativeHeightAt(grid.numRows - 1);
const tableRight = grid.getCumulativeWidthAt(grid.numCols - 1);

const nextScrollTop =
tableBottom < viewportRect.top + viewportRect.height
? // scroll the last row into view
Math.max(0, tableBottom - viewportRect.height)
: undefined;

const nextScrollLeft =
tableRight < viewportRect.left + viewportRect.width
? // scroll the last column into view
Math.max(0, tableRight - viewportRect.width)
: undefined;

// these will only be defined if they differ from viewportRect
return { nextScrollLeft, nextScrollTop };
}

public componentDidUpdate(prevProps: TableProps, prevState: TableState, snapshot: TableSnapshot) {
super.componentDidUpdate(prevProps, prevState, snapshot);
public componentDidUpdate(prevProps: TableProps, prevState: TableState) {
super.componentDidUpdate(prevProps, prevState);
this.hotkeysImpl.setState(this.state);
this.hotkeysImpl.setProps(this.props);

const didChildrenChange =
(React.Children.toArray(this.props.children) as Array<React.ReactElement<ColumnProps>>) !==
this.state.childrenArray;
const didChildrenChange = !compareChildren(
React.Children.toArray(this.props.children) as Array<React.ReactElement<ColumnProps>>,
this.state.childrenArray,
);

const shouldInvalidateGrid =
didChildrenChange ||
Expand All @@ -589,18 +563,6 @@ export class Table2 extends AbstractComponent2<TableProps, TableState, TableSnap
this.updateLocator();
}

// When true, we'll need to imperatively synchronize quadrant views after
// the update. This check lets us avoid expensively diff'ing columnWidths
// and rowHeights in <TableQuadrantStack> on each update.
const didUpdateColumnOrRowSizes =
!CoreUtils.arraysEqual(this.state.columnWidths, prevState.columnWidths) ||
!CoreUtils.arraysEqual(this.state.rowHeights, prevState.rowHeights);

if (didUpdateColumnOrRowSizes) {
this.quadrantStackInstance?.synchronizeQuadrantViews();
this.syncViewportPosition(snapshot);
}

const shouldInvalidateHotkeys =
this.props.getCellClipboardData !== prevProps.getCellClipboardData ||
this.props.enableFocusedCell !== prevProps.enableFocusedCell ||
Expand Down
16 changes: 15 additions & 1 deletion packages/table/src/table2Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import * as React from "react";

import { HotkeyConfig } from "@blueprintjs/core";

import type { ColumnProps } from "./column";
import { RegionCardinality } from "./regions";
import type { TableHotkeys } from "./tableHotkeys";
import { TablePropsWithDefaults } from "./tableProps";
import type { TablePropsWithDefaults } from "./tableProps";

export function isSelectionModeEnabled(
props: TablePropsWithDefaults,
Expand Down Expand Up @@ -142,3 +143,16 @@ export function getHotkeysFromProps(props: TablePropsWithDefaults, hotkeysImpl:

return hotkeys;
}

/**
* @returns true if new and old children arrays are the same
*/
export function compareChildren(
newChildren: Array<React.ReactElement<ColumnProps>>,
oldChildren: Array<React.ReactElement<ColumnProps>>,
): boolean {
return (
newChildren.length === oldChildren.length &&
newChildren.every((child, index) => child.key === oldChildren[index].key)
);
}