@difizen/libro-virtualized 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/es/auto-sizer/auto-sizer.d.ts +56 -0
- package/es/auto-sizer/auto-sizer.d.ts.map +1 -0
- package/es/auto-sizer/auto-sizer.js +157 -0
- package/es/auto-sizer/index.d.ts +3 -0
- package/es/auto-sizer/index.d.ts.map +1 -0
- package/es/auto-sizer/index.js +2 -0
- package/es/cell-measurer/cell-measurer-cache.d.ts +52 -0
- package/es/cell-measurer/cell-measurer-cache.d.ts.map +1 -0
- package/es/cell-measurer/cell-measurer-cache.js +176 -0
- package/es/cell-measurer/cell-measurer.d.ts +39 -0
- package/es/cell-measurer/cell-measurer.d.ts.map +1 -0
- package/es/cell-measurer/cell-measurer.js +154 -0
- package/es/cell-measurer/index.d.ts +5 -0
- package/es/cell-measurer/index.d.ts.map +1 -0
- package/es/cell-measurer/index.js +4 -0
- package/es/cell-measurer/types.d.ts +9 -0
- package/es/cell-measurer/types.d.ts.map +1 -0
- package/es/cell-measurer/types.js +0 -0
- package/es/grid/accessibility-overscanIndices-getter.d.ts +11 -0
- package/es/grid/accessibility-overscanIndices-getter.d.ts.map +1 -0
- package/es/grid/accessibility-overscanIndices-getter.js +33 -0
- package/es/grid/default-cell-range-renderer.d.ts +10 -0
- package/es/grid/default-cell-range-renderer.d.ts.map +1 -0
- package/es/grid/default-cell-range-renderer.js +135 -0
- package/es/grid/default-overscanIndices-getter.d.ts +11 -0
- package/es/grid/default-overscanIndices-getter.d.ts.map +1 -0
- package/es/grid/default-overscanIndices-getter.js +28 -0
- package/es/grid/grid.d.ts +359 -0
- package/es/grid/grid.d.ts.map +1 -0
- package/es/grid/grid.js +1287 -0
- package/es/grid/index.d.ts +6 -0
- package/es/grid/index.d.ts.map +1 -0
- package/es/grid/index.js +4 -0
- package/es/grid/types.d.ts +87 -0
- package/es/grid/types.d.ts.map +1 -0
- package/es/grid/types.js +1 -0
- package/es/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.d.ts +17 -0
- package/es/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.d.ts.map +1 -0
- package/es/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.js +25 -0
- package/es/grid/utils/cell-size-and-position-manager-row.d.ts +97 -0
- package/es/grid/utils/cell-size-and-position-manager-row.d.ts.map +1 -0
- package/es/grid/utils/cell-size-and-position-manager-row.js +297 -0
- package/es/grid/utils/cell-size-and-position-manager.d.ts +85 -0
- package/es/grid/utils/cell-size-and-position-manager.d.ts.map +1 -0
- package/es/grid/utils/cell-size-and-position-manager.js +268 -0
- package/es/grid/utils/max-element-size.d.ts +2 -0
- package/es/grid/utils/max-element-size.d.ts.map +1 -0
- package/es/grid/utils/max-element-size.js +17 -0
- package/es/grid/utils/scaling-cell-size-and-position-manager-row.d.ts +78 -0
- package/es/grid/utils/scaling-cell-size-and-position-manager-row.d.ts.map +1 -0
- package/es/grid/utils/scaling-cell-size-and-position-manager-row.js +187 -0
- package/es/grid/utils/scaling-cell-size-and-position-manager.d.ts +70 -0
- package/es/grid/utils/scaling-cell-size-and-position-manager.d.ts.map +1 -0
- package/es/grid/utils/scaling-cell-size-and-position-manager.js +187 -0
- package/es/grid/utils/update-scroll-index-helper.d.ts +24 -0
- package/es/grid/utils/update-scroll-index-helper.d.ts.map +1 -0
- package/es/grid/utils/update-scroll-index-helper.js +40 -0
- package/es/index.d.ts +5 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +4 -0
- package/es/list/index.d.ts +3 -0
- package/es/list/index.d.ts.map +1 -0
- package/es/list/index.js +1 -0
- package/es/list/list.d.ts +109 -0
- package/es/list/list.d.ts.map +1 -0
- package/es/list/list.js +261 -0
- package/es/list/types.d.ts +22 -0
- package/es/list/types.d.ts.map +1 -0
- package/es/list/types.js +1 -0
- package/es/utils/animation-frame.d.ts +7 -0
- package/es/utils/animation-frame.d.ts.map +1 -0
- package/es/utils/animation-frame.js +20 -0
- package/es/utils/create-callback-memoizer.d.ts +5 -0
- package/es/utils/create-callback-memoizer.d.ts.map +1 -0
- package/es/utils/create-callback-memoizer.js +25 -0
- package/es/utils/get-updated-offset-for-index.d.ts +14 -0
- package/es/utils/get-updated-offset-for-index.d.ts.map +1 -0
- package/es/utils/get-updated-offset-for-index.js +32 -0
- package/es/utils/init-cell-metadata.d.ts +13 -0
- package/es/utils/init-cell-metadata.d.ts.map +1 -0
- package/es/utils/init-cell-metadata.js +32 -0
- package/es/utils/request-animation-timeout.d.ts +12 -0
- package/es/utils/request-animation-timeout.d.ts.map +1 -0
- package/es/utils/request-animation-timeout.js +33 -0
- package/es/utils/test-helper.d.ts +5 -0
- package/es/utils/test-helper.d.ts.map +1 -0
- package/es/utils/test-helper.js +31 -0
- package/es/vendor/binary-search-bounds.d.ts +22 -0
- package/es/vendor/binary-search-bounds.d.ts.map +1 -0
- package/es/vendor/binary-search-bounds.js +198 -0
- package/es/vendor/detect-element-resize.d.ts +16 -0
- package/es/vendor/detect-element-resize.d.ts.map +1 -0
- package/es/vendor/detect-element-resize.js +184 -0
- package/es/vendor/interval-tree.d.ts +10 -0
- package/es/vendor/interval-tree.d.ts.map +1 -0
- package/es/vendor/interval-tree.js +359 -0
- package/package.json +59 -0
- package/src/auto-sizer/auto-sizer.tsx +187 -0
- package/src/auto-sizer/index.ts +4 -0
- package/src/cell-measurer/cell-measurer-cache.ts +220 -0
- package/src/cell-measurer/cell-measurer.ts +151 -0
- package/src/cell-measurer/index.ts +5 -0
- package/src/cell-measurer/types.ts +8 -0
- package/src/grid/accessibility-overscanIndices-getter.ts +38 -0
- package/src/grid/default-cell-range-renderer.ts +166 -0
- package/src/grid/default-overscanIndices-getter.ts +32 -0
- package/src/grid/grid.tsx +1672 -0
- package/src/grid/index.ts +14 -0
- package/src/grid/types.ts +112 -0
- package/src/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.ts +62 -0
- package/src/grid/utils/cell-size-and-position-manager-row.ts +365 -0
- package/src/grid/utils/cell-size-and-position-manager.ts +309 -0
- package/src/grid/utils/max-element-size.ts +18 -0
- package/src/grid/utils/scaling-cell-size-and-position-manager-row.ts +206 -0
- package/src/grid/utils/scaling-cell-size-and-position-manager.ts +198 -0
- package/src/grid/utils/update-scroll-index-helper.ts +96 -0
- package/src/index.spec.ts +10 -0
- package/src/index.ts +4 -0
- package/src/list/index.ts +2 -0
- package/src/list/list.tsx +292 -0
- package/src/list/types.ts +25 -0
- package/src/utils/animation-frame.ts +38 -0
- package/src/utils/create-callback-memoizer.ts +32 -0
- package/src/utils/get-updated-offset-for-index.ts +33 -0
- package/src/utils/init-cell-metadata.ts +32 -0
- package/src/utils/request-animation-timeout.ts +44 -0
- package/src/utils/test-helper.ts +20 -0
- package/src/vendor/binary-search-bounds.ts +203 -0
- package/src/vendor/detect-element-resize.ts +241 -0
- package/src/vendor/interval-tree.ts +406 -0
|
@@ -0,0 +1,1672 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-shadow */
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { scrollbarSize } from 'dom-helpers';
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
|
|
7
|
+
import createCallbackMemoizer from '../utils/create-callback-memoizer.js';
|
|
8
|
+
import type { AnimationTimeoutId } from '../utils/request-animation-timeout.js';
|
|
9
|
+
import {
|
|
10
|
+
cancelAnimationTimeout,
|
|
11
|
+
requestAnimationTimeout,
|
|
12
|
+
} from '../utils/request-animation-timeout.js';
|
|
13
|
+
|
|
14
|
+
import defaultCellRangeRenderer from './default-cell-range-renderer.js';
|
|
15
|
+
import defaultOverscanIndicesGetter, {
|
|
16
|
+
SCROLL_DIRECTION_BACKWARD,
|
|
17
|
+
SCROLL_DIRECTION_FORWARD,
|
|
18
|
+
} from './default-overscanIndices-getter.js';
|
|
19
|
+
import type {
|
|
20
|
+
Alignment,
|
|
21
|
+
CellCache,
|
|
22
|
+
CellPosition,
|
|
23
|
+
CellRangeRenderer,
|
|
24
|
+
CellRenderer,
|
|
25
|
+
CellSize,
|
|
26
|
+
CellSizeGetter,
|
|
27
|
+
NoContentRenderer,
|
|
28
|
+
OverscanIndicesGetter,
|
|
29
|
+
RenderedSection,
|
|
30
|
+
Scroll,
|
|
31
|
+
ScrollbarPresenceChange,
|
|
32
|
+
StyleCache,
|
|
33
|
+
} from './types.js';
|
|
34
|
+
import calculateSizeAndPositionDataAndUpdateScrollOffset from './utils/calculate-size-and-position-data-and-update-scroll-offset.js';
|
|
35
|
+
import RowScalingCellSizeAndPositionManager from './utils/scaling-cell-size-and-position-manager-row.js';
|
|
36
|
+
import ScalingCellSizeAndPositionManager from './utils/scaling-cell-size-and-position-manager.js';
|
|
37
|
+
import updateScrollIndexHelper from './utils/update-scroll-index-helper.js';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Specifies the number of milliseconds during which to disable pointer events while a scroll is in progress.
|
|
41
|
+
* This improves performance and makes scrolling smoother.
|
|
42
|
+
*/
|
|
43
|
+
export const DEFAULT_SCROLLING_RESET_TIME_INTERVAL = 150;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Controls whether the Grid updates the DOM element's scrollLeft/scrollTop based on the current state or just observes it.
|
|
47
|
+
* This prevents Grid from interrupting mouse-wheel animations (see issue #2).
|
|
48
|
+
*/
|
|
49
|
+
const SCROLL_POSITION_CHANGE_REASONS = {
|
|
50
|
+
OBSERVED: 'observed',
|
|
51
|
+
REQUESTED: 'requested',
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const renderNull: NoContentRenderer = () => null;
|
|
55
|
+
|
|
56
|
+
type ScrollPosition = {
|
|
57
|
+
scrollTop?: number;
|
|
58
|
+
scrollLeft?: number;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type Props = {
|
|
62
|
+
'aria-label': string;
|
|
63
|
+
'aria-readonly'?: boolean;
|
|
64
|
+
|
|
65
|
+
// 每个cell的高度
|
|
66
|
+
cellsHeight: number[];
|
|
67
|
+
|
|
68
|
+
noEditorArea: React.ReactElement;
|
|
69
|
+
|
|
70
|
+
editorAreaHeight: number[];
|
|
71
|
+
|
|
72
|
+
totalSize: number;
|
|
73
|
+
|
|
74
|
+
editorsOffset: number[];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Set the width of the inner scrollable container to 'auto'.
|
|
78
|
+
* This is useful for single-column Grids to ensure that the column doesn't extend below a vertical scrollbar.
|
|
79
|
+
*/
|
|
80
|
+
autoContainerWidth: boolean;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Removes fixed height from the scrollingContainer so that the total height of rows can stretch the window.
|
|
84
|
+
* Intended for use with WindowScroller
|
|
85
|
+
*/
|
|
86
|
+
autoHeight: boolean;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Removes fixed width from the scrollingContainer so that the total width of rows can stretch the window.
|
|
90
|
+
* Intended for use with WindowScroller
|
|
91
|
+
*/
|
|
92
|
+
autoWidth: boolean;
|
|
93
|
+
|
|
94
|
+
/** Responsible for rendering a cell given an row and column index. */
|
|
95
|
+
cellRenderer: CellRenderer;
|
|
96
|
+
|
|
97
|
+
/** Responsible for rendering a group of cells given their index ranges. */
|
|
98
|
+
cellRangeRenderer: CellRangeRenderer;
|
|
99
|
+
|
|
100
|
+
/** Optional custom CSS class name to attach to root Grid element. */
|
|
101
|
+
className?: string;
|
|
102
|
+
|
|
103
|
+
/** Number of columns in grid. */
|
|
104
|
+
columnCount: number;
|
|
105
|
+
|
|
106
|
+
/** Either a fixed column width (number) or a function that returns the width of a column given its index. */
|
|
107
|
+
columnWidth: CellSize;
|
|
108
|
+
|
|
109
|
+
/** Unfiltered props for the Grid container. */
|
|
110
|
+
containerProps?: object;
|
|
111
|
+
|
|
112
|
+
/** ARIA role for the cell-container. */
|
|
113
|
+
containerRole: string;
|
|
114
|
+
|
|
115
|
+
/** Optional inline style applied to inner cell-container */
|
|
116
|
+
containerStyle: object;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* If CellMeasurer is used to measure this Grid's children, this should be a pointer to its CellMeasurerCache.
|
|
120
|
+
* A shared CellMeasurerCache reference enables Grid and CellMeasurer to share measurement data.
|
|
121
|
+
*/
|
|
122
|
+
deferredMeasurementCache?: any;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Used to estimate the total width of a Grid before all of its columns have actually been measured.
|
|
126
|
+
* The estimated total width is adjusted as columns are rendered.
|
|
127
|
+
*/
|
|
128
|
+
estimatedColumnSize: number;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Used to estimate the total height of a Grid before all of its rows have actually been measured.
|
|
132
|
+
* The estimated total height is adjusted as rows are rendered.
|
|
133
|
+
*/
|
|
134
|
+
estimatedRowSize: number;
|
|
135
|
+
|
|
136
|
+
/** Exposed for testing purposes only. */
|
|
137
|
+
getScrollbarSize: () => number;
|
|
138
|
+
|
|
139
|
+
/** Height of Grid; this property determines the number of visible (vs virtualized) rows. */
|
|
140
|
+
height: number;
|
|
141
|
+
|
|
142
|
+
/** Optional custom id to attach to root Grid element. */
|
|
143
|
+
id?: string;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Override internal is-scrolling state tracking.
|
|
147
|
+
* This property is primarily intended for use with the WindowScroller component.
|
|
148
|
+
*/
|
|
149
|
+
isScrolling?: boolean;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Opt-out of isScrolling param passed to cellRangeRenderer.
|
|
153
|
+
* To avoid the extra render when scroll stops.
|
|
154
|
+
*/
|
|
155
|
+
isScrollingOptOut: boolean;
|
|
156
|
+
|
|
157
|
+
/** Optional renderer to be used in place of rows when either :rowCount or :columnCount is 0. */
|
|
158
|
+
noContentRenderer: NoContentRenderer;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Callback invoked whenever the scroll offset changes within the inner scrollable region.
|
|
162
|
+
* This callback can be used to sync scrolling between lists, tables, or grids.
|
|
163
|
+
*/
|
|
164
|
+
onScroll: (params: Scroll) => void;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Called whenever a horizontal or vertical scrollbar is added or removed.
|
|
168
|
+
* This prop is not intended for end-user use;
|
|
169
|
+
* It is used by MultiGrid to support fixed-row/fixed-column scroll syncing.
|
|
170
|
+
*/
|
|
171
|
+
onScrollbarPresenceChange: (params: ScrollbarPresenceChange) => void;
|
|
172
|
+
|
|
173
|
+
/** Callback invoked with information about the section of the Grid that was just rendered. */
|
|
174
|
+
onSectionRendered: (params: RenderedSection) => void;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Number of columns to render before/after the visible section of the grid.
|
|
178
|
+
* These columns can help for smoother scrolling on touch devices or browsers that send scroll events infrequently.
|
|
179
|
+
*/
|
|
180
|
+
overscanColumnCount: number;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Calculates the number of cells to overscan before and after a specified range.
|
|
184
|
+
* This function ensures that overscanning doesn't exceed the available cells.
|
|
185
|
+
*/
|
|
186
|
+
overscanIndicesGetter: OverscanIndicesGetter;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Number of rows to render above/below the visible section of the grid.
|
|
190
|
+
* These rows can help for smoother scrolling on touch devices or browsers that send scroll events infrequently.
|
|
191
|
+
*/
|
|
192
|
+
overscanRowCount: number;
|
|
193
|
+
|
|
194
|
+
/** ARIA role for the grid element. */
|
|
195
|
+
role: string;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Either a fixed row height (number) or a function that returns the height of a row given its index.
|
|
199
|
+
* Should implement the following interface: ({ index: number }): number
|
|
200
|
+
*/
|
|
201
|
+
rowHeight: CellSize;
|
|
202
|
+
|
|
203
|
+
/** Number of rows in grid. */
|
|
204
|
+
rowCount: number;
|
|
205
|
+
|
|
206
|
+
/** Wait this amount of time after the last scroll event before resetting Grid `pointer-events`. */
|
|
207
|
+
scrollingResetTimeInterval: number;
|
|
208
|
+
|
|
209
|
+
/** Horizontal offset. */
|
|
210
|
+
scrollLeft?: number;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Controls scroll-to-cell behavior of the Grid.
|
|
214
|
+
* The default ("auto") scrolls the least amount possible to ensure that the specified cell is fully visible.
|
|
215
|
+
* Use "start" to align cells to the top/left of the Grid and "end" to align bottom/right.
|
|
216
|
+
*/
|
|
217
|
+
scrollToAlignment: Alignment;
|
|
218
|
+
|
|
219
|
+
/** Column index to ensure visible (by forcefully scrolling if necessary) */
|
|
220
|
+
scrollToColumn: number;
|
|
221
|
+
|
|
222
|
+
/** Vertical offset. */
|
|
223
|
+
scrollTop?: number;
|
|
224
|
+
|
|
225
|
+
/** Row index to ensure visible (by forcefully scrolling if necessary) */
|
|
226
|
+
scrollToRow: number;
|
|
227
|
+
|
|
228
|
+
/** Optional inline style */
|
|
229
|
+
style: object;
|
|
230
|
+
|
|
231
|
+
/** Tab index for focus */
|
|
232
|
+
tabIndex?: number;
|
|
233
|
+
|
|
234
|
+
/** Width of Grid; this property determines the number of visible (vs virtualized) columns. */
|
|
235
|
+
width: number;
|
|
236
|
+
|
|
237
|
+
resizeObserver: ResizeObserver;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
type InstanceProps = {
|
|
241
|
+
prevColumnWidth: CellSize;
|
|
242
|
+
prevRowHeight: CellSize;
|
|
243
|
+
|
|
244
|
+
prevColumnCount: number;
|
|
245
|
+
prevRowCount: number;
|
|
246
|
+
prevIsScrolling: boolean;
|
|
247
|
+
prevScrollToColumn: number;
|
|
248
|
+
prevScrollToRow: number;
|
|
249
|
+
prevScrollLeft?: number;
|
|
250
|
+
prevScrollTop?: number;
|
|
251
|
+
|
|
252
|
+
columnSizeAndPositionManager: ScalingCellSizeAndPositionManager;
|
|
253
|
+
rowSizeAndPositionManager: RowScalingCellSizeAndPositionManager;
|
|
254
|
+
|
|
255
|
+
scrollbarSize: number;
|
|
256
|
+
scrollbarSizeMeasured: boolean;
|
|
257
|
+
cellsHeight: number[];
|
|
258
|
+
noEditorArea: React.ReactElement;
|
|
259
|
+
editorAreaHeight: number[];
|
|
260
|
+
totalSize: number;
|
|
261
|
+
editorsOffset: number[];
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
type State = {
|
|
265
|
+
instanceProps: InstanceProps;
|
|
266
|
+
isScrolling: boolean;
|
|
267
|
+
scrollDirectionHorizontal: -1 | 1;
|
|
268
|
+
scrollDirectionVertical: -1 | 1;
|
|
269
|
+
scrollLeft: number;
|
|
270
|
+
scrollTop: number;
|
|
271
|
+
scrollPositionChangeReason: 'observed' | 'requested' | null;
|
|
272
|
+
needToResetStyleCache: boolean;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// let scrollCache: { direction: number; timestamp: number } | null = null;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Renders tabular data with virtualization along the vertical and horizontal axes.
|
|
279
|
+
* Row heights and column widths must be known ahead of time and specified as properties.
|
|
280
|
+
*/
|
|
281
|
+
class Grid extends React.PureComponent<Props, State> {
|
|
282
|
+
static defaultProps = {
|
|
283
|
+
'aria-label': 'grid',
|
|
284
|
+
'aria-readonly': true,
|
|
285
|
+
autoContainerWidth: false,
|
|
286
|
+
autoHeight: false,
|
|
287
|
+
autoWidth: false,
|
|
288
|
+
cellRangeRenderer: defaultCellRangeRenderer,
|
|
289
|
+
containerRole: 'rowgroup',
|
|
290
|
+
containerStyle: {},
|
|
291
|
+
estimatedColumnSize: 100,
|
|
292
|
+
estimatedRowSize: 30,
|
|
293
|
+
getScrollbarSize: scrollbarSize,
|
|
294
|
+
noContentRenderer: renderNull,
|
|
295
|
+
onScroll: () => {
|
|
296
|
+
//
|
|
297
|
+
},
|
|
298
|
+
onScrollbarPresenceChange: () => {
|
|
299
|
+
//
|
|
300
|
+
},
|
|
301
|
+
onSectionRendered: () => {
|
|
302
|
+
//
|
|
303
|
+
},
|
|
304
|
+
overscanColumnCount: 0,
|
|
305
|
+
overscanIndicesGetter: defaultOverscanIndicesGetter,
|
|
306
|
+
overscanRowCount: 10,
|
|
307
|
+
role: 'grid',
|
|
308
|
+
scrollingResetTimeInterval: DEFAULT_SCROLLING_RESET_TIME_INTERVAL,
|
|
309
|
+
scrollToAlignment: 'auto',
|
|
310
|
+
scrollToColumn: -1,
|
|
311
|
+
scrollToRow: -1,
|
|
312
|
+
style: {},
|
|
313
|
+
tabIndex: 0,
|
|
314
|
+
isScrollingOptOut: false,
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// Invokes onSectionRendered callback only when start/stop row or column indices change
|
|
318
|
+
_onGridRenderedMemoizer = createCallbackMemoizer();
|
|
319
|
+
_onScrollMemoizer = createCallbackMemoizer(false);
|
|
320
|
+
|
|
321
|
+
_deferredInvalidateColumnIndex: number | null = null;
|
|
322
|
+
_deferredInvalidateRowIndex: number | null = null;
|
|
323
|
+
_recomputeScrollLeftFlag = false;
|
|
324
|
+
_recomputeScrollTopFlag = false;
|
|
325
|
+
|
|
326
|
+
_horizontalScrollBarSize = 0;
|
|
327
|
+
_verticalScrollBarSize = 0;
|
|
328
|
+
_scrollbarPresenceChanged = false;
|
|
329
|
+
_scrollingContainer: Element;
|
|
330
|
+
|
|
331
|
+
_childrenToDisplay: React.ReactNode[] = [];
|
|
332
|
+
|
|
333
|
+
_columnStartIndex: number;
|
|
334
|
+
_columnStopIndex: number;
|
|
335
|
+
_rowStartIndex: number;
|
|
336
|
+
_rowStopIndex: number;
|
|
337
|
+
_renderedColumnStartIndex?: number = 0;
|
|
338
|
+
_renderedColumnStopIndex?: number = 0;
|
|
339
|
+
_renderedRowStartIndex?: number = 0;
|
|
340
|
+
_renderedRowStopIndex?: number = 0;
|
|
341
|
+
|
|
342
|
+
_initialScrollTop: number;
|
|
343
|
+
_initialScrollLeft: number;
|
|
344
|
+
|
|
345
|
+
resizeObserver: ResizeObserver;
|
|
346
|
+
|
|
347
|
+
_disablePointerEventsTimeoutId?: AnimationTimeoutId | null;
|
|
348
|
+
|
|
349
|
+
_styleCache: StyleCache = {};
|
|
350
|
+
_cellCache: CellCache = {};
|
|
351
|
+
|
|
352
|
+
constructor(props: Props) {
|
|
353
|
+
super(props);
|
|
354
|
+
|
|
355
|
+
const columnSizeAndPositionManager = new ScalingCellSizeAndPositionManager({
|
|
356
|
+
cellCount: props.columnCount,
|
|
357
|
+
cellSizeGetter: (params) => Grid._wrapSizeGetter(props.columnWidth)(params),
|
|
358
|
+
estimatedCellSize: Grid._getEstimatedColumnSize(props),
|
|
359
|
+
});
|
|
360
|
+
const rowSizeAndPositionManager = new RowScalingCellSizeAndPositionManager({
|
|
361
|
+
cellCount: props.rowCount,
|
|
362
|
+
cellSizeGetter: (params) => Grid._wrapSizeGetter(props.rowHeight)(params),
|
|
363
|
+
estimatedCellSize: Grid._getEstimatedRowSize(props),
|
|
364
|
+
cellsHeight: props.cellsHeight,
|
|
365
|
+
editorAreaHeight: props.editorAreaHeight,
|
|
366
|
+
totalSize: props.totalSize,
|
|
367
|
+
editorsOffset: props.editorsOffset,
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
this.state = {
|
|
371
|
+
instanceProps: {
|
|
372
|
+
columnSizeAndPositionManager,
|
|
373
|
+
rowSizeAndPositionManager,
|
|
374
|
+
|
|
375
|
+
prevColumnWidth: props.columnWidth,
|
|
376
|
+
prevRowHeight: props.rowHeight,
|
|
377
|
+
prevColumnCount: props.columnCount,
|
|
378
|
+
prevRowCount: props.rowCount,
|
|
379
|
+
prevIsScrolling: props.isScrolling === true,
|
|
380
|
+
prevScrollToColumn: props.scrollToColumn,
|
|
381
|
+
prevScrollToRow: props.scrollToRow,
|
|
382
|
+
prevScrollLeft: props.scrollLeft,
|
|
383
|
+
prevScrollTop: props.scrollTop,
|
|
384
|
+
|
|
385
|
+
scrollbarSize: 0,
|
|
386
|
+
scrollbarSizeMeasured: false,
|
|
387
|
+
cellsHeight: props.cellsHeight,
|
|
388
|
+
noEditorArea: props.noEditorArea,
|
|
389
|
+
editorAreaHeight: props.editorAreaHeight,
|
|
390
|
+
totalSize: props.totalSize,
|
|
391
|
+
editorsOffset: props.editorsOffset,
|
|
392
|
+
},
|
|
393
|
+
isScrolling: false,
|
|
394
|
+
scrollDirectionHorizontal: SCROLL_DIRECTION_FORWARD,
|
|
395
|
+
scrollDirectionVertical: SCROLL_DIRECTION_FORWARD,
|
|
396
|
+
scrollLeft: props.scrollLeft || 0,
|
|
397
|
+
scrollTop: props.scrollTop || 0,
|
|
398
|
+
scrollPositionChangeReason: null,
|
|
399
|
+
|
|
400
|
+
needToResetStyleCache: false,
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
if (props.scrollToRow > 0) {
|
|
404
|
+
this._initialScrollTop = this._getCalculatedScrollTop(props, this.state);
|
|
405
|
+
}
|
|
406
|
+
if (props.scrollToColumn > 0) {
|
|
407
|
+
this._initialScrollLeft = this._getCalculatedScrollLeft(props, this.state);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Gets offsets for a given cell and alignment.
|
|
413
|
+
*/
|
|
414
|
+
getOffsetForCell({
|
|
415
|
+
alignment = this.props.scrollToAlignment,
|
|
416
|
+
columnIndex = this.props.scrollToColumn,
|
|
417
|
+
rowIndex = this.props.scrollToRow,
|
|
418
|
+
}: {
|
|
419
|
+
alignment?: Alignment;
|
|
420
|
+
columnIndex?: number;
|
|
421
|
+
rowIndex?: number;
|
|
422
|
+
} = {}) {
|
|
423
|
+
const offsetProps = {
|
|
424
|
+
...this.props,
|
|
425
|
+
scrollToAlignment: alignment,
|
|
426
|
+
scrollToColumn: columnIndex,
|
|
427
|
+
scrollToRow: rowIndex,
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
return {
|
|
431
|
+
scrollLeft: this._getCalculatedScrollLeft(offsetProps),
|
|
432
|
+
scrollTop: this._getCalculatedScrollTop(offsetProps),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Gets estimated total rows' height.
|
|
438
|
+
*/
|
|
439
|
+
getTotalRowsHeight() {
|
|
440
|
+
return this.state.instanceProps.rowSizeAndPositionManager.getTotalSize();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Gets estimated total columns' width.
|
|
445
|
+
*/
|
|
446
|
+
getTotalColumnsWidth() {
|
|
447
|
+
return this.state.instanceProps.columnSizeAndPositionManager.getTotalSize();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* This method handles a scroll event originating from an external scroll control.
|
|
452
|
+
* It's an advanced method and should probably not be used unless you're implementing a custom scroll-bar solution.
|
|
453
|
+
*/
|
|
454
|
+
handleScrollEvent({
|
|
455
|
+
scrollLeft: scrollLeftParam = 0,
|
|
456
|
+
scrollTop: scrollTopParam = 0,
|
|
457
|
+
}: ScrollPosition) {
|
|
458
|
+
// On iOS, we can arrive at negative offsets by swiping past the start.
|
|
459
|
+
// To prevent flicker here, we make playing in the negative offset zone cause nothing to happen.
|
|
460
|
+
if (scrollTopParam < 0) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// Prevent pointer events from interrupting a smooth scroll
|
|
465
|
+
this._debounceScrollEnded();
|
|
466
|
+
|
|
467
|
+
const { autoHeight, autoWidth, height, width } = this.props;
|
|
468
|
+
const { instanceProps } = this.state;
|
|
469
|
+
|
|
470
|
+
// When this component is shrunk drastically, React dispatches a series of back-to-back scroll events,
|
|
471
|
+
// Gradually converging on a scrollTop that is within the bounds of the new, smaller height.
|
|
472
|
+
// This causes a series of rapid renders that is slow for long lists.
|
|
473
|
+
// We can avoid that by doing some simple bounds checking to ensure that scroll offsets never exceed their bounds.
|
|
474
|
+
const scrollbarSize = instanceProps.scrollbarSize;
|
|
475
|
+
const totalRowsHeight = instanceProps.rowSizeAndPositionManager.getTotalSize();
|
|
476
|
+
const totalColumnsWidth = instanceProps.columnSizeAndPositionManager.getTotalSize();
|
|
477
|
+
const scrollLeft = Math.min(
|
|
478
|
+
Math.max(0, totalColumnsWidth - width + scrollbarSize),
|
|
479
|
+
scrollLeftParam,
|
|
480
|
+
);
|
|
481
|
+
const scrollTop = Math.min(
|
|
482
|
+
Math.max(0, totalRowsHeight - height + scrollbarSize),
|
|
483
|
+
scrollTopParam,
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
// Certain devices (like Apple touchpad) rapid-fire duplicate events.
|
|
487
|
+
// Don't force a re-render if this is the case.
|
|
488
|
+
// The mouse may move faster then the animation frame does.
|
|
489
|
+
// Use requestAnimationFrame to avoid over-updating.
|
|
490
|
+
if (this.state.scrollLeft !== scrollLeft || this.state.scrollTop !== scrollTop) {
|
|
491
|
+
// Track scrolling direction so we can more efficiently overscan rows to reduce empty space around the edges while scrolling.
|
|
492
|
+
// Don't change direction for an axis unless scroll offset has changed.
|
|
493
|
+
const scrollDirectionHorizontal =
|
|
494
|
+
scrollLeft !== this.state.scrollLeft
|
|
495
|
+
? scrollLeft > this.state.scrollLeft
|
|
496
|
+
? SCROLL_DIRECTION_FORWARD
|
|
497
|
+
: SCROLL_DIRECTION_BACKWARD
|
|
498
|
+
: this.state.scrollDirectionHorizontal;
|
|
499
|
+
const scrollDirectionVertical =
|
|
500
|
+
scrollTop !== this.state.scrollTop
|
|
501
|
+
? scrollTop > this.state.scrollTop
|
|
502
|
+
? SCROLL_DIRECTION_FORWARD
|
|
503
|
+
: SCROLL_DIRECTION_BACKWARD
|
|
504
|
+
: this.state.scrollDirectionVertical;
|
|
505
|
+
|
|
506
|
+
// if (
|
|
507
|
+
// scrollCache &&
|
|
508
|
+
// // scrollCache.direction !== scrollDirectionVertical &&
|
|
509
|
+
// Date.now() - scrollCache.timestamp < 150
|
|
510
|
+
// ) {
|
|
511
|
+
// return;
|
|
512
|
+
// }
|
|
513
|
+
|
|
514
|
+
// // 更新缓存
|
|
515
|
+
// scrollCache = {
|
|
516
|
+
// timestamp: Date.now(),
|
|
517
|
+
// direction: scrollDirectionVertical,
|
|
518
|
+
// };
|
|
519
|
+
|
|
520
|
+
const newState: any = {
|
|
521
|
+
isScrolling: true,
|
|
522
|
+
scrollDirectionHorizontal,
|
|
523
|
+
scrollDirectionVertical,
|
|
524
|
+
scrollPositionChangeReason: SCROLL_POSITION_CHANGE_REASONS.OBSERVED,
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
if (!autoHeight) {
|
|
528
|
+
newState.scrollTop = scrollTop;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (!autoWidth) {
|
|
532
|
+
newState.scrollLeft = scrollLeft;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
newState.needToResetStyleCache = false;
|
|
536
|
+
this.setState(newState);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
this._invokeOnScrollMemoizer({
|
|
540
|
+
scrollLeft,
|
|
541
|
+
scrollTop,
|
|
542
|
+
totalColumnsWidth,
|
|
543
|
+
totalRowsHeight,
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Invalidate Grid size and recompute visible cells.
|
|
549
|
+
* This is a deferred wrapper for recomputeGridSize().
|
|
550
|
+
* It sets a flag to be evaluated on cDM/cDU to avoid unnecessary renders.
|
|
551
|
+
* This method is intended for advanced use-cases like CellMeasurer.
|
|
552
|
+
*/
|
|
553
|
+
// @TODO (bvaughn) Add automated test coverage for this.
|
|
554
|
+
invalidateCellSizeAfterRender({ columnIndex, rowIndex }: CellPosition) {
|
|
555
|
+
this._deferredInvalidateColumnIndex =
|
|
556
|
+
typeof this._deferredInvalidateColumnIndex === 'number'
|
|
557
|
+
? Math.min(this._deferredInvalidateColumnIndex, columnIndex)
|
|
558
|
+
: columnIndex;
|
|
559
|
+
this._deferredInvalidateRowIndex =
|
|
560
|
+
typeof this._deferredInvalidateRowIndex === 'number'
|
|
561
|
+
? Math.min(this._deferredInvalidateRowIndex, rowIndex)
|
|
562
|
+
: rowIndex;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Pre-measure all columns and rows in a Grid.
|
|
567
|
+
* Typically cells are only measured as needed and estimated sizes are used for cells that have not yet been measured.
|
|
568
|
+
* This method ensures that the next call to getTotalSize() returns an exact size (as opposed to just an estimated one).
|
|
569
|
+
*/
|
|
570
|
+
measureAllCells() {
|
|
571
|
+
const { columnCount, rowCount } = this.props;
|
|
572
|
+
const { instanceProps } = this.state;
|
|
573
|
+
instanceProps.columnSizeAndPositionManager.getSizeAndPositionOfCell(
|
|
574
|
+
columnCount - 1,
|
|
575
|
+
);
|
|
576
|
+
instanceProps.rowSizeAndPositionManager.getSizeAndPositionOfCell(rowCount - 1);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Forced recompute of row heights and column widths.
|
|
581
|
+
* This function should be called if dynamic column or row sizes have changed but nothing else has.
|
|
582
|
+
* Since Grid only receives :columnCount and :rowCount it has no way of detecting when the underlying data changes.
|
|
583
|
+
*/
|
|
584
|
+
recomputeGridSize(
|
|
585
|
+
{ columnIndex = 0, rowIndex = 0 }: CellPosition = { columnIndex: 0, rowIndex: 0 },
|
|
586
|
+
) {
|
|
587
|
+
const { scrollToColumn, scrollToRow } = this.props;
|
|
588
|
+
const { instanceProps } = this.state;
|
|
589
|
+
|
|
590
|
+
instanceProps.columnSizeAndPositionManager.resetCell(columnIndex);
|
|
591
|
+
instanceProps.rowSizeAndPositionManager.resetCell(rowIndex);
|
|
592
|
+
|
|
593
|
+
// Cell sizes may be determined by a function property.
|
|
594
|
+
// In this case the cDU handler can't know if they changed.
|
|
595
|
+
// Store this flag to let the next cDU pass know it needs to recompute the scroll offset.
|
|
596
|
+
this._recomputeScrollLeftFlag =
|
|
597
|
+
scrollToColumn >= 0 &&
|
|
598
|
+
(this.state.scrollDirectionHorizontal === SCROLL_DIRECTION_FORWARD
|
|
599
|
+
? columnIndex <= scrollToColumn
|
|
600
|
+
: columnIndex >= scrollToColumn);
|
|
601
|
+
this._recomputeScrollTopFlag =
|
|
602
|
+
scrollToRow >= 0 &&
|
|
603
|
+
(this.state.scrollDirectionVertical === SCROLL_DIRECTION_FORWARD
|
|
604
|
+
? rowIndex <= scrollToRow
|
|
605
|
+
: rowIndex >= scrollToRow);
|
|
606
|
+
|
|
607
|
+
// Clear cell cache in case we are scrolling;
|
|
608
|
+
// Invalid row heights likely mean invalid cached content as well.
|
|
609
|
+
this._styleCache = {};
|
|
610
|
+
this._cellCache = {};
|
|
611
|
+
|
|
612
|
+
this.forceUpdate();
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
scrollToLine({ cellIndex, lineIndex }: { cellIndex: number; lineIndex: number }) {
|
|
616
|
+
const { offset } =
|
|
617
|
+
this.state.instanceProps.rowSizeAndPositionManager.getSizeAndPositionOfCell(
|
|
618
|
+
cellIndex,
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
const scrollTop = offset + (lineIndex - 1) * 20 + 12; // 上padding 为12
|
|
622
|
+
|
|
623
|
+
this.scrollToPosition({ scrollTop });
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Ensure column and row are visible.
|
|
628
|
+
*/
|
|
629
|
+
scrollToCell({ columnIndex, rowIndex }: CellPosition) {
|
|
630
|
+
const { columnCount } = this.props;
|
|
631
|
+
|
|
632
|
+
const props = this.props;
|
|
633
|
+
|
|
634
|
+
// Don't adjust scroll offset for single-column grids (eg List, Table).
|
|
635
|
+
// This can cause a funky scroll offset because of the vertical scrollbar width.
|
|
636
|
+
if (columnCount > 1 && columnIndex !== undefined) {
|
|
637
|
+
this._updateScrollLeftForScrollToColumn({
|
|
638
|
+
...props,
|
|
639
|
+
scrollToColumn: columnIndex,
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (rowIndex !== undefined) {
|
|
644
|
+
this._updateScrollTopForScrollToRow({
|
|
645
|
+
...props,
|
|
646
|
+
scrollToRow: rowIndex,
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
override componentDidMount() {
|
|
652
|
+
const {
|
|
653
|
+
getScrollbarSize,
|
|
654
|
+
height,
|
|
655
|
+
scrollLeft,
|
|
656
|
+
scrollToColumn,
|
|
657
|
+
scrollTop,
|
|
658
|
+
scrollToRow,
|
|
659
|
+
width,
|
|
660
|
+
} = this.props;
|
|
661
|
+
|
|
662
|
+
// // 创建ResizeObserver实例,当观察的元素尺寸变化时调用回调函数
|
|
663
|
+
// this.resizeObserver = new ResizeObserver(entries => {
|
|
664
|
+
// entries.forEach(entry => {
|
|
665
|
+
// const isVisible = entry.contentRect.width !== 0 && entry.contentRect.height !== 0;
|
|
666
|
+
|
|
667
|
+
// if (isVisible) {
|
|
668
|
+
// console.log(
|
|
669
|
+
// 'output Container width, height',
|
|
670
|
+
// entry.target.clientWidth,
|
|
671
|
+
// entry.target.clientHeight,
|
|
672
|
+
// );
|
|
673
|
+
// }
|
|
674
|
+
// });
|
|
675
|
+
// });
|
|
676
|
+
|
|
677
|
+
const { instanceProps } = this.state;
|
|
678
|
+
|
|
679
|
+
// Reset initial offsets to be ignored in browser
|
|
680
|
+
this._initialScrollTop = 0;
|
|
681
|
+
this._initialScrollLeft = 0;
|
|
682
|
+
|
|
683
|
+
// If cell sizes have been invalidated (eg we are using CellMeasurer) then reset cached positions.
|
|
684
|
+
// We must do this at the start of the method as we may calculate and update scroll position below.
|
|
685
|
+
this._handleInvalidatedGridSize();
|
|
686
|
+
|
|
687
|
+
// If this component was first rendered server-side, scrollbar size will be undefined.
|
|
688
|
+
// In that event we need to remeasure.
|
|
689
|
+
if (!instanceProps.scrollbarSizeMeasured) {
|
|
690
|
+
this.setState((prevState) => {
|
|
691
|
+
const stateUpdate = { ...prevState, needToResetStyleCache: false };
|
|
692
|
+
stateUpdate.instanceProps.scrollbarSize = getScrollbarSize();
|
|
693
|
+
stateUpdate.instanceProps.scrollbarSizeMeasured = true;
|
|
694
|
+
return stateUpdate;
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (
|
|
699
|
+
(typeof scrollLeft === 'number' && scrollLeft >= 0) ||
|
|
700
|
+
(typeof scrollTop === 'number' && scrollTop >= 0)
|
|
701
|
+
) {
|
|
702
|
+
const stateUpdate = Grid._getScrollToPositionStateUpdate({
|
|
703
|
+
prevState: this.state,
|
|
704
|
+
scrollLeft,
|
|
705
|
+
scrollTop,
|
|
706
|
+
});
|
|
707
|
+
if (stateUpdate) {
|
|
708
|
+
stateUpdate.needToResetStyleCache = false;
|
|
709
|
+
this.setState(stateUpdate);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// refs don't work in `react-test-renderer`
|
|
714
|
+
if (this._scrollingContainer) {
|
|
715
|
+
// setting the ref's scrollLeft and scrollTop.
|
|
716
|
+
// Somehow in MultiGrid the main grid doesn't trigger a update on mount.
|
|
717
|
+
if (this._scrollingContainer.scrollLeft !== this.state.scrollLeft) {
|
|
718
|
+
this._scrollingContainer.scrollLeft = this.state.scrollLeft;
|
|
719
|
+
}
|
|
720
|
+
if (this._scrollingContainer.scrollTop !== this.state.scrollTop) {
|
|
721
|
+
this._scrollingContainer.scrollTop = this.state.scrollTop;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// Don't update scroll offset if the size is 0; we don't render any cells in this case.
|
|
726
|
+
// Setting a state may cause us to later thing we've updated the offce when we haven't.
|
|
727
|
+
const sizeIsBiggerThanZero = height > 0 && width > 0;
|
|
728
|
+
if (scrollToColumn >= 0 && sizeIsBiggerThanZero) {
|
|
729
|
+
this._updateScrollLeftForScrollToColumn();
|
|
730
|
+
}
|
|
731
|
+
if (scrollToRow >= 0 && sizeIsBiggerThanZero) {
|
|
732
|
+
this._updateScrollTopForScrollToRow();
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Update onRowsRendered callback
|
|
736
|
+
this._invokeOnGridRenderedHelper();
|
|
737
|
+
|
|
738
|
+
// Initialize onScroll callback
|
|
739
|
+
this._invokeOnScrollMemoizer({
|
|
740
|
+
scrollLeft: scrollLeft || 0,
|
|
741
|
+
scrollTop: scrollTop || 0,
|
|
742
|
+
totalColumnsWidth: instanceProps.columnSizeAndPositionManager.getTotalSize(),
|
|
743
|
+
totalRowsHeight: instanceProps.rowSizeAndPositionManager.getTotalSize(),
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
this._maybeCallOnScrollbarPresenceChange();
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* @private
|
|
751
|
+
* This method updates scrollLeft/scrollTop in state for the following conditions:
|
|
752
|
+
* 1) New scroll-to-cell props have been set
|
|
753
|
+
*/
|
|
754
|
+
override componentDidUpdate(prevProps: Props, prevState: State) {
|
|
755
|
+
const {
|
|
756
|
+
autoHeight,
|
|
757
|
+
autoWidth,
|
|
758
|
+
columnCount,
|
|
759
|
+
height,
|
|
760
|
+
rowCount,
|
|
761
|
+
scrollToAlignment,
|
|
762
|
+
scrollToColumn,
|
|
763
|
+
scrollToRow,
|
|
764
|
+
width,
|
|
765
|
+
} = this.props;
|
|
766
|
+
const { scrollLeft, scrollPositionChangeReason, scrollTop, instanceProps } =
|
|
767
|
+
this.state;
|
|
768
|
+
|
|
769
|
+
// If cell sizes have been invalidated (eg we are using CellMeasurer) then reset cached positions.
|
|
770
|
+
// We must do this at the start of the method as we may calculate and update scroll position below.
|
|
771
|
+
this._handleInvalidatedGridSize();
|
|
772
|
+
|
|
773
|
+
// Handle edge case where column or row count has only just increased over 0.
|
|
774
|
+
// In this case we may have to restore a previously-specified scroll offset.
|
|
775
|
+
// For more info see bvaughn/react-virtualized/issues/218
|
|
776
|
+
const columnOrRowCountJustIncreasedFromZero =
|
|
777
|
+
(columnCount > 0 && prevProps.columnCount === 0) ||
|
|
778
|
+
(rowCount > 0 && prevProps.rowCount === 0);
|
|
779
|
+
|
|
780
|
+
// Make sure requested changes to :scrollLeft or :scrollTop get applied.
|
|
781
|
+
// Assigning to scrollLeft/scrollTop tells the browser to interrupt any running scroll animations,
|
|
782
|
+
// And to discard any pending async changes to the scroll position that may have happened in the meantime (e.g. on a separate scrolling thread).
|
|
783
|
+
// So we only set these when we require an adjustment of the scroll position.
|
|
784
|
+
// See issue #2 for more information.
|
|
785
|
+
if (scrollPositionChangeReason === SCROLL_POSITION_CHANGE_REASONS.REQUESTED) {
|
|
786
|
+
// @TRICKY :autoHeight and :autoWidth properties instructs Grid to leave :scrollTop and :scrollLeft management to an external HOC (eg WindowScroller).
|
|
787
|
+
// In this case we should avoid checking scrollingContainer.scrollTop and scrollingContainer.scrollLeft since it forces layout/flow.
|
|
788
|
+
if (
|
|
789
|
+
!autoWidth &&
|
|
790
|
+
scrollLeft >= 0 &&
|
|
791
|
+
(scrollLeft !== this._scrollingContainer.scrollLeft ||
|
|
792
|
+
columnOrRowCountJustIncreasedFromZero)
|
|
793
|
+
) {
|
|
794
|
+
this._scrollingContainer.scrollLeft = scrollLeft;
|
|
795
|
+
}
|
|
796
|
+
if (
|
|
797
|
+
!autoHeight &&
|
|
798
|
+
scrollTop >= 0 &&
|
|
799
|
+
(scrollTop !== this._scrollingContainer.scrollTop ||
|
|
800
|
+
columnOrRowCountJustIncreasedFromZero)
|
|
801
|
+
) {
|
|
802
|
+
this._scrollingContainer.scrollTop = scrollTop;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// Special case where the previous size was 0:
|
|
807
|
+
// In this case we don't show any windowed cells at all.
|
|
808
|
+
// So we should always recalculate offset afterwards.
|
|
809
|
+
const sizeJustIncreasedFromZero =
|
|
810
|
+
(prevProps.width === 0 || prevProps.height === 0) && height > 0 && width > 0;
|
|
811
|
+
|
|
812
|
+
// Update scroll offsets if the current :scrollToColumn or :scrollToRow values requires it
|
|
813
|
+
// @TODO Do we also need this check or can the one in componentWillUpdate() suffice?
|
|
814
|
+
if (this._recomputeScrollLeftFlag) {
|
|
815
|
+
this._recomputeScrollLeftFlag = false;
|
|
816
|
+
this._updateScrollLeftForScrollToColumn(this.props);
|
|
817
|
+
} else {
|
|
818
|
+
updateScrollIndexHelper({
|
|
819
|
+
cellSizeAndPositionManager: instanceProps.columnSizeAndPositionManager,
|
|
820
|
+
previousCellsCount: prevProps.columnCount,
|
|
821
|
+
previousCellSize: prevProps.columnWidth,
|
|
822
|
+
previousScrollToAlignment: prevProps.scrollToAlignment,
|
|
823
|
+
previousScrollToIndex: prevProps.scrollToColumn,
|
|
824
|
+
previousSize: prevProps.width,
|
|
825
|
+
scrollOffset: scrollLeft,
|
|
826
|
+
scrollToAlignment,
|
|
827
|
+
scrollToIndex: scrollToColumn,
|
|
828
|
+
size: width,
|
|
829
|
+
sizeJustIncreasedFromZero,
|
|
830
|
+
updateScrollIndexCallback: () =>
|
|
831
|
+
this._updateScrollLeftForScrollToColumn(this.props),
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
if (this._recomputeScrollTopFlag) {
|
|
836
|
+
this._recomputeScrollTopFlag = false;
|
|
837
|
+
this._updateScrollTopForScrollToRow(this.props);
|
|
838
|
+
} else {
|
|
839
|
+
updateScrollIndexHelper({
|
|
840
|
+
cellSizeAndPositionManager: instanceProps.rowSizeAndPositionManager,
|
|
841
|
+
previousCellsCount: prevProps.rowCount,
|
|
842
|
+
previousCellSize: prevProps.rowHeight,
|
|
843
|
+
previousScrollToAlignment: prevProps.scrollToAlignment,
|
|
844
|
+
previousScrollToIndex: prevProps.scrollToRow,
|
|
845
|
+
previousSize: prevProps.height,
|
|
846
|
+
scrollOffset: scrollTop,
|
|
847
|
+
scrollToAlignment,
|
|
848
|
+
scrollToIndex: scrollToRow,
|
|
849
|
+
size: height,
|
|
850
|
+
sizeJustIncreasedFromZero,
|
|
851
|
+
updateScrollIndexCallback: () =>
|
|
852
|
+
this._updateScrollTopForScrollToRow(this.props),
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// Update onRowsRendered callback if start/stop indices have changed
|
|
857
|
+
this._invokeOnGridRenderedHelper();
|
|
858
|
+
|
|
859
|
+
// Changes to :scrollLeft or :scrollTop should also notify :onScroll listeners
|
|
860
|
+
if (scrollLeft !== prevState.scrollLeft || scrollTop !== prevState.scrollTop) {
|
|
861
|
+
const totalRowsHeight = instanceProps.rowSizeAndPositionManager.getTotalSize();
|
|
862
|
+
const totalColumnsWidth =
|
|
863
|
+
instanceProps.columnSizeAndPositionManager.getTotalSize();
|
|
864
|
+
|
|
865
|
+
this._invokeOnScrollMemoizer({
|
|
866
|
+
scrollLeft,
|
|
867
|
+
scrollTop,
|
|
868
|
+
totalColumnsWidth,
|
|
869
|
+
totalRowsHeight,
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
this._maybeCallOnScrollbarPresenceChange();
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
override componentWillUnmount() {
|
|
877
|
+
if (this._disablePointerEventsTimeoutId) {
|
|
878
|
+
cancelAnimationTimeout(this._disablePointerEventsTimeoutId);
|
|
879
|
+
}
|
|
880
|
+
// 组件卸载时,停止监听
|
|
881
|
+
if (this.resizeObserver) {
|
|
882
|
+
this.resizeObserver.disconnect();
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* This method updates scrollLeft/scrollTop in state for the following conditions:
|
|
888
|
+
* 1) Empty content (0 rows or columns)
|
|
889
|
+
* 2) New scroll props overriding the current state
|
|
890
|
+
* 3) Cells-count or cells-size has changed, making previous scroll offsets invalid
|
|
891
|
+
*/
|
|
892
|
+
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
|
|
893
|
+
const newState: any = {};
|
|
894
|
+
const { instanceProps } = prevState;
|
|
895
|
+
|
|
896
|
+
if (
|
|
897
|
+
(nextProps.columnCount === 0 && prevState.scrollLeft !== 0) ||
|
|
898
|
+
(nextProps.rowCount === 0 && prevState.scrollTop !== 0)
|
|
899
|
+
) {
|
|
900
|
+
newState.scrollLeft = 0;
|
|
901
|
+
newState.scrollTop = 0;
|
|
902
|
+
|
|
903
|
+
// only use scroll{Left,Top} from props if scrollTo{Column,Row} isn't specified
|
|
904
|
+
// scrollTo{Column,Row} should override scroll{Left,Top}
|
|
905
|
+
} else if (
|
|
906
|
+
(nextProps.scrollLeft !== instanceProps.prevScrollLeft &&
|
|
907
|
+
nextProps.scrollToColumn < 0) ||
|
|
908
|
+
(nextProps.scrollTop !== instanceProps.prevScrollTop && nextProps.scrollToRow < 0)
|
|
909
|
+
) {
|
|
910
|
+
Object.assign(
|
|
911
|
+
newState,
|
|
912
|
+
Grid._getScrollToPositionStateUpdate({
|
|
913
|
+
prevState,
|
|
914
|
+
scrollLeft: nextProps.scrollLeft,
|
|
915
|
+
scrollTop: nextProps.scrollTop,
|
|
916
|
+
}),
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// Initially we should not clearStyleCache
|
|
921
|
+
newState.needToResetStyleCache = false;
|
|
922
|
+
if (
|
|
923
|
+
nextProps.columnWidth !== instanceProps.prevColumnWidth ||
|
|
924
|
+
nextProps.rowHeight !== instanceProps.prevRowHeight
|
|
925
|
+
) {
|
|
926
|
+
// Reset cache. set it to {} in render
|
|
927
|
+
newState.needToResetStyleCache = true;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
instanceProps.columnSizeAndPositionManager.configure({
|
|
931
|
+
cellCount: nextProps.columnCount,
|
|
932
|
+
estimatedCellSize: Grid._getEstimatedColumnSize(nextProps),
|
|
933
|
+
cellSizeGetter: Grid._wrapSizeGetter(nextProps.columnWidth),
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
instanceProps.rowSizeAndPositionManager.configure({
|
|
937
|
+
cellCount: nextProps.rowCount,
|
|
938
|
+
estimatedCellSize: Grid._getEstimatedRowSize(nextProps),
|
|
939
|
+
cellSizeGetter: Grid._wrapSizeGetter(nextProps.rowHeight),
|
|
940
|
+
cellsHeight: nextProps.cellsHeight,
|
|
941
|
+
editorAreaHeight: nextProps.editorAreaHeight,
|
|
942
|
+
totalSize: nextProps.totalSize,
|
|
943
|
+
editorsOffset: nextProps.editorsOffset,
|
|
944
|
+
});
|
|
945
|
+
|
|
946
|
+
if (instanceProps.prevColumnCount === 0 || instanceProps.prevRowCount === 0) {
|
|
947
|
+
instanceProps.prevColumnCount = 0;
|
|
948
|
+
instanceProps.prevRowCount = 0;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
// If scrolling is controlled outside this component, clear cache when scrolling stops
|
|
952
|
+
if (
|
|
953
|
+
nextProps.autoHeight &&
|
|
954
|
+
nextProps.isScrolling === false &&
|
|
955
|
+
instanceProps.prevIsScrolling === true
|
|
956
|
+
) {
|
|
957
|
+
Object.assign(newState, {
|
|
958
|
+
isScrolling: false,
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
let maybeStateA: any;
|
|
963
|
+
let maybeStateB: any;
|
|
964
|
+
|
|
965
|
+
calculateSizeAndPositionDataAndUpdateScrollOffset({
|
|
966
|
+
cellCount: instanceProps.prevColumnCount,
|
|
967
|
+
cellSize:
|
|
968
|
+
typeof instanceProps.prevColumnWidth === 'number'
|
|
969
|
+
? instanceProps.prevColumnWidth
|
|
970
|
+
: null,
|
|
971
|
+
computeMetadataCallback: () =>
|
|
972
|
+
instanceProps.columnSizeAndPositionManager.resetCell(0),
|
|
973
|
+
computeMetadataCallbackProps: nextProps,
|
|
974
|
+
nextCellsCount: nextProps.columnCount,
|
|
975
|
+
nextCellSize:
|
|
976
|
+
typeof nextProps.columnWidth === 'number' ? nextProps.columnWidth : null,
|
|
977
|
+
nextScrollToIndex: nextProps.scrollToColumn,
|
|
978
|
+
scrollToIndex: instanceProps.prevScrollToColumn,
|
|
979
|
+
updateScrollOffsetForScrollToIndex: () => {
|
|
980
|
+
maybeStateA = Grid._getScrollLeftForScrollToColumnStateUpdate(
|
|
981
|
+
nextProps,
|
|
982
|
+
prevState,
|
|
983
|
+
);
|
|
984
|
+
},
|
|
985
|
+
});
|
|
986
|
+
calculateSizeAndPositionDataAndUpdateScrollOffset({
|
|
987
|
+
cellCount: instanceProps.prevRowCount,
|
|
988
|
+
cellSize:
|
|
989
|
+
typeof instanceProps.prevRowHeight === 'number'
|
|
990
|
+
? instanceProps.prevRowHeight
|
|
991
|
+
: null,
|
|
992
|
+
computeMetadataCallback: () =>
|
|
993
|
+
instanceProps.rowSizeAndPositionManager.resetCell(0),
|
|
994
|
+
computeMetadataCallbackProps: nextProps,
|
|
995
|
+
nextCellsCount: nextProps.rowCount,
|
|
996
|
+
nextCellSize:
|
|
997
|
+
typeof nextProps.rowHeight === 'number' ? nextProps.rowHeight : null,
|
|
998
|
+
nextScrollToIndex: nextProps.scrollToRow,
|
|
999
|
+
scrollToIndex: instanceProps.prevScrollToRow,
|
|
1000
|
+
updateScrollOffsetForScrollToIndex: () => {
|
|
1001
|
+
maybeStateB = Grid._getScrollTopForScrollToRowStateUpdate(nextProps, prevState);
|
|
1002
|
+
},
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
instanceProps.prevColumnCount = nextProps.columnCount;
|
|
1006
|
+
instanceProps.prevColumnWidth = nextProps.columnWidth;
|
|
1007
|
+
instanceProps.prevIsScrolling = nextProps.isScrolling === true;
|
|
1008
|
+
instanceProps.prevRowCount = nextProps.rowCount;
|
|
1009
|
+
instanceProps.prevRowHeight = nextProps.rowHeight;
|
|
1010
|
+
instanceProps.prevScrollToColumn = nextProps.scrollToColumn;
|
|
1011
|
+
instanceProps.prevScrollToRow = nextProps.scrollToRow;
|
|
1012
|
+
instanceProps.prevScrollLeft = nextProps.scrollLeft;
|
|
1013
|
+
instanceProps.prevScrollTop = nextProps.scrollTop;
|
|
1014
|
+
|
|
1015
|
+
// getting scrollBarSize (moved from componentWillMount)
|
|
1016
|
+
instanceProps.scrollbarSize = nextProps.getScrollbarSize();
|
|
1017
|
+
if (instanceProps.scrollbarSize === undefined) {
|
|
1018
|
+
instanceProps.scrollbarSizeMeasured = false;
|
|
1019
|
+
instanceProps.scrollbarSize = 0;
|
|
1020
|
+
} else {
|
|
1021
|
+
instanceProps.scrollbarSizeMeasured = true;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
newState.instanceProps = instanceProps;
|
|
1025
|
+
|
|
1026
|
+
return { ...newState, ...maybeStateA, ...maybeStateB };
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
override render() {
|
|
1030
|
+
const {
|
|
1031
|
+
autoContainerWidth,
|
|
1032
|
+
autoHeight,
|
|
1033
|
+
autoWidth,
|
|
1034
|
+
className,
|
|
1035
|
+
containerProps,
|
|
1036
|
+
containerRole,
|
|
1037
|
+
containerStyle,
|
|
1038
|
+
height,
|
|
1039
|
+
id,
|
|
1040
|
+
noContentRenderer,
|
|
1041
|
+
role,
|
|
1042
|
+
style,
|
|
1043
|
+
tabIndex,
|
|
1044
|
+
width,
|
|
1045
|
+
noEditorArea,
|
|
1046
|
+
} = this.props;
|
|
1047
|
+
const { instanceProps, needToResetStyleCache } = this.state;
|
|
1048
|
+
|
|
1049
|
+
const isScrolling = this._isScrolling();
|
|
1050
|
+
|
|
1051
|
+
const gridStyle: any = {
|
|
1052
|
+
boxSizing: 'border-box',
|
|
1053
|
+
direction: 'ltr',
|
|
1054
|
+
height: autoHeight ? 'auto' : height,
|
|
1055
|
+
position: 'relative',
|
|
1056
|
+
width: autoWidth ? 'auto' : width,
|
|
1057
|
+
WebkitOverflowScrolling: 'touch',
|
|
1058
|
+
willChange: 'transform',
|
|
1059
|
+
};
|
|
1060
|
+
|
|
1061
|
+
if (needToResetStyleCache) {
|
|
1062
|
+
this._styleCache = {};
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// calculate _styleCache here
|
|
1066
|
+
// if state.isScrolling (not from _isScrolling) then reset
|
|
1067
|
+
if (!this.state.isScrolling) {
|
|
1068
|
+
this._resetStyleCache();
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// calculate children to render here
|
|
1072
|
+
this._calculateChildrenToRender(this.props, this.state);
|
|
1073
|
+
|
|
1074
|
+
const totalColumnsWidth = instanceProps.columnSizeAndPositionManager.getTotalSize();
|
|
1075
|
+
const totalRowsHeight = instanceProps.rowSizeAndPositionManager.getTotalSize();
|
|
1076
|
+
|
|
1077
|
+
// Force browser to hide scrollbars when we know they aren't necessary.
|
|
1078
|
+
// Otherwise once scrollbars appear they may not disappear again.
|
|
1079
|
+
// For more info see issue #116
|
|
1080
|
+
const verticalScrollBarSize =
|
|
1081
|
+
totalRowsHeight > height ? instanceProps.scrollbarSize : 0;
|
|
1082
|
+
const horizontalScrollBarSize =
|
|
1083
|
+
totalColumnsWidth > width ? instanceProps.scrollbarSize : 0;
|
|
1084
|
+
|
|
1085
|
+
if (
|
|
1086
|
+
horizontalScrollBarSize !== this._horizontalScrollBarSize ||
|
|
1087
|
+
verticalScrollBarSize !== this._verticalScrollBarSize
|
|
1088
|
+
) {
|
|
1089
|
+
this._horizontalScrollBarSize = horizontalScrollBarSize;
|
|
1090
|
+
this._verticalScrollBarSize = verticalScrollBarSize;
|
|
1091
|
+
this._scrollbarPresenceChanged = true;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
// Also explicitly init styles to 'auto' if scrollbars are required.
|
|
1095
|
+
// This works around an obscure edge case where external CSS styles have not yet been loaded,
|
|
1096
|
+
// But an initial scroll index of offset is set as an external prop.
|
|
1097
|
+
// Without this style, Grid would render the correct range of cells but would NOT update its internal offset.
|
|
1098
|
+
// This was originally reported via clauderic/react-infinite-calendar/issues/23
|
|
1099
|
+
gridStyle.overflowX =
|
|
1100
|
+
totalColumnsWidth + verticalScrollBarSize <= width ? 'hidden' : 'auto';
|
|
1101
|
+
gridStyle.overflowY =
|
|
1102
|
+
totalRowsHeight + horizontalScrollBarSize <= height ? 'hidden' : 'auto';
|
|
1103
|
+
|
|
1104
|
+
const childrenToDisplay = this._childrenToDisplay;
|
|
1105
|
+
|
|
1106
|
+
const showNoContentRenderer =
|
|
1107
|
+
childrenToDisplay.length === 0 && height > 0 && width > 0;
|
|
1108
|
+
|
|
1109
|
+
return (
|
|
1110
|
+
<div
|
|
1111
|
+
ref={this._setScrollingContainerRef}
|
|
1112
|
+
{...containerProps}
|
|
1113
|
+
aria-label={this.props['aria-label']}
|
|
1114
|
+
aria-readonly={this.props['aria-readonly']}
|
|
1115
|
+
className={classNames('ReactVirtualized__Grid', className)}
|
|
1116
|
+
id={id}
|
|
1117
|
+
onScroll={this._onScroll}
|
|
1118
|
+
role={role}
|
|
1119
|
+
style={{
|
|
1120
|
+
...gridStyle,
|
|
1121
|
+
...style,
|
|
1122
|
+
}}
|
|
1123
|
+
tabIndex={tabIndex}
|
|
1124
|
+
>
|
|
1125
|
+
<div
|
|
1126
|
+
className="ReactVirtualized__Grid__innerScrollContainer"
|
|
1127
|
+
role={containerRole}
|
|
1128
|
+
style={{
|
|
1129
|
+
width: autoContainerWidth ? 'auto' : totalColumnsWidth,
|
|
1130
|
+
height: totalRowsHeight,
|
|
1131
|
+
maxWidth: totalColumnsWidth,
|
|
1132
|
+
maxHeight: totalRowsHeight,
|
|
1133
|
+
overflow: 'hidden',
|
|
1134
|
+
pointerEvents: isScrolling ? 'none' : undefined,
|
|
1135
|
+
position: 'relative',
|
|
1136
|
+
...containerStyle,
|
|
1137
|
+
}}
|
|
1138
|
+
>
|
|
1139
|
+
{/* <div
|
|
1140
|
+
style={{
|
|
1141
|
+
position: 'absolute',
|
|
1142
|
+
visibility: 'visible',
|
|
1143
|
+
width: '100%',
|
|
1144
|
+
height: '100%',
|
|
1145
|
+
}}
|
|
1146
|
+
> */}
|
|
1147
|
+
{noEditorArea}
|
|
1148
|
+
{/* </div> */}
|
|
1149
|
+
{childrenToDisplay.length > 0 && childrenToDisplay}
|
|
1150
|
+
</div>
|
|
1151
|
+
{showNoContentRenderer && noContentRenderer()}
|
|
1152
|
+
</div>
|
|
1153
|
+
);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/* ---------------------------- Helper methods ---------------------------- */
|
|
1157
|
+
|
|
1158
|
+
_calculateChildrenToRender(props: Props = this.props, state: State = this.state) {
|
|
1159
|
+
const {
|
|
1160
|
+
cellRenderer,
|
|
1161
|
+
cellRangeRenderer,
|
|
1162
|
+
columnCount,
|
|
1163
|
+
deferredMeasurementCache,
|
|
1164
|
+
height,
|
|
1165
|
+
overscanColumnCount,
|
|
1166
|
+
overscanIndicesGetter,
|
|
1167
|
+
overscanRowCount,
|
|
1168
|
+
rowCount,
|
|
1169
|
+
width,
|
|
1170
|
+
isScrollingOptOut,
|
|
1171
|
+
} = props;
|
|
1172
|
+
|
|
1173
|
+
const { scrollDirectionHorizontal, scrollDirectionVertical, instanceProps } = state;
|
|
1174
|
+
|
|
1175
|
+
const scrollTop =
|
|
1176
|
+
this._initialScrollTop > 0 ? this._initialScrollTop : state.scrollTop;
|
|
1177
|
+
const scrollLeft =
|
|
1178
|
+
this._initialScrollLeft > 0 ? this._initialScrollLeft : state.scrollLeft;
|
|
1179
|
+
|
|
1180
|
+
const isScrolling = this._isScrolling(props, state);
|
|
1181
|
+
|
|
1182
|
+
this._childrenToDisplay = [];
|
|
1183
|
+
|
|
1184
|
+
// Render only enough columns and rows to cover the visible area of the grid.
|
|
1185
|
+
if (height > 0 && width > 0) {
|
|
1186
|
+
const visibleColumnIndices =
|
|
1187
|
+
instanceProps.columnSizeAndPositionManager.getVisibleCellRange({
|
|
1188
|
+
containerSize: width,
|
|
1189
|
+
offset: scrollLeft,
|
|
1190
|
+
});
|
|
1191
|
+
|
|
1192
|
+
const visibleRowIndices =
|
|
1193
|
+
instanceProps.rowSizeAndPositionManager.getVisibleCellRange({
|
|
1194
|
+
containerSize: height,
|
|
1195
|
+
offset: scrollTop,
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
const horizontalOffsetAdjustment =
|
|
1199
|
+
instanceProps.columnSizeAndPositionManager.getOffsetAdjustment({
|
|
1200
|
+
containerSize: width,
|
|
1201
|
+
offset: scrollLeft,
|
|
1202
|
+
});
|
|
1203
|
+
const verticalOffsetAdjustment =
|
|
1204
|
+
instanceProps.rowSizeAndPositionManager.getOffsetAdjustment({
|
|
1205
|
+
containerSize: height,
|
|
1206
|
+
offset: scrollTop,
|
|
1207
|
+
});
|
|
1208
|
+
|
|
1209
|
+
// Store for _invokeOnGridRenderedHelper()
|
|
1210
|
+
this._renderedColumnStartIndex = visibleColumnIndices.start;
|
|
1211
|
+
this._renderedColumnStopIndex = visibleColumnIndices.stop;
|
|
1212
|
+
this._renderedRowStartIndex = visibleRowIndices.start;
|
|
1213
|
+
this._renderedRowStopIndex = visibleRowIndices.stop;
|
|
1214
|
+
|
|
1215
|
+
const overscanColumnIndices = overscanIndicesGetter({
|
|
1216
|
+
direction: 'horizontal',
|
|
1217
|
+
cellCount: columnCount,
|
|
1218
|
+
overscanCellsCount: overscanColumnCount,
|
|
1219
|
+
scrollDirection: scrollDirectionHorizontal,
|
|
1220
|
+
startIndex:
|
|
1221
|
+
typeof visibleColumnIndices.start === 'number'
|
|
1222
|
+
? visibleColumnIndices.start
|
|
1223
|
+
: 0,
|
|
1224
|
+
stopIndex:
|
|
1225
|
+
typeof visibleColumnIndices.stop === 'number'
|
|
1226
|
+
? visibleColumnIndices.stop
|
|
1227
|
+
: -1,
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
const overscanRowIndices = overscanIndicesGetter({
|
|
1231
|
+
direction: 'vertical',
|
|
1232
|
+
cellCount: rowCount,
|
|
1233
|
+
overscanCellsCount: overscanRowCount,
|
|
1234
|
+
scrollDirection: scrollDirectionVertical,
|
|
1235
|
+
startIndex:
|
|
1236
|
+
typeof visibleRowIndices.start === 'number' ? visibleRowIndices.start : 0,
|
|
1237
|
+
stopIndex:
|
|
1238
|
+
typeof visibleRowIndices.stop === 'number' ? visibleRowIndices.stop : -1,
|
|
1239
|
+
});
|
|
1240
|
+
|
|
1241
|
+
// Store for _invokeOnGridRenderedHelper()
|
|
1242
|
+
let columnStartIndex = overscanColumnIndices.overscanStartIndex;
|
|
1243
|
+
let columnStopIndex = overscanColumnIndices.overscanStopIndex;
|
|
1244
|
+
let rowStartIndex = overscanRowIndices.overscanStartIndex;
|
|
1245
|
+
let rowStopIndex = overscanRowIndices.overscanStopIndex;
|
|
1246
|
+
|
|
1247
|
+
// Advanced use-cases (eg CellMeasurer) require batched measurements to determine accurate sizes.
|
|
1248
|
+
if (deferredMeasurementCache) {
|
|
1249
|
+
// If rows have a dynamic height, scan the rows we are about to render.
|
|
1250
|
+
// If any have not yet been measured, then we need to render all columns initially,
|
|
1251
|
+
// Because the height of the row is equal to the tallest cell within that row,
|
|
1252
|
+
// (And so we can't know the height without measuring all column-cells first).
|
|
1253
|
+
if (!deferredMeasurementCache.hasFixedHeight()) {
|
|
1254
|
+
for (let rowIndex = rowStartIndex; rowIndex <= rowStopIndex; rowIndex++) {
|
|
1255
|
+
if (!deferredMeasurementCache.has(rowIndex, 0)) {
|
|
1256
|
+
columnStartIndex = 0;
|
|
1257
|
+
columnStopIndex = columnCount - 1;
|
|
1258
|
+
break;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
// If columns have a dynamic width, scan the columns we are about to render.
|
|
1264
|
+
// If any have not yet been measured, then we need to render all rows initially,
|
|
1265
|
+
// Because the width of the column is equal to the widest cell within that column,
|
|
1266
|
+
// (And so we can't know the width without measuring all row-cells first).
|
|
1267
|
+
if (!deferredMeasurementCache.hasFixedWidth()) {
|
|
1268
|
+
for (
|
|
1269
|
+
let columnIndex = columnStartIndex;
|
|
1270
|
+
columnIndex <= columnStopIndex;
|
|
1271
|
+
columnIndex++
|
|
1272
|
+
) {
|
|
1273
|
+
if (!deferredMeasurementCache.has(0, columnIndex)) {
|
|
1274
|
+
rowStartIndex = 0;
|
|
1275
|
+
rowStopIndex = rowCount - 1;
|
|
1276
|
+
break;
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
// 计算偏差
|
|
1283
|
+
this._childrenToDisplay = cellRangeRenderer({
|
|
1284
|
+
cellCache: this._cellCache,
|
|
1285
|
+
cellRenderer,
|
|
1286
|
+
columnSizeAndPositionManager: instanceProps.columnSizeAndPositionManager,
|
|
1287
|
+
columnStartIndex,
|
|
1288
|
+
columnStopIndex,
|
|
1289
|
+
deferredMeasurementCache,
|
|
1290
|
+
horizontalOffsetAdjustment,
|
|
1291
|
+
isScrolling,
|
|
1292
|
+
isScrollingOptOut,
|
|
1293
|
+
parent: this,
|
|
1294
|
+
rowSizeAndPositionManager: instanceProps.rowSizeAndPositionManager,
|
|
1295
|
+
rowStartIndex,
|
|
1296
|
+
rowStopIndex,
|
|
1297
|
+
scrollLeft,
|
|
1298
|
+
scrollTop,
|
|
1299
|
+
styleCache: this._styleCache,
|
|
1300
|
+
verticalOffsetAdjustment,
|
|
1301
|
+
visibleColumnIndices,
|
|
1302
|
+
visibleRowIndices,
|
|
1303
|
+
});
|
|
1304
|
+
|
|
1305
|
+
// update the indices
|
|
1306
|
+
this._columnStartIndex = columnStartIndex;
|
|
1307
|
+
this._columnStopIndex = columnStopIndex;
|
|
1308
|
+
this._rowStartIndex = rowStartIndex;
|
|
1309
|
+
this._rowStopIndex = rowStopIndex;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/**
|
|
1314
|
+
* Sets an :isScrolling flag for a small window of time.
|
|
1315
|
+
* This flag is used to disable pointer events on the scrollable portion of the Grid.
|
|
1316
|
+
* This prevents jerky/stuttery mouse-wheel scrolling.
|
|
1317
|
+
*/
|
|
1318
|
+
_debounceScrollEnded() {
|
|
1319
|
+
const { scrollingResetTimeInterval } = this.props;
|
|
1320
|
+
|
|
1321
|
+
if (this._disablePointerEventsTimeoutId) {
|
|
1322
|
+
cancelAnimationTimeout(this._disablePointerEventsTimeoutId);
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
this._disablePointerEventsTimeoutId = requestAnimationTimeout(
|
|
1326
|
+
this._debounceScrollEndedCallback,
|
|
1327
|
+
scrollingResetTimeInterval,
|
|
1328
|
+
);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
_debounceScrollEndedCallback = () => {
|
|
1332
|
+
this._disablePointerEventsTimeoutId = null;
|
|
1333
|
+
// isScrolling is used to determine if we reset styleCache
|
|
1334
|
+
this.setState({
|
|
1335
|
+
isScrolling: false,
|
|
1336
|
+
needToResetStyleCache: false,
|
|
1337
|
+
});
|
|
1338
|
+
};
|
|
1339
|
+
|
|
1340
|
+
static _getEstimatedColumnSize(props: Props) {
|
|
1341
|
+
return typeof props.columnWidth === 'number'
|
|
1342
|
+
? props.columnWidth
|
|
1343
|
+
: props.estimatedColumnSize;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
static _getEstimatedRowSize(props: Props) {
|
|
1347
|
+
return typeof props.rowHeight === 'number'
|
|
1348
|
+
? props.rowHeight
|
|
1349
|
+
: props.estimatedRowSize;
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
/**
|
|
1353
|
+
* Check for batched CellMeasurer size invalidations.
|
|
1354
|
+
* This will occur the first time one or more previously unmeasured cells are rendered.
|
|
1355
|
+
*/
|
|
1356
|
+
_handleInvalidatedGridSize() {
|
|
1357
|
+
if (
|
|
1358
|
+
typeof this._deferredInvalidateColumnIndex === 'number' &&
|
|
1359
|
+
typeof this._deferredInvalidateRowIndex === 'number'
|
|
1360
|
+
) {
|
|
1361
|
+
const columnIndex = this._deferredInvalidateColumnIndex;
|
|
1362
|
+
const rowIndex = this._deferredInvalidateRowIndex;
|
|
1363
|
+
|
|
1364
|
+
this._deferredInvalidateColumnIndex = null;
|
|
1365
|
+
this._deferredInvalidateRowIndex = null;
|
|
1366
|
+
|
|
1367
|
+
this.recomputeGridSize({ columnIndex, rowIndex });
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
_invokeOnGridRenderedHelper = () => {
|
|
1372
|
+
const { onSectionRendered } = this.props;
|
|
1373
|
+
|
|
1374
|
+
this._onGridRenderedMemoizer({
|
|
1375
|
+
callback: onSectionRendered,
|
|
1376
|
+
indices: {
|
|
1377
|
+
columnOverscanStartIndex: this._columnStartIndex,
|
|
1378
|
+
columnOverscanStopIndex: this._columnStopIndex,
|
|
1379
|
+
columnStartIndex: this._renderedColumnStartIndex,
|
|
1380
|
+
columnStopIndex: this._renderedColumnStopIndex,
|
|
1381
|
+
rowOverscanStartIndex: this._rowStartIndex,
|
|
1382
|
+
rowOverscanStopIndex: this._rowStopIndex,
|
|
1383
|
+
rowStartIndex: this._renderedRowStartIndex,
|
|
1384
|
+
rowStopIndex: this._renderedRowStopIndex,
|
|
1385
|
+
},
|
|
1386
|
+
});
|
|
1387
|
+
};
|
|
1388
|
+
|
|
1389
|
+
_invokeOnScrollMemoizer({
|
|
1390
|
+
scrollLeft,
|
|
1391
|
+
scrollTop,
|
|
1392
|
+
totalColumnsWidth,
|
|
1393
|
+
totalRowsHeight,
|
|
1394
|
+
}: {
|
|
1395
|
+
scrollLeft: number;
|
|
1396
|
+
scrollTop: number;
|
|
1397
|
+
totalColumnsWidth: number;
|
|
1398
|
+
totalRowsHeight: number;
|
|
1399
|
+
}) {
|
|
1400
|
+
this._onScrollMemoizer({
|
|
1401
|
+
callback: ({ scrollLeft, scrollTop }: any) => {
|
|
1402
|
+
const { height, onScroll, width } = this.props;
|
|
1403
|
+
|
|
1404
|
+
onScroll({
|
|
1405
|
+
clientHeight: height,
|
|
1406
|
+
clientWidth: width,
|
|
1407
|
+
scrollHeight: totalRowsHeight,
|
|
1408
|
+
scrollLeft,
|
|
1409
|
+
scrollTop,
|
|
1410
|
+
scrollWidth: totalColumnsWidth,
|
|
1411
|
+
});
|
|
1412
|
+
},
|
|
1413
|
+
indices: {
|
|
1414
|
+
scrollLeft,
|
|
1415
|
+
scrollTop,
|
|
1416
|
+
},
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
_isScrolling(props: Props = this.props, state: State = this.state): boolean {
|
|
1421
|
+
// If isScrolling is defined in props, use it to override the value in state
|
|
1422
|
+
// This is a performance optimization for WindowScroller + Grid
|
|
1423
|
+
return Object.hasOwnProperty.call(props, 'isScrolling')
|
|
1424
|
+
? Boolean(props.isScrolling)
|
|
1425
|
+
: Boolean(state.isScrolling);
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
_maybeCallOnScrollbarPresenceChange() {
|
|
1429
|
+
if (this._scrollbarPresenceChanged) {
|
|
1430
|
+
const { onScrollbarPresenceChange } = this.props;
|
|
1431
|
+
|
|
1432
|
+
this._scrollbarPresenceChanged = false;
|
|
1433
|
+
|
|
1434
|
+
onScrollbarPresenceChange({
|
|
1435
|
+
horizontal: this._horizontalScrollBarSize > 0,
|
|
1436
|
+
size: this.state.instanceProps.scrollbarSize,
|
|
1437
|
+
vertical: this._verticalScrollBarSize > 0,
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
_setScrollingContainerRef = (ref: HTMLDivElement) => {
|
|
1443
|
+
this._scrollingContainer = ref;
|
|
1444
|
+
};
|
|
1445
|
+
|
|
1446
|
+
/**
|
|
1447
|
+
* Get the updated state after scrolling to
|
|
1448
|
+
* scrollLeft and scrollTop
|
|
1449
|
+
*/
|
|
1450
|
+
static _getScrollToPositionStateUpdate({
|
|
1451
|
+
prevState,
|
|
1452
|
+
scrollLeft,
|
|
1453
|
+
scrollTop,
|
|
1454
|
+
}: {
|
|
1455
|
+
prevState: State;
|
|
1456
|
+
scrollLeft?: number;
|
|
1457
|
+
scrollTop?: number;
|
|
1458
|
+
}) {
|
|
1459
|
+
const newState: any = {
|
|
1460
|
+
scrollPositionChangeReason: SCROLL_POSITION_CHANGE_REASONS.REQUESTED,
|
|
1461
|
+
};
|
|
1462
|
+
|
|
1463
|
+
if (typeof scrollLeft === 'number' && scrollLeft >= 0) {
|
|
1464
|
+
newState.scrollDirectionHorizontal =
|
|
1465
|
+
scrollLeft > prevState.scrollLeft
|
|
1466
|
+
? SCROLL_DIRECTION_FORWARD
|
|
1467
|
+
: SCROLL_DIRECTION_BACKWARD;
|
|
1468
|
+
newState.scrollLeft = scrollLeft;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
if (typeof scrollTop === 'number' && scrollTop >= 0) {
|
|
1472
|
+
newState.scrollDirectionVertical =
|
|
1473
|
+
scrollTop > prevState.scrollTop
|
|
1474
|
+
? SCROLL_DIRECTION_FORWARD
|
|
1475
|
+
: SCROLL_DIRECTION_BACKWARD;
|
|
1476
|
+
newState.scrollTop = scrollTop;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
if (
|
|
1480
|
+
(typeof scrollLeft === 'number' &&
|
|
1481
|
+
scrollLeft >= 0 &&
|
|
1482
|
+
scrollLeft !== prevState.scrollLeft) ||
|
|
1483
|
+
(typeof scrollTop === 'number' &&
|
|
1484
|
+
scrollTop >= 0 &&
|
|
1485
|
+
scrollTop !== prevState.scrollTop)
|
|
1486
|
+
) {
|
|
1487
|
+
return newState;
|
|
1488
|
+
}
|
|
1489
|
+
return null;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* Scroll to the specified offset(s).
|
|
1494
|
+
* Useful for animating position changes.
|
|
1495
|
+
*/
|
|
1496
|
+
scrollToPosition({ scrollLeft, scrollTop }: ScrollPosition) {
|
|
1497
|
+
const stateUpdate = Grid._getScrollToPositionStateUpdate({
|
|
1498
|
+
prevState: this.state,
|
|
1499
|
+
scrollLeft,
|
|
1500
|
+
scrollTop,
|
|
1501
|
+
});
|
|
1502
|
+
|
|
1503
|
+
if (stateUpdate) {
|
|
1504
|
+
stateUpdate.needToResetStyleCache = false;
|
|
1505
|
+
this.setState(stateUpdate);
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
static _wrapSizeGetter(value: CellSize): CellSizeGetter {
|
|
1510
|
+
return typeof value === 'function' ? value : () => value;
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
static _getCalculatedScrollLeft(nextProps: Props, prevState: State) {
|
|
1514
|
+
const { columnCount, height, scrollToAlignment, scrollToColumn, width } = nextProps;
|
|
1515
|
+
const { scrollLeft, instanceProps } = prevState;
|
|
1516
|
+
|
|
1517
|
+
if (columnCount > 0) {
|
|
1518
|
+
const finalColumn = columnCount - 1;
|
|
1519
|
+
const targetIndex =
|
|
1520
|
+
scrollToColumn < 0 ? finalColumn : Math.min(finalColumn, scrollToColumn);
|
|
1521
|
+
const totalRowsHeight = instanceProps.rowSizeAndPositionManager.getTotalSize();
|
|
1522
|
+
const scrollBarSize =
|
|
1523
|
+
instanceProps.scrollbarSizeMeasured && totalRowsHeight > height
|
|
1524
|
+
? instanceProps.scrollbarSize
|
|
1525
|
+
: 0;
|
|
1526
|
+
|
|
1527
|
+
return instanceProps.columnSizeAndPositionManager.getUpdatedOffsetForIndex({
|
|
1528
|
+
align: scrollToAlignment,
|
|
1529
|
+
containerSize: width - scrollBarSize,
|
|
1530
|
+
currentOffset: scrollLeft,
|
|
1531
|
+
targetIndex,
|
|
1532
|
+
});
|
|
1533
|
+
}
|
|
1534
|
+
return 0;
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
_getCalculatedScrollLeft(props: Props = this.props, state: State = this.state) {
|
|
1538
|
+
return Grid._getCalculatedScrollLeft(props, state);
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
static _getScrollLeftForScrollToColumnStateUpdate(
|
|
1542
|
+
nextProps: Props,
|
|
1543
|
+
prevState: State,
|
|
1544
|
+
) {
|
|
1545
|
+
const { scrollLeft } = prevState;
|
|
1546
|
+
const calculatedScrollLeft = Grid._getCalculatedScrollLeft(nextProps, prevState);
|
|
1547
|
+
|
|
1548
|
+
if (
|
|
1549
|
+
typeof calculatedScrollLeft === 'number' &&
|
|
1550
|
+
calculatedScrollLeft >= 0 &&
|
|
1551
|
+
scrollLeft !== calculatedScrollLeft
|
|
1552
|
+
) {
|
|
1553
|
+
return Grid._getScrollToPositionStateUpdate({
|
|
1554
|
+
prevState,
|
|
1555
|
+
scrollLeft: calculatedScrollLeft,
|
|
1556
|
+
scrollTop: -1,
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
return null;
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
_updateScrollLeftForScrollToColumn(
|
|
1563
|
+
props: Props = this.props,
|
|
1564
|
+
state: State = this.state,
|
|
1565
|
+
) {
|
|
1566
|
+
const stateUpdate = Grid._getScrollLeftForScrollToColumnStateUpdate(props, state);
|
|
1567
|
+
if (stateUpdate) {
|
|
1568
|
+
stateUpdate.needToResetStyleCache = false;
|
|
1569
|
+
this.setState(stateUpdate);
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
static _getCalculatedScrollTop(nextProps: Props, prevState: State) {
|
|
1574
|
+
const { height, rowCount, scrollToAlignment, scrollToRow, width } = nextProps;
|
|
1575
|
+
const { scrollTop, instanceProps } = prevState;
|
|
1576
|
+
|
|
1577
|
+
if (rowCount > 0) {
|
|
1578
|
+
const finalRow = rowCount - 1;
|
|
1579
|
+
const targetIndex = scrollToRow < 0 ? finalRow : Math.min(finalRow, scrollToRow);
|
|
1580
|
+
const totalColumnsWidth =
|
|
1581
|
+
instanceProps.columnSizeAndPositionManager.getTotalSize();
|
|
1582
|
+
const scrollBarSize =
|
|
1583
|
+
instanceProps.scrollbarSizeMeasured && totalColumnsWidth > width
|
|
1584
|
+
? instanceProps.scrollbarSize
|
|
1585
|
+
: 0;
|
|
1586
|
+
|
|
1587
|
+
return instanceProps.rowSizeAndPositionManager.getUpdatedOffsetForIndex({
|
|
1588
|
+
align: scrollToAlignment,
|
|
1589
|
+
containerSize: height - scrollBarSize,
|
|
1590
|
+
currentOffset: scrollTop,
|
|
1591
|
+
targetIndex,
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
return 0;
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
_getCalculatedScrollTop(props: Props = this.props, state: State = this.state) {
|
|
1598
|
+
return Grid._getCalculatedScrollTop(props, state);
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
_resetStyleCache() {
|
|
1602
|
+
const styleCache = this._styleCache;
|
|
1603
|
+
const cellCache = this._cellCache;
|
|
1604
|
+
const { isScrollingOptOut } = this.props;
|
|
1605
|
+
|
|
1606
|
+
// Reset cell and style caches once scrolling stops.
|
|
1607
|
+
// This makes Grid simpler to use (since cells commonly change).
|
|
1608
|
+
// And it keeps the caches from growing too large.
|
|
1609
|
+
// Performance is most sensitive when a user is scrolling.
|
|
1610
|
+
// Don't clear visible cells from cellCache if isScrollingOptOut is specified.
|
|
1611
|
+
// This keeps the cellCache to a resonable size.
|
|
1612
|
+
this._cellCache = {};
|
|
1613
|
+
this._styleCache = {};
|
|
1614
|
+
|
|
1615
|
+
// Copy over the visible cell styles so avoid unnecessary re-render.
|
|
1616
|
+
for (
|
|
1617
|
+
let rowIndex = this._rowStartIndex;
|
|
1618
|
+
rowIndex <= this._rowStopIndex;
|
|
1619
|
+
rowIndex++
|
|
1620
|
+
) {
|
|
1621
|
+
for (
|
|
1622
|
+
let columnIndex = this._columnStartIndex;
|
|
1623
|
+
columnIndex <= this._columnStopIndex;
|
|
1624
|
+
columnIndex++
|
|
1625
|
+
) {
|
|
1626
|
+
const key = `${rowIndex}-${columnIndex}`;
|
|
1627
|
+
this._styleCache[key] = styleCache[key];
|
|
1628
|
+
|
|
1629
|
+
if (isScrollingOptOut) {
|
|
1630
|
+
this._cellCache[key] = cellCache[key];
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
static _getScrollTopForScrollToRowStateUpdate(nextProps: Props, prevState: State) {
|
|
1637
|
+
const { scrollTop } = prevState;
|
|
1638
|
+
const calculatedScrollTop = Grid._getCalculatedScrollTop(nextProps, prevState);
|
|
1639
|
+
|
|
1640
|
+
if (
|
|
1641
|
+
typeof calculatedScrollTop === 'number' &&
|
|
1642
|
+
calculatedScrollTop >= 0 &&
|
|
1643
|
+
scrollTop !== calculatedScrollTop
|
|
1644
|
+
) {
|
|
1645
|
+
return Grid._getScrollToPositionStateUpdate({
|
|
1646
|
+
prevState,
|
|
1647
|
+
scrollLeft: -1,
|
|
1648
|
+
scrollTop: calculatedScrollTop,
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
return null;
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
_updateScrollTopForScrollToRow(props: Props = this.props, state: State = this.state) {
|
|
1655
|
+
const stateUpdate = Grid._getScrollTopForScrollToRowStateUpdate(props, state);
|
|
1656
|
+
if (stateUpdate) {
|
|
1657
|
+
stateUpdate.needToResetStyleCache = false;
|
|
1658
|
+
this.setState(stateUpdate);
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
_onScroll: React.UIEventHandler<HTMLDivElement> = (event) => {
|
|
1663
|
+
// In certain edge-cases React dispatches an onScroll event with an invalid target.scrollLeft / target.scrollTop.
|
|
1664
|
+
// This invalid event can be detected by comparing event.target to this component's scrollable DOM element.
|
|
1665
|
+
// See issue #404 for more information.
|
|
1666
|
+
if (event.target === this._scrollingContainer) {
|
|
1667
|
+
this.handleScrollEvent(event.target as any);
|
|
1668
|
+
}
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
export default Grid;
|