-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfiniteList.js
278 lines (224 loc) · 11.1 KB
/
InfiniteList.js
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ramda = require("ramda");
var _InfiniteListModule = _interopRequireDefault(require("./InfiniteList.module.css"));
var _InfiniteList = _interopRequireDefault(require("./InfiniteList.proptypes"));
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 _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); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var RenderedItemComponent = function RenderedItemComponent(_ref) {
var top = _ref.top,
item = _ref.item,
addItemRef = _ref.addItemRef,
ItemComponent = _ref.ItemComponent;
return _react.default.createElement("div", {
style: {
position: 'absolute',
top: top,
width: '100%'
},
ref: addItemRef
}, _react.default.createElement("div", {
style: {
paddingBottom: '8px'
}
}, _react.default.createElement(ItemComponent, item)));
};
var InfiniteList = /*#__PURE__*/function (_Component) {
_inherits(InfiniteList, _Component);
function InfiniteList(props) {
var _this;
_classCallCheck(this, InfiniteList);
_this = _possibleConstructorReturn(this, _getPrototypeOf(InfiniteList).call(this, props));
_this.handleScroll = function (event) {
var itemList = _this.props.itemList;
var currentMax = _this.state.currentMax;
var bottom = event.target.scrollHeight - event.target.scrollTop <= event.target.clientHeight * 2 + 10;
if (bottom && currentMax < itemList.length) {
_this.handleScollToBottom(event.target.scrollTop - event.target.clientHeight);
return;
}
_this.setListIntervalToRender(event.target.scrollTop - Math.floor(event.target.clientHeight / 2), event.target.clientHeight * 2);
};
_this.setListIntervalToRender = function (scrollTop, clientHeight) {
var _this$state = _this.state,
itemPosition = _this$state.itemPosition,
currentMax = _this$state.currentMax;
var beginList = itemPosition.findIndex(function (position) {
return position >= scrollTop;
});
if (beginList > 0) beginList -= 1;
var endList = itemPosition.findIndex(function (position) {
return position >= clientHeight + scrollTop;
});
if (endList === -1) endList = currentMax;
_this.setState({
beginList: beginList,
endList: endList
});
};
_this.addItemRef = function (itemComp) {
if (itemComp) {
_this.itemRef.push(itemComp);
}
};
_this.renderList = function () {
var _this$props = _this.props,
itemList = _this$props.itemList,
ItemComponent = _this$props.ItemComponent;
var _this$state2 = _this.state,
beginList = _this$state2.beginList,
endList = _this$state2.endList,
itemPosition = _this$state2.itemPosition;
_this.itemRef = [];
var renderedItemList = itemList.slice(beginList, endList).map(function (item, index) {
return _react.default.createElement(RenderedItemComponent, {
key: item.key,
addItemRef: _this.addItemRef,
top: itemPosition[index + beginList],
ItemComponent: ItemComponent,
item: item
});
});
return renderedItemList;
};
_this.calculateItemPosition = function () {
var newItemPosition = _this.state.itemHeight.reduce(function (heightArr, currentHeight) {
var position = currentHeight + heightArr[heightArr.length - 1];
return [].concat(_toConsumableArray(heightArr), [position]);
}, [0]);
_this.setState({
itemPosition: newItemPosition
});
};
_this.addItemHeight = function () {
var addedRef = _this.itemRef[_this.itemRef.length - 1];
if (addedRef) {
var itemHeight = [].concat(_toConsumableArray(_this.state.itemHeight), [addedRef.scrollHeight]);
_this.setState({
itemHeight: itemHeight
}, _this.calculateItemPosition);
}
};
_this.loadMoreOnInitialization = function () {
var itemList = _this.props.itemList;
var _this$state3 = _this.state,
endList = _this$state3.endList,
currentMax = _this$state3.currentMax;
if (_this.container) {
var bottom = _this.container.scrollHeight <= _this.container.clientHeight * 2 + 10;
if (bottom && currentMax < itemList.length) {
_this.setState({
endList: endList + 1,
currentMax: currentMax + 1
}, _this.addItemHeight);
}
}
};
_this.changeSizeComponent = function () {
var _this$state4 = _this.state,
itemHeight = _this$state4.itemHeight,
beginList = _this$state4.beginList,
endList = _this$state4.endList;
var itemListChildren = (0, _ramda.pathOr)([], ['childNodes'])(_this.itemListRef);
var itemListChildrenHeight = _toConsumableArray(itemListChildren).map(function (child) {
return child.scrollHeight;
});
var newItemHeight = _toConsumableArray(itemHeight);
var toCompareItemHeight = newItemHeight.slice(beginList, endList);
if ((0, _ramda.equals)(toCompareItemHeight, itemListChildrenHeight)) {
return;
}
newItemHeight.splice.apply(newItemHeight, [beginList, endList - beginList].concat(_toConsumableArray(itemListChildrenHeight)));
_this.setState({
itemHeight: newItemHeight
}, _this.calculateItemPosition);
};
_this.state = {
beginList: 0,
endList: 0,
itemPosition: [0],
currentMax: 0,
itemHeight: []
};
_this.itemRef = [];
_this.itemListRef = null;
return _this;
}
_createClass(InfiniteList, [{
key: "handleScollToBottom",
value: function handleScollToBottom(scrollTop) {
var _this$state5 = this.state,
currentMax = _this$state5.currentMax,
itemPosition = _this$state5.itemPosition;
var beginList = itemPosition.findIndex(function (position) {
return position >= scrollTop;
});
if (beginList > 0) beginList -= 1;
this.setState({
beginList: beginList,
endList: currentMax + 1,
currentMax: currentMax + 1
}, this.addItemHeight);
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.loadMoreOnInitialization();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.loadMoreOnInitialization();
this.changeSizeComponent();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.container = null;
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var itemPosition = this.state.itemPosition;
return _react.default.createElement("div", {
ref: function ref(container) {
_this2.container = container;
},
onScroll: this.handleScroll,
className: "".concat(_InfiniteListModule.default.InfiniteListContainer, " ").concat(this.props.className)
}, _react.default.createElement("div", {
style: {
height: itemPosition[itemPosition.length - 1],
position: 'relative'
},
ref: function ref(itemListRef) {
_this2.itemListRef = itemListRef;
}
}, this.renderList()));
}
}]);
return InfiniteList;
}(_react.Component);
InfiniteList.propTypes = _InfiniteList.default.propTypes;
InfiniteList.defaultProps = _InfiniteList.default.defaultProps;
var _default = InfiniteList;
exports.default = _default;