react-window 1.8.10 → 2.0.0-alpha.0
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/README.md +9 -161
- package/dist/react-window.cjs +22 -0
- package/dist/react-window.d.ts +217 -0
- package/dist/react-window.js +812 -0
- package/docs/assets/index-DlGpNu0r.css +1 -0
- package/docs/assets/index-fVOw1dKb.js +67 -0
- package/docs/data/addresses.json +7954 -0
- package/docs/data/contacts.json +4202 -0
- package/docs/data/names.json +1002 -0
- package/docs/favicon.svg +16 -0
- package/docs/generated/README.md +1 -0
- package/docs/generated/code-snippets/CellComponent.json +4 -0
- package/docs/generated/code-snippets/FixedHeightList.json +4 -0
- package/docs/generated/code-snippets/FixedHeightRowComponent.json +4 -0
- package/docs/generated/code-snippets/FlexboxLayout.json +4 -0
- package/docs/generated/code-snippets/Grid.json +4 -0
- package/docs/generated/code-snippets/ListVariableRowHeights.json +4 -0
- package/docs/generated/code-snippets/columnWidth.json +4 -0
- package/docs/generated/code-snippets/gridRefClickEventHandler.json +3 -0
- package/docs/generated/code-snippets/listRefClickEventHandler.json +3 -0
- package/docs/generated/code-snippets/rowHeight.json +4 -0
- package/docs/generated/code-snippets/useGridRef.json +4 -0
- package/docs/generated/code-snippets/useGridRefImport.json +3 -0
- package/docs/generated/code-snippets/useListRef.json +4 -0
- package/docs/generated/code-snippets/useListRefImport.json +3 -0
- package/docs/generated/js-docs/Grid.json +314 -0
- package/docs/generated/js-docs/List.json +266 -0
- package/docs/index.html +30 -0
- package/docs/og.html +42 -0
- package/docs/og.png +0 -0
- package/docs/stats.html +4949 -0
- package/docs/svgs/checkbox-checked.svg +1 -0
- package/docs/svgs/checkbox-indeterminate.svg +1 -0
- package/docs/svgs/checkbox-unchecked.svg +1 -0
- package/docs/svgs/github.svg +3 -0
- package/docs/svgs/npm.svg +1 -0
- package/docs/svgs/radio-checked.svg +1 -0
- package/docs/svgs/radio-unchecked.svg +1 -0
- package/package.json +70 -90
- package/LICENSE.md +0 -21
- package/dist/index-dev.umd.js +0 -2
- package/dist/index-dev.umd.js.map +0 -1
- package/dist/index-prod.umd.js +0 -2
- package/dist/index-prod.umd.js.map +0 -1
- package/dist/index.cjs.js +0 -2087
- package/dist/index.cjs.js.flow +0 -3
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -2076
- package/dist/index.esm.js.flow +0 -3
- package/dist/index.esm.js.map +0 -1
- package/src/FixedSizeGrid.js +0 -244
- package/src/FixedSizeList.js +0 -137
- package/src/VariableSizeGrid.js +0 -507
- package/src/VariableSizeList.js +0 -317
- package/src/areEqual.js +0 -18
- package/src/createGridComponent.js +0 -919
- package/src/createListComponent.js +0 -745
- package/src/domHelpers.js +0 -72
- package/src/index.js +0 -9
- package/src/shallowDiffers.js +0 -17
- package/src/shouldComponentUpdate.js +0 -16
- package/src/timer.js +0 -37
|
@@ -1,745 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import memoizeOne from 'memoize-one';
|
|
4
|
-
import { createElement, PureComponent } from 'react';
|
|
5
|
-
import { cancelTimeout, requestTimeout } from './timer';
|
|
6
|
-
import { getScrollbarSize, getRTLOffsetType } from './domHelpers';
|
|
7
|
-
|
|
8
|
-
import type { TimeoutID } from './timer';
|
|
9
|
-
|
|
10
|
-
export type ScrollToAlign = 'auto' | 'smart' | 'center' | 'start' | 'end';
|
|
11
|
-
|
|
12
|
-
type itemSize = number | ((index: number) => number);
|
|
13
|
-
// TODO Deprecate directions "horizontal" and "vertical"
|
|
14
|
-
type Direction = 'ltr' | 'rtl' | 'horizontal' | 'vertical';
|
|
15
|
-
type Layout = 'horizontal' | 'vertical';
|
|
16
|
-
|
|
17
|
-
type RenderComponentProps<T> = {|
|
|
18
|
-
data: T,
|
|
19
|
-
index: number,
|
|
20
|
-
isScrolling?: boolean,
|
|
21
|
-
style: Object,
|
|
22
|
-
|};
|
|
23
|
-
type RenderComponent<T> = React$ComponentType<$Shape<RenderComponentProps<T>>>;
|
|
24
|
-
|
|
25
|
-
type ScrollDirection = 'forward' | 'backward';
|
|
26
|
-
|
|
27
|
-
type onItemsRenderedCallback = ({
|
|
28
|
-
overscanStartIndex: number,
|
|
29
|
-
overscanStopIndex: number,
|
|
30
|
-
visibleStartIndex: number,
|
|
31
|
-
visibleStopIndex: number,
|
|
32
|
-
}) => void;
|
|
33
|
-
type onScrollCallback = ({
|
|
34
|
-
scrollDirection: ScrollDirection,
|
|
35
|
-
scrollOffset: number,
|
|
36
|
-
scrollUpdateWasRequested: boolean,
|
|
37
|
-
}) => void;
|
|
38
|
-
|
|
39
|
-
type ScrollEvent = SyntheticEvent<HTMLDivElement>;
|
|
40
|
-
type ItemStyleCache = { [index: number]: Object };
|
|
41
|
-
|
|
42
|
-
type OuterProps = {|
|
|
43
|
-
children: React$Node,
|
|
44
|
-
className: string | void,
|
|
45
|
-
onScroll: ScrollEvent => void,
|
|
46
|
-
style: {
|
|
47
|
-
[string]: mixed,
|
|
48
|
-
},
|
|
49
|
-
|};
|
|
50
|
-
|
|
51
|
-
type InnerProps = {|
|
|
52
|
-
children: React$Node,
|
|
53
|
-
style: {
|
|
54
|
-
[string]: mixed,
|
|
55
|
-
},
|
|
56
|
-
|};
|
|
57
|
-
|
|
58
|
-
export type Props<T> = {|
|
|
59
|
-
children: RenderComponent<T>,
|
|
60
|
-
className?: string,
|
|
61
|
-
direction: Direction,
|
|
62
|
-
height: number | string,
|
|
63
|
-
initialScrollOffset?: number,
|
|
64
|
-
innerRef?: any,
|
|
65
|
-
innerElementType?: string | React$AbstractComponent<InnerProps, any>,
|
|
66
|
-
innerTagName?: string, // deprecated
|
|
67
|
-
itemCount: number,
|
|
68
|
-
itemData: T,
|
|
69
|
-
itemKey?: (index: number, data: T) => any,
|
|
70
|
-
itemSize: itemSize,
|
|
71
|
-
layout: Layout,
|
|
72
|
-
onItemsRendered?: onItemsRenderedCallback,
|
|
73
|
-
onScroll?: onScrollCallback,
|
|
74
|
-
outerRef?: any,
|
|
75
|
-
outerElementType?: string | React$AbstractComponent<OuterProps, any>,
|
|
76
|
-
outerTagName?: string, // deprecated
|
|
77
|
-
overscanCount: number,
|
|
78
|
-
style?: Object,
|
|
79
|
-
useIsScrolling: boolean,
|
|
80
|
-
width: number | string,
|
|
81
|
-
|};
|
|
82
|
-
|
|
83
|
-
type State = {|
|
|
84
|
-
instance: any,
|
|
85
|
-
isScrolling: boolean,
|
|
86
|
-
scrollDirection: ScrollDirection,
|
|
87
|
-
scrollOffset: number,
|
|
88
|
-
scrollUpdateWasRequested: boolean,
|
|
89
|
-
|};
|
|
90
|
-
|
|
91
|
-
type GetItemOffset = (
|
|
92
|
-
props: Props<any>,
|
|
93
|
-
index: number,
|
|
94
|
-
instanceProps: any
|
|
95
|
-
) => number;
|
|
96
|
-
type GetItemSize = (
|
|
97
|
-
props: Props<any>,
|
|
98
|
-
index: number,
|
|
99
|
-
instanceProps: any
|
|
100
|
-
) => number;
|
|
101
|
-
type GetEstimatedTotalSize = (props: Props<any>, instanceProps: any) => number;
|
|
102
|
-
type GetOffsetForIndexAndAlignment = (
|
|
103
|
-
props: Props<any>,
|
|
104
|
-
index: number,
|
|
105
|
-
align: ScrollToAlign,
|
|
106
|
-
scrollOffset: number,
|
|
107
|
-
instanceProps: any
|
|
108
|
-
) => number;
|
|
109
|
-
type GetStartIndexForOffset = (
|
|
110
|
-
props: Props<any>,
|
|
111
|
-
offset: number,
|
|
112
|
-
instanceProps: any
|
|
113
|
-
) => number;
|
|
114
|
-
type GetStopIndexForStartIndex = (
|
|
115
|
-
props: Props<any>,
|
|
116
|
-
startIndex: number,
|
|
117
|
-
scrollOffset: number,
|
|
118
|
-
instanceProps: any
|
|
119
|
-
) => number;
|
|
120
|
-
type InitInstanceProps = (props: Props<any>, instance: any) => any;
|
|
121
|
-
type ValidateProps = (props: Props<any>) => void;
|
|
122
|
-
|
|
123
|
-
const IS_SCROLLING_DEBOUNCE_INTERVAL = 150;
|
|
124
|
-
|
|
125
|
-
const defaultItemKey = (index: number, data: any) => index;
|
|
126
|
-
|
|
127
|
-
// In DEV mode, this Set helps us only log a warning once per component instance.
|
|
128
|
-
// This avoids spamming the console every time a render happens.
|
|
129
|
-
let devWarningsDirection = null;
|
|
130
|
-
let devWarningsTagName = null;
|
|
131
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
132
|
-
if (typeof window !== 'undefined' && typeof window.WeakSet !== 'undefined') {
|
|
133
|
-
devWarningsDirection = new WeakSet();
|
|
134
|
-
devWarningsTagName = new WeakSet();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export default function createListComponent({
|
|
139
|
-
getItemOffset,
|
|
140
|
-
getEstimatedTotalSize,
|
|
141
|
-
getItemSize,
|
|
142
|
-
getOffsetForIndexAndAlignment,
|
|
143
|
-
getStartIndexForOffset,
|
|
144
|
-
getStopIndexForStartIndex,
|
|
145
|
-
initInstanceProps,
|
|
146
|
-
shouldResetStyleCacheOnItemSizeChange,
|
|
147
|
-
validateProps,
|
|
148
|
-
}: {|
|
|
149
|
-
getItemOffset: GetItemOffset,
|
|
150
|
-
getEstimatedTotalSize: GetEstimatedTotalSize,
|
|
151
|
-
getItemSize: GetItemSize,
|
|
152
|
-
getOffsetForIndexAndAlignment: GetOffsetForIndexAndAlignment,
|
|
153
|
-
getStartIndexForOffset: GetStartIndexForOffset,
|
|
154
|
-
getStopIndexForStartIndex: GetStopIndexForStartIndex,
|
|
155
|
-
initInstanceProps: InitInstanceProps,
|
|
156
|
-
shouldResetStyleCacheOnItemSizeChange: boolean,
|
|
157
|
-
validateProps: ValidateProps,
|
|
158
|
-
|}) {
|
|
159
|
-
return class List<T> extends PureComponent<Props<T>, State> {
|
|
160
|
-
_instanceProps: any = initInstanceProps(this.props, this);
|
|
161
|
-
_outerRef: ?HTMLDivElement;
|
|
162
|
-
_resetIsScrollingTimeoutId: TimeoutID | null = null;
|
|
163
|
-
|
|
164
|
-
static defaultProps = {
|
|
165
|
-
direction: 'ltr',
|
|
166
|
-
itemData: undefined,
|
|
167
|
-
layout: 'vertical',
|
|
168
|
-
overscanCount: 2,
|
|
169
|
-
useIsScrolling: false,
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
state: State = {
|
|
173
|
-
instance: this,
|
|
174
|
-
isScrolling: false,
|
|
175
|
-
scrollDirection: 'forward',
|
|
176
|
-
scrollOffset:
|
|
177
|
-
typeof this.props.initialScrollOffset === 'number'
|
|
178
|
-
? this.props.initialScrollOffset
|
|
179
|
-
: 0,
|
|
180
|
-
scrollUpdateWasRequested: false,
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
// Always use explicit constructor for React components.
|
|
184
|
-
// It produces less code after transpilation. (#26)
|
|
185
|
-
// eslint-disable-next-line no-useless-constructor
|
|
186
|
-
constructor(props: Props<T>) {
|
|
187
|
-
super(props);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
static getDerivedStateFromProps(
|
|
191
|
-
nextProps: Props<T>,
|
|
192
|
-
prevState: State
|
|
193
|
-
): $Shape<State> | null {
|
|
194
|
-
validateSharedProps(nextProps, prevState);
|
|
195
|
-
validateProps(nextProps);
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
scrollTo(scrollOffset: number): void {
|
|
200
|
-
scrollOffset = Math.max(0, scrollOffset);
|
|
201
|
-
|
|
202
|
-
this.setState(prevState => {
|
|
203
|
-
if (prevState.scrollOffset === scrollOffset) {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
return {
|
|
207
|
-
scrollDirection:
|
|
208
|
-
prevState.scrollOffset < scrollOffset ? 'forward' : 'backward',
|
|
209
|
-
scrollOffset: scrollOffset,
|
|
210
|
-
scrollUpdateWasRequested: true,
|
|
211
|
-
};
|
|
212
|
-
}, this._resetIsScrollingDebounced);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
scrollToItem(index: number, align: ScrollToAlign = 'auto'): void {
|
|
216
|
-
const { itemCount, layout } = this.props;
|
|
217
|
-
const { scrollOffset } = this.state;
|
|
218
|
-
|
|
219
|
-
index = Math.max(0, Math.min(index, itemCount - 1));
|
|
220
|
-
|
|
221
|
-
// The scrollbar size should be considered when scrolling an item into view, to ensure it's fully visible.
|
|
222
|
-
// But we only need to account for its size when it's actually visible.
|
|
223
|
-
// This is an edge case for lists; normally they only scroll in the dominant direction.
|
|
224
|
-
let scrollbarSize = 0;
|
|
225
|
-
if (this._outerRef) {
|
|
226
|
-
const outerRef = ((this._outerRef: any): HTMLElement);
|
|
227
|
-
if (layout === 'vertical') {
|
|
228
|
-
scrollbarSize =
|
|
229
|
-
outerRef.scrollWidth > outerRef.clientWidth
|
|
230
|
-
? getScrollbarSize()
|
|
231
|
-
: 0;
|
|
232
|
-
} else {
|
|
233
|
-
scrollbarSize =
|
|
234
|
-
outerRef.scrollHeight > outerRef.clientHeight
|
|
235
|
-
? getScrollbarSize()
|
|
236
|
-
: 0;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
this.scrollTo(
|
|
241
|
-
getOffsetForIndexAndAlignment(
|
|
242
|
-
this.props,
|
|
243
|
-
index,
|
|
244
|
-
align,
|
|
245
|
-
scrollOffset,
|
|
246
|
-
this._instanceProps,
|
|
247
|
-
scrollbarSize
|
|
248
|
-
)
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
componentDidMount() {
|
|
253
|
-
const { direction, initialScrollOffset, layout } = this.props;
|
|
254
|
-
|
|
255
|
-
if (typeof initialScrollOffset === 'number' && this._outerRef != null) {
|
|
256
|
-
const outerRef = ((this._outerRef: any): HTMLElement);
|
|
257
|
-
// TODO Deprecate direction "horizontal"
|
|
258
|
-
if (direction === 'horizontal' || layout === 'horizontal') {
|
|
259
|
-
outerRef.scrollLeft = initialScrollOffset;
|
|
260
|
-
} else {
|
|
261
|
-
outerRef.scrollTop = initialScrollOffset;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
this._callPropsCallbacks();
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
componentDidUpdate() {
|
|
269
|
-
const { direction, layout } = this.props;
|
|
270
|
-
const { scrollOffset, scrollUpdateWasRequested } = this.state;
|
|
271
|
-
|
|
272
|
-
if (scrollUpdateWasRequested && this._outerRef != null) {
|
|
273
|
-
const outerRef = ((this._outerRef: any): HTMLElement);
|
|
274
|
-
|
|
275
|
-
// TODO Deprecate direction "horizontal"
|
|
276
|
-
if (direction === 'horizontal' || layout === 'horizontal') {
|
|
277
|
-
if (direction === 'rtl') {
|
|
278
|
-
// TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
|
|
279
|
-
// This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).
|
|
280
|
-
// So we need to determine which browser behavior we're dealing with, and mimic it.
|
|
281
|
-
switch (getRTLOffsetType()) {
|
|
282
|
-
case 'negative':
|
|
283
|
-
outerRef.scrollLeft = -scrollOffset;
|
|
284
|
-
break;
|
|
285
|
-
case 'positive-ascending':
|
|
286
|
-
outerRef.scrollLeft = scrollOffset;
|
|
287
|
-
break;
|
|
288
|
-
default:
|
|
289
|
-
const { clientWidth, scrollWidth } = outerRef;
|
|
290
|
-
outerRef.scrollLeft = scrollWidth - clientWidth - scrollOffset;
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
} else {
|
|
294
|
-
outerRef.scrollLeft = scrollOffset;
|
|
295
|
-
}
|
|
296
|
-
} else {
|
|
297
|
-
outerRef.scrollTop = scrollOffset;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
this._callPropsCallbacks();
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
componentWillUnmount() {
|
|
305
|
-
if (this._resetIsScrollingTimeoutId !== null) {
|
|
306
|
-
cancelTimeout(this._resetIsScrollingTimeoutId);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
render() {
|
|
311
|
-
const {
|
|
312
|
-
children,
|
|
313
|
-
className,
|
|
314
|
-
direction,
|
|
315
|
-
height,
|
|
316
|
-
innerRef,
|
|
317
|
-
innerElementType,
|
|
318
|
-
innerTagName,
|
|
319
|
-
itemCount,
|
|
320
|
-
itemData,
|
|
321
|
-
itemKey = defaultItemKey,
|
|
322
|
-
layout,
|
|
323
|
-
outerElementType,
|
|
324
|
-
outerTagName,
|
|
325
|
-
style,
|
|
326
|
-
useIsScrolling,
|
|
327
|
-
width,
|
|
328
|
-
} = this.props;
|
|
329
|
-
const { isScrolling } = this.state;
|
|
330
|
-
|
|
331
|
-
// TODO Deprecate direction "horizontal"
|
|
332
|
-
const isHorizontal =
|
|
333
|
-
direction === 'horizontal' || layout === 'horizontal';
|
|
334
|
-
|
|
335
|
-
const onScroll = isHorizontal
|
|
336
|
-
? this._onScrollHorizontal
|
|
337
|
-
: this._onScrollVertical;
|
|
338
|
-
|
|
339
|
-
const [startIndex, stopIndex] = this._getRangeToRender();
|
|
340
|
-
|
|
341
|
-
const items = [];
|
|
342
|
-
if (itemCount > 0) {
|
|
343
|
-
for (let index = startIndex; index <= stopIndex; index++) {
|
|
344
|
-
items.push(
|
|
345
|
-
createElement(children, {
|
|
346
|
-
data: itemData,
|
|
347
|
-
key: itemKey(index, itemData),
|
|
348
|
-
index,
|
|
349
|
-
isScrolling: useIsScrolling ? isScrolling : undefined,
|
|
350
|
-
style: this._getItemStyle(index),
|
|
351
|
-
})
|
|
352
|
-
);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// Read this value AFTER items have been created,
|
|
357
|
-
// So their actual sizes (if variable) are taken into consideration.
|
|
358
|
-
const estimatedTotalSize = getEstimatedTotalSize(
|
|
359
|
-
this.props,
|
|
360
|
-
this._instanceProps
|
|
361
|
-
);
|
|
362
|
-
|
|
363
|
-
return createElement(
|
|
364
|
-
outerElementType || outerTagName || 'div',
|
|
365
|
-
{
|
|
366
|
-
className,
|
|
367
|
-
onScroll,
|
|
368
|
-
ref: this._outerRefSetter,
|
|
369
|
-
style: {
|
|
370
|
-
position: 'relative',
|
|
371
|
-
height,
|
|
372
|
-
width,
|
|
373
|
-
overflow: 'auto',
|
|
374
|
-
WebkitOverflowScrolling: 'touch',
|
|
375
|
-
willChange: 'transform',
|
|
376
|
-
direction,
|
|
377
|
-
...style,
|
|
378
|
-
},
|
|
379
|
-
},
|
|
380
|
-
createElement(innerElementType || innerTagName || 'div', {
|
|
381
|
-
children: items,
|
|
382
|
-
ref: innerRef,
|
|
383
|
-
style: {
|
|
384
|
-
height: isHorizontal ? '100%' : estimatedTotalSize,
|
|
385
|
-
pointerEvents: isScrolling ? 'none' : undefined,
|
|
386
|
-
width: isHorizontal ? estimatedTotalSize : '100%',
|
|
387
|
-
},
|
|
388
|
-
})
|
|
389
|
-
);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
_callOnItemsRendered: (
|
|
393
|
-
overscanStartIndex: number,
|
|
394
|
-
overscanStopIndex: number,
|
|
395
|
-
visibleStartIndex: number,
|
|
396
|
-
visibleStopIndex: number
|
|
397
|
-
) => void;
|
|
398
|
-
_callOnItemsRendered = memoizeOne(
|
|
399
|
-
(
|
|
400
|
-
overscanStartIndex: number,
|
|
401
|
-
overscanStopIndex: number,
|
|
402
|
-
visibleStartIndex: number,
|
|
403
|
-
visibleStopIndex: number
|
|
404
|
-
) =>
|
|
405
|
-
((this.props.onItemsRendered: any): onItemsRenderedCallback)({
|
|
406
|
-
overscanStartIndex,
|
|
407
|
-
overscanStopIndex,
|
|
408
|
-
visibleStartIndex,
|
|
409
|
-
visibleStopIndex,
|
|
410
|
-
})
|
|
411
|
-
);
|
|
412
|
-
|
|
413
|
-
_callOnScroll: (
|
|
414
|
-
scrollDirection: ScrollDirection,
|
|
415
|
-
scrollOffset: number,
|
|
416
|
-
scrollUpdateWasRequested: boolean
|
|
417
|
-
) => void;
|
|
418
|
-
_callOnScroll = memoizeOne(
|
|
419
|
-
(
|
|
420
|
-
scrollDirection: ScrollDirection,
|
|
421
|
-
scrollOffset: number,
|
|
422
|
-
scrollUpdateWasRequested: boolean
|
|
423
|
-
) =>
|
|
424
|
-
((this.props.onScroll: any): onScrollCallback)({
|
|
425
|
-
scrollDirection,
|
|
426
|
-
scrollOffset,
|
|
427
|
-
scrollUpdateWasRequested,
|
|
428
|
-
})
|
|
429
|
-
);
|
|
430
|
-
|
|
431
|
-
_callPropsCallbacks() {
|
|
432
|
-
if (typeof this.props.onItemsRendered === 'function') {
|
|
433
|
-
const { itemCount } = this.props;
|
|
434
|
-
if (itemCount > 0) {
|
|
435
|
-
const [
|
|
436
|
-
overscanStartIndex,
|
|
437
|
-
overscanStopIndex,
|
|
438
|
-
visibleStartIndex,
|
|
439
|
-
visibleStopIndex,
|
|
440
|
-
] = this._getRangeToRender();
|
|
441
|
-
this._callOnItemsRendered(
|
|
442
|
-
overscanStartIndex,
|
|
443
|
-
overscanStopIndex,
|
|
444
|
-
visibleStartIndex,
|
|
445
|
-
visibleStopIndex
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
if (typeof this.props.onScroll === 'function') {
|
|
451
|
-
const {
|
|
452
|
-
scrollDirection,
|
|
453
|
-
scrollOffset,
|
|
454
|
-
scrollUpdateWasRequested,
|
|
455
|
-
} = this.state;
|
|
456
|
-
this._callOnScroll(
|
|
457
|
-
scrollDirection,
|
|
458
|
-
scrollOffset,
|
|
459
|
-
scrollUpdateWasRequested
|
|
460
|
-
);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// Lazily create and cache item styles while scrolling,
|
|
465
|
-
// So that pure component sCU will prevent re-renders.
|
|
466
|
-
// We maintain this cache, and pass a style prop rather than index,
|
|
467
|
-
// So that List can clear cached styles and force item re-render if necessary.
|
|
468
|
-
_getItemStyle: (index: number) => Object;
|
|
469
|
-
_getItemStyle = (index: number): Object => {
|
|
470
|
-
const { direction, itemSize, layout } = this.props;
|
|
471
|
-
|
|
472
|
-
const itemStyleCache = this._getItemStyleCache(
|
|
473
|
-
shouldResetStyleCacheOnItemSizeChange && itemSize,
|
|
474
|
-
shouldResetStyleCacheOnItemSizeChange && layout,
|
|
475
|
-
shouldResetStyleCacheOnItemSizeChange && direction
|
|
476
|
-
);
|
|
477
|
-
|
|
478
|
-
let style;
|
|
479
|
-
if (itemStyleCache.hasOwnProperty(index)) {
|
|
480
|
-
style = itemStyleCache[index];
|
|
481
|
-
} else {
|
|
482
|
-
const offset = getItemOffset(this.props, index, this._instanceProps);
|
|
483
|
-
const size = getItemSize(this.props, index, this._instanceProps);
|
|
484
|
-
|
|
485
|
-
// TODO Deprecate direction "horizontal"
|
|
486
|
-
const isHorizontal =
|
|
487
|
-
direction === 'horizontal' || layout === 'horizontal';
|
|
488
|
-
|
|
489
|
-
const isRtl = direction === 'rtl';
|
|
490
|
-
const offsetHorizontal = isHorizontal ? offset : 0;
|
|
491
|
-
itemStyleCache[index] = style = {
|
|
492
|
-
position: 'absolute',
|
|
493
|
-
left: isRtl ? undefined : offsetHorizontal,
|
|
494
|
-
right: isRtl ? offsetHorizontal : undefined,
|
|
495
|
-
top: !isHorizontal ? offset : 0,
|
|
496
|
-
height: !isHorizontal ? size : '100%',
|
|
497
|
-
width: isHorizontal ? size : '100%',
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
return style;
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
_getItemStyleCache: (_: any, __: any, ___: any) => ItemStyleCache;
|
|
505
|
-
_getItemStyleCache = memoizeOne((_: any, __: any, ___: any) => ({}));
|
|
506
|
-
|
|
507
|
-
_getRangeToRender(): [number, number, number, number] {
|
|
508
|
-
const { itemCount, overscanCount } = this.props;
|
|
509
|
-
const { isScrolling, scrollDirection, scrollOffset } = this.state;
|
|
510
|
-
|
|
511
|
-
if (itemCount === 0) {
|
|
512
|
-
return [0, 0, 0, 0];
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
const startIndex = getStartIndexForOffset(
|
|
516
|
-
this.props,
|
|
517
|
-
scrollOffset,
|
|
518
|
-
this._instanceProps
|
|
519
|
-
);
|
|
520
|
-
const stopIndex = getStopIndexForStartIndex(
|
|
521
|
-
this.props,
|
|
522
|
-
startIndex,
|
|
523
|
-
scrollOffset,
|
|
524
|
-
this._instanceProps
|
|
525
|
-
);
|
|
526
|
-
|
|
527
|
-
// Overscan by one item in each direction so that tab/focus works.
|
|
528
|
-
// If there isn't at least one extra item, tab loops back around.
|
|
529
|
-
const overscanBackward =
|
|
530
|
-
!isScrolling || scrollDirection === 'backward'
|
|
531
|
-
? Math.max(1, overscanCount)
|
|
532
|
-
: 1;
|
|
533
|
-
const overscanForward =
|
|
534
|
-
!isScrolling || scrollDirection === 'forward'
|
|
535
|
-
? Math.max(1, overscanCount)
|
|
536
|
-
: 1;
|
|
537
|
-
|
|
538
|
-
return [
|
|
539
|
-
Math.max(0, startIndex - overscanBackward),
|
|
540
|
-
Math.max(0, Math.min(itemCount - 1, stopIndex + overscanForward)),
|
|
541
|
-
startIndex,
|
|
542
|
-
stopIndex,
|
|
543
|
-
];
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
_onScrollHorizontal = (event: ScrollEvent): void => {
|
|
547
|
-
const { clientWidth, scrollLeft, scrollWidth } = event.currentTarget;
|
|
548
|
-
this.setState(prevState => {
|
|
549
|
-
if (prevState.scrollOffset === scrollLeft) {
|
|
550
|
-
// Scroll position may have been updated by cDM/cDU,
|
|
551
|
-
// In which case we don't need to trigger another render,
|
|
552
|
-
// And we don't want to update state.isScrolling.
|
|
553
|
-
return null;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
const { direction } = this.props;
|
|
557
|
-
|
|
558
|
-
let scrollOffset = scrollLeft;
|
|
559
|
-
if (direction === 'rtl') {
|
|
560
|
-
// TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
|
|
561
|
-
// This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).
|
|
562
|
-
// It's also easier for this component if we convert offsets to the same format as they would be in for ltr.
|
|
563
|
-
// So the simplest solution is to determine which browser behavior we're dealing with, and convert based on it.
|
|
564
|
-
switch (getRTLOffsetType()) {
|
|
565
|
-
case 'negative':
|
|
566
|
-
scrollOffset = -scrollLeft;
|
|
567
|
-
break;
|
|
568
|
-
case 'positive-descending':
|
|
569
|
-
scrollOffset = scrollWidth - clientWidth - scrollLeft;
|
|
570
|
-
break;
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
// Prevent Safari's elastic scrolling from causing visual shaking when scrolling past bounds.
|
|
575
|
-
scrollOffset = Math.max(
|
|
576
|
-
0,
|
|
577
|
-
Math.min(scrollOffset, scrollWidth - clientWidth)
|
|
578
|
-
);
|
|
579
|
-
|
|
580
|
-
return {
|
|
581
|
-
isScrolling: true,
|
|
582
|
-
scrollDirection:
|
|
583
|
-
prevState.scrollOffset < scrollOffset ? 'forward' : 'backward',
|
|
584
|
-
scrollOffset,
|
|
585
|
-
scrollUpdateWasRequested: false,
|
|
586
|
-
};
|
|
587
|
-
}, this._resetIsScrollingDebounced);
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
_onScrollVertical = (event: ScrollEvent): void => {
|
|
591
|
-
const { clientHeight, scrollHeight, scrollTop } = event.currentTarget;
|
|
592
|
-
this.setState(prevState => {
|
|
593
|
-
if (prevState.scrollOffset === scrollTop) {
|
|
594
|
-
// Scroll position may have been updated by cDM/cDU,
|
|
595
|
-
// In which case we don't need to trigger another render,
|
|
596
|
-
// And we don't want to update state.isScrolling.
|
|
597
|
-
return null;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
// Prevent Safari's elastic scrolling from causing visual shaking when scrolling past bounds.
|
|
601
|
-
const scrollOffset = Math.max(
|
|
602
|
-
0,
|
|
603
|
-
Math.min(scrollTop, scrollHeight - clientHeight)
|
|
604
|
-
);
|
|
605
|
-
|
|
606
|
-
return {
|
|
607
|
-
isScrolling: true,
|
|
608
|
-
scrollDirection:
|
|
609
|
-
prevState.scrollOffset < scrollOffset ? 'forward' : 'backward',
|
|
610
|
-
scrollOffset,
|
|
611
|
-
scrollUpdateWasRequested: false,
|
|
612
|
-
};
|
|
613
|
-
}, this._resetIsScrollingDebounced);
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
_outerRefSetter = (ref: any): void => {
|
|
617
|
-
const { outerRef } = this.props;
|
|
618
|
-
|
|
619
|
-
this._outerRef = ((ref: any): HTMLDivElement);
|
|
620
|
-
|
|
621
|
-
if (typeof outerRef === 'function') {
|
|
622
|
-
outerRef(ref);
|
|
623
|
-
} else if (
|
|
624
|
-
outerRef != null &&
|
|
625
|
-
typeof outerRef === 'object' &&
|
|
626
|
-
outerRef.hasOwnProperty('current')
|
|
627
|
-
) {
|
|
628
|
-
outerRef.current = ref;
|
|
629
|
-
}
|
|
630
|
-
};
|
|
631
|
-
|
|
632
|
-
_resetIsScrollingDebounced = () => {
|
|
633
|
-
if (this._resetIsScrollingTimeoutId !== null) {
|
|
634
|
-
cancelTimeout(this._resetIsScrollingTimeoutId);
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
this._resetIsScrollingTimeoutId = requestTimeout(
|
|
638
|
-
this._resetIsScrolling,
|
|
639
|
-
IS_SCROLLING_DEBOUNCE_INTERVAL
|
|
640
|
-
);
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
_resetIsScrolling = () => {
|
|
644
|
-
this._resetIsScrollingTimeoutId = null;
|
|
645
|
-
|
|
646
|
-
this.setState({ isScrolling: false }, () => {
|
|
647
|
-
// Clear style cache after state update has been committed.
|
|
648
|
-
// This way we don't break pure sCU for items that don't use isScrolling param.
|
|
649
|
-
this._getItemStyleCache(-1, null);
|
|
650
|
-
});
|
|
651
|
-
};
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
// NOTE: I considered further wrapping individual items with a pure ListItem component.
|
|
656
|
-
// This would avoid ever calling the render function for the same index more than once,
|
|
657
|
-
// But it would also add the overhead of a lot of components/fibers.
|
|
658
|
-
// I assume people already do this (render function returning a class component),
|
|
659
|
-
// So my doing it would just unnecessarily double the wrappers.
|
|
660
|
-
|
|
661
|
-
const validateSharedProps = (
|
|
662
|
-
{
|
|
663
|
-
children,
|
|
664
|
-
direction,
|
|
665
|
-
height,
|
|
666
|
-
layout,
|
|
667
|
-
innerTagName,
|
|
668
|
-
outerTagName,
|
|
669
|
-
width,
|
|
670
|
-
}: Props<any>,
|
|
671
|
-
{ instance }: State
|
|
672
|
-
): void => {
|
|
673
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
674
|
-
if (innerTagName != null || outerTagName != null) {
|
|
675
|
-
if (devWarningsTagName && !devWarningsTagName.has(instance)) {
|
|
676
|
-
devWarningsTagName.add(instance);
|
|
677
|
-
console.warn(
|
|
678
|
-
'The innerTagName and outerTagName props have been deprecated. ' +
|
|
679
|
-
'Please use the innerElementType and outerElementType props instead.'
|
|
680
|
-
);
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
// TODO Deprecate direction "horizontal"
|
|
685
|
-
const isHorizontal = direction === 'horizontal' || layout === 'horizontal';
|
|
686
|
-
|
|
687
|
-
switch (direction) {
|
|
688
|
-
case 'horizontal':
|
|
689
|
-
case 'vertical':
|
|
690
|
-
if (devWarningsDirection && !devWarningsDirection.has(instance)) {
|
|
691
|
-
devWarningsDirection.add(instance);
|
|
692
|
-
console.warn(
|
|
693
|
-
'The direction prop should be either "ltr" (default) or "rtl". ' +
|
|
694
|
-
'Please use the layout prop to specify "vertical" (default) or "horizontal" orientation.'
|
|
695
|
-
);
|
|
696
|
-
}
|
|
697
|
-
break;
|
|
698
|
-
case 'ltr':
|
|
699
|
-
case 'rtl':
|
|
700
|
-
// Valid values
|
|
701
|
-
break;
|
|
702
|
-
default:
|
|
703
|
-
throw Error(
|
|
704
|
-
'An invalid "direction" prop has been specified. ' +
|
|
705
|
-
'Value should be either "ltr" or "rtl". ' +
|
|
706
|
-
`"${direction}" was specified.`
|
|
707
|
-
);
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
switch (layout) {
|
|
711
|
-
case 'horizontal':
|
|
712
|
-
case 'vertical':
|
|
713
|
-
// Valid values
|
|
714
|
-
break;
|
|
715
|
-
default:
|
|
716
|
-
throw Error(
|
|
717
|
-
'An invalid "layout" prop has been specified. ' +
|
|
718
|
-
'Value should be either "horizontal" or "vertical". ' +
|
|
719
|
-
`"${layout}" was specified.`
|
|
720
|
-
);
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
if (children == null) {
|
|
724
|
-
throw Error(
|
|
725
|
-
'An invalid "children" prop has been specified. ' +
|
|
726
|
-
'Value should be a React component. ' +
|
|
727
|
-
`"${children === null ? 'null' : typeof children}" was specified.`
|
|
728
|
-
);
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
if (isHorizontal && typeof width !== 'number') {
|
|
732
|
-
throw Error(
|
|
733
|
-
'An invalid "width" prop has been specified. ' +
|
|
734
|
-
'Horizontal lists must specify a number for width. ' +
|
|
735
|
-
`"${width === null ? 'null' : typeof width}" was specified.`
|
|
736
|
-
);
|
|
737
|
-
} else if (!isHorizontal && typeof height !== 'number') {
|
|
738
|
-
throw Error(
|
|
739
|
-
'An invalid "height" prop has been specified. ' +
|
|
740
|
-
'Vertical lists must specify a number for height. ' +
|
|
741
|
-
`"${height === null ? 'null' : typeof height}" was specified.`
|
|
742
|
-
);
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
};
|