List.js
15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.RawList = RawList;
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Filler = _interopRequireDefault(require("./Filler"));
var _ScrollBar = _interopRequireDefault(require("./ScrollBar"));
var _useChildren = _interopRequireDefault(require("./hooks/useChildren"));
var _useHeights3 = _interopRequireDefault(require("./hooks/useHeights"));
var _useScrollTo = _interopRequireDefault(require("./hooks/useScrollTo"));
var _useDiffItem3 = _interopRequireDefault(require("./hooks/useDiffItem"));
var _useFrameWheel3 = _interopRequireDefault(require("./hooks/useFrameWheel"));
var _useMobileTouchMove = _interopRequireDefault(require("./hooks/useMobileTouchMove"));
var _useOriginScroll = _interopRequireDefault(require("./hooks/useOriginScroll"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var EMPTY_DATA = [];
var ScrollStyle = {
overflowY: 'auto',
overflowAnchor: 'none'
};
function RawList(props, ref) {
var _props$prefixCls = props.prefixCls,
prefixCls = _props$prefixCls === void 0 ? 'rc-virtual-list' : _props$prefixCls,
className = props.className,
height = props.height,
itemHeight = props.itemHeight,
_props$fullHeight = props.fullHeight,
fullHeight = _props$fullHeight === void 0 ? true : _props$fullHeight,
style = props.style,
data = props.data,
children = props.children,
itemKey = props.itemKey,
virtual = props.virtual,
_props$component = props.component,
Component = _props$component === void 0 ? 'div' : _props$component,
onScroll = props.onScroll,
restProps = _objectWithoutProperties(props, ["prefixCls", "className", "height", "itemHeight", "fullHeight", "style", "data", "children", "itemKey", "virtual", "component", "onScroll"]); // ================================= MISC =================================
var useVirtual = !!(virtual !== false && height && itemHeight);
var inVirtual = useVirtual && data && itemHeight * data.length > height;
var _useState = (0, React.useState)(0),
_useState2 = _slicedToArray(_useState, 2),
scrollTop = _useState2[0],
setScrollTop = _useState2[1];
var _useState3 = (0, React.useState)(false),
_useState4 = _slicedToArray(_useState3, 2),
scrollMoving = _useState4[0],
setScrollMoving = _useState4[1];
var mergedClassName = (0, _classnames.default)(prefixCls, className);
var mergedData = data || EMPTY_DATA;
var componentRef = (0, React.useRef)();
var fillerInnerRef = (0, React.useRef)();
var scrollBarRef = (0, React.useRef)(); // Hack on scrollbar to enable flash call
// =============================== Item Key ===============================
var getKey = React.useCallback(function (item) {
if (typeof itemKey === 'function') {
return itemKey(item);
}
return item === null || item === void 0 ? void 0 : item[itemKey];
}, [itemKey]);
var sharedConfig = {
getKey: getKey
}; // ================================ Scroll ================================
function syncScrollTop(newTop) {
setScrollTop(function (origin) {
var value;
if (typeof newTop === 'function') {
value = newTop(origin);
} else {
value = newTop;
}
var alignedTop = keepInRange(value);
componentRef.current.scrollTop = alignedTop;
return alignedTop;
});
} // ================================ Legacy ================================
// Put ref here since the range is generate by follow
var rangeRef = (0, React.useRef)({
start: 0,
end: mergedData.length
});
var diffItemRef = (0, React.useRef)();
var _useDiffItem = (0, _useDiffItem3.default)(mergedData, getKey),
_useDiffItem2 = _slicedToArray(_useDiffItem, 1),
diffItem = _useDiffItem2[0];
diffItemRef.current = diffItem; // ================================ Height ================================
var _useHeights = (0, _useHeights3.default)(getKey, null, null),
_useHeights2 = _slicedToArray(_useHeights, 4),
setInstanceRef = _useHeights2[0],
collectHeight = _useHeights2[1],
heights = _useHeights2[2],
heightUpdatedMark = _useHeights2[3]; // ========================== Visible Calculation =========================
var _React$useMemo = React.useMemo(function () {
if (!useVirtual) {
return {
scrollHeight: undefined,
start: 0,
end: mergedData.length - 1,
offset: undefined
};
} // Always use virtual scroll bar in avoid shaking
if (!inVirtual) {
var _fillerInnerRef$curre;
return {
scrollHeight: ((_fillerInnerRef$curre = fillerInnerRef.current) === null || _fillerInnerRef$curre === void 0 ? void 0 : _fillerInnerRef$curre.offsetHeight) || 0,
start: 0,
end: mergedData.length - 1,
offset: undefined
};
}
var itemTop = 0;
var startIndex;
var startOffset;
var endIndex;
var dataLen = mergedData.length;
for (var i = 0; i < dataLen; i += 1) {
var item = mergedData[i];
var key = getKey(item);
var cacheHeight = heights.get(key);
var currentItemBottom = itemTop + (cacheHeight === undefined ? itemHeight : cacheHeight); // Check item top in the range
if (currentItemBottom >= scrollTop && startIndex === undefined) {
startIndex = i;
startOffset = itemTop;
} // Check item bottom in the range. We will render additional one item for motion usage
if (currentItemBottom > scrollTop + height && endIndex === undefined) {
endIndex = i;
}
itemTop = currentItemBottom;
} // Fallback to normal if not match. This code should never reach
/* istanbul ignore next */
if (startIndex === undefined) {
startIndex = 0;
startOffset = 0;
}
if (endIndex === undefined) {
endIndex = mergedData.length - 1;
} // Give cache to improve scroll experience
endIndex = Math.min(endIndex + 1, mergedData.length);
return {
scrollHeight: itemTop,
start: startIndex,
end: endIndex,
offset: startOffset
};
}, [inVirtual, useVirtual, scrollTop, mergedData, heightUpdatedMark, height]),
scrollHeight = _React$useMemo.scrollHeight,
start = _React$useMemo.start,
end = _React$useMemo.end,
offset = _React$useMemo.offset;
rangeRef.current.start = start;
rangeRef.current.end = end; // =============================== In Range ===============================
var maxScrollHeight = scrollHeight - height;
var maxScrollHeightRef = (0, React.useRef)(maxScrollHeight);
maxScrollHeightRef.current = maxScrollHeight;
function keepInRange(newScrollTop) {
var newTop = Math.max(newScrollTop, 0);
if (!Number.isNaN(maxScrollHeightRef.current)) {
newTop = Math.min(newTop, maxScrollHeightRef.current);
}
return newTop;
}
var isScrollAtTop = scrollTop <= 0;
var isScrollAtBottom = scrollTop >= maxScrollHeight;
var originScroll = (0, _useOriginScroll.default)(isScrollAtTop, isScrollAtBottom); // ================================ Scroll ================================
function onScrollBar(newScrollTop) {
var newTop = newScrollTop;
syncScrollTop(newTop);
} // This code may only trigger in test case.
// But we still need a sync if some special escape
function onFallbackScroll(e) {
var newScrollTop = e.currentTarget.scrollTop;
if (newScrollTop !== scrollTop) {
syncScrollTop(newScrollTop);
} // Trigger origin onScroll
onScroll === null || onScroll === void 0 ? void 0 : onScroll(e);
} // Since this added in global,should use ref to keep update
var _useFrameWheel = (0, _useFrameWheel3.default)(useVirtual, isScrollAtTop, isScrollAtBottom, function (offsetY) {
syncScrollTop(function (top) {
var newTop = top + offsetY;
return newTop;
});
}),
_useFrameWheel2 = _slicedToArray(_useFrameWheel, 2),
onRawWheel = _useFrameWheel2[0],
onFireFoxScroll = _useFrameWheel2[1]; // Mobile touch move
(0, _useMobileTouchMove.default)(useVirtual, componentRef, function (deltaY, smoothOffset) {
if (originScroll(deltaY, smoothOffset)) {
return false;
}
onRawWheel({
preventDefault: function preventDefault() {},
deltaY: deltaY
});
return true;
});
React.useLayoutEffect(function () {
// Firefox only
function onMozMousePixelScroll(e) {
if (useVirtual) {
e.preventDefault();
}
}
componentRef.current.addEventListener('wheel', onRawWheel);
componentRef.current.addEventListener('DOMMouseScroll', onFireFoxScroll);
componentRef.current.addEventListener('MozMousePixelScroll', onMozMousePixelScroll);
return function () {
componentRef.current.removeEventListener('wheel', onRawWheel);
componentRef.current.removeEventListener('DOMMouseScroll', onFireFoxScroll);
componentRef.current.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll);
};
}, [useVirtual]); // ================================= Ref ==================================
var scrollTo = (0, _useScrollTo.default)(componentRef, mergedData, heights, itemHeight, getKey, collectHeight, syncScrollTop, function () {
var _scrollBarRef$current;
(_scrollBarRef$current = scrollBarRef.current) === null || _scrollBarRef$current === void 0 ? void 0 : _scrollBarRef$current.delayHidden();
});
React.useImperativeHandle(ref, function () {
return {
scrollTo: scrollTo
};
}); // ================================ Render ================================
var listChildren = (0, _useChildren.default)(mergedData, start, end, setInstanceRef, children, sharedConfig);
var componentStyle = null;
if (height) {
componentStyle = _objectSpread(_defineProperty({}, fullHeight ? 'height' : 'maxHeight', height), ScrollStyle);
if (useVirtual) {
componentStyle.overflowY = 'hidden';
if (scrollMoving) {
componentStyle.pointerEvents = 'none';
}
}
}
return /*#__PURE__*/React.createElement("div", Object.assign({
style: _objectSpread(_objectSpread({}, style), {}, {
position: 'relative'
}),
className: mergedClassName
}, restProps), /*#__PURE__*/React.createElement(Component, {
className: "".concat(prefixCls, "-holder"),
style: componentStyle,
ref: componentRef,
onScroll: onFallbackScroll
}, /*#__PURE__*/React.createElement(_Filler.default, {
prefixCls: prefixCls,
height: scrollHeight,
offset: offset,
onInnerResize: collectHeight,
ref: fillerInnerRef
}, listChildren)), useVirtual && /*#__PURE__*/React.createElement(_ScrollBar.default, {
ref: scrollBarRef,
prefixCls: prefixCls,
scrollTop: scrollTop,
height: height,
scrollHeight: scrollHeight,
count: mergedData.length,
onScroll: onScrollBar,
onStartMove: function onStartMove() {
setScrollMoving(true);
},
onStopMove: function onStopMove() {
setScrollMoving(false);
}
}));
}
var List = /*#__PURE__*/React.forwardRef(RawList);
List.displayName = 'List';
var _default = List;
exports.default = _default;