Skip to content

Commit d559553

Browse files
authored
Revisit SCU (#1764)
* Ignore cellMetaData in SCU Improve scrollLeft logic for initial render * Add some comments * Destructure props * Fix typo * Do not ignore cellMetaData
1 parent 009db98 commit d559553

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

packages/react-data-grid/src/Canvas.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface CanvasProps<R> extends SharedViewportProps<R>, SharedViewportSt
4545
onScroll(position: ScrollPosition): void;
4646
}
4747

48-
type RendererProps<R> = Pick<CanvasProps<R>, 'columns' | 'cellMetaData' | 'colVisibleStartIdx' | 'colVisibleEndIdx' | 'colOverscanEndIdx' | 'colOverscanStartIdx' | 'lastFrozenColumnIndex' | 'isScrolling'> & {
48+
type RendererProps<R> = Pick<CanvasProps<R>, 'columns' | 'cellMetaData' | 'colOverscanEndIdx' | 'colOverscanStartIdx' | 'lastFrozenColumnIndex' | 'isScrolling'> & {
4949
ref(row: (RowRenderer<R> & React.Component<RowRendererProps<R>>) | null): void;
5050
key: number;
5151
idx: number;
@@ -289,8 +289,6 @@ export default class Canvas<R> extends React.PureComponent<CanvasProps<R>> {
289289
isSelected: this.isRowSelected(rowIdx, row),
290290
cellMetaData,
291291
subRowDetails,
292-
colVisibleStartIdx,
293-
colVisibleEndIdx,
294292
colOverscanStartIdx,
295293
colOverscanEndIdx,
296294
lastFrozenColumnIndex,

packages/react-data-grid/src/Cell.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CellActions from './Cell/CellActions';
77
import CellExpand from './Cell/CellExpander';
88
import CellContent from './Cell/CellContent';
99
import { isFrozen } from './ColumnUtils';
10+
import { isPositionStickySupported } from './utils';
1011

1112
function getSubRowOptions<R>({ rowIdx, idx, rowData, expandableOptions: expandArgs }: CellProps<R>): SubRowOptions<R> {
1213
return { rowIdx, idx, rowData, expandArgs };
@@ -27,25 +28,15 @@ export default class Cell<R> extends React.Component<CellProps<R>> implements Ce
2728
private readonly cell = React.createRef<HTMLDivElement>();
2829

2930
shouldComponentUpdate(nextProps: CellProps<R>) {
31+
// TODO: optimize cellMetatData as it is recreated whenever `ReactDataGrid` renders
32+
// On the modern browsers we are using position sticky so scrollLeft/setScrollLeft is not required
33+
// On the legacy browsers scrollLeft sets the initial position so it can be safely ignored in the subsequent renders. Scrolling is handled by the setScrollLeft method
3034
const { scrollLeft, ...rest } = this.props;
3135
const { scrollLeft: nextScrollLeft, ...nextRest } = nextProps;
3236

3337
return !shallowEqual(rest, nextRest);
3438
}
3539

36-
componentDidMount() {
37-
const { scrollLeft } = this.props;
38-
if (scrollLeft !== undefined) {
39-
this.setScrollLeft(scrollLeft);
40-
}
41-
}
42-
43-
componentDidUpdate(prevProps: CellProps<R>) {
44-
if (isFrozen(prevProps.column) && !isFrozen(this.props.column)) {
45-
this.removeScroll();
46-
}
47-
}
48-
4940
handleCellClick = () => {
5041
const { idx, rowIdx, cellMetaData } = this.props;
5142
cellMetaData.onCellClick({ idx, rowIdx });
@@ -88,10 +79,13 @@ export default class Cell<R> extends React.Component<CellProps<R>> implements Ce
8879
};
8980

9081
getStyle(): React.CSSProperties {
82+
const { column, height, scrollLeft } = this.props;
83+
9184
return {
92-
width: this.props.column.width,
93-
height: this.props.height,
94-
left: this.props.column.left
85+
height,
86+
width: column.width,
87+
left: column.left,
88+
transform: !isPositionStickySupported() && isFrozen(column) ? `translateX(${scrollLeft}px)` : 'none'
9589
};
9690
}
9791

packages/react-data-grid/src/Row.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import React from 'react';
22
import classNames from 'classnames';
3+
import shallowEqual from 'shallowequal';
34

4-
import rowComparer from './common/utils/RowComparer';
55
import Cell from './Cell';
66
import { isFrozen } from './ColumnUtils';
77
import * as rowUtils from './RowUtils';
8-
import { isPositionStickySupported } from './utils';
98
import { RowRenderer, RowRendererProps, CellRenderer, CellRendererProps, CalculatedColumn } from './common/types';
109

1110
export default class Row<R> extends React.Component<RowRendererProps<R>> implements RowRenderer<R> {
@@ -21,7 +20,10 @@ export default class Row<R> extends React.Component<RowRendererProps<R>> impleme
2120
private readonly cells = new Map<keyof R, CellRenderer>();
2221

2322
shouldComponentUpdate(nextProps: RowRendererProps<R>) {
24-
return rowComparer(nextProps, this.props);
23+
const { scrollLeft, ...rest } = this.props;
24+
const { scrollLeft: nextScrollLeft, ...nextRest } = nextProps;
25+
26+
return !shallowEqual(rest, nextRest);
2527
}
2628

2729
handleDragEnter = (e: React.DragEvent<HTMLDivElement>) => {
@@ -58,7 +60,7 @@ export default class Row<R> extends React.Component<RowRendererProps<R>> impleme
5860
rowData: row,
5961
expandableOptions: this.getExpandableOptions(key),
6062
isScrolling,
61-
scrollLeft: isFrozen(column) && !isPositionStickySupported() ? scrollLeft : undefined,
63+
scrollLeft,
6264
lastFrozenColumnIndex
6365
};
6466

packages/react-data-grid/src/RowGroup.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interface Props<R> {
1616
forceUpdate?: boolean;
1717
subRowDetails?: unknown;
1818
isRowHovered?: boolean;
19-
colVisibleStartIdx: number;
20-
colVisibleEndIdx: number;
2119
colOverscanStartIdx: number;
2220
colOverscanEndIdx: number;
2321
isScrolling?: boolean;

packages/react-data-grid/src/common/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export interface CellRendererProps<TRow, TValue = unknown> {
160160
rowData: TRow;
161161
cellMetaData: CellMetaData<TRow>;
162162
isScrolling?: boolean;
163-
scrollLeft?: number;
163+
scrollLeft: number;
164164
expandableOptions?: ExpandableOptions;
165165
lastFrozenColumnIndex: number;
166166
}

0 commit comments

Comments
 (0)