Skip to content

Commit 4ba0fdc

Browse files
Updates lib; minor version bump
1 parent 30593cf commit 4ba0fdc

File tree

4 files changed

+63
-43
lines changed

4 files changed

+63
-43
lines changed

lib/VirtualList.js

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ var _getVisibleItemBounds = require('./utils/getVisibleItemBounds');
2020

2121
var _getVisibleItemBounds2 = _interopRequireDefault(_getVisibleItemBounds);
2222

23+
var _throttleWithRAF = require('./utils/throttleWithRAF');
24+
25+
var _throttleWithRAF2 = _interopRequireDefault(_throttleWithRAF);
26+
27+
var _defaultMapVirtualToProps = require('./utils/defaultMapVirtualToProps');
28+
29+
var _defaultMapVirtualToProps2 = _interopRequireDefault(_defaultMapVirtualToProps);
30+
2331
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2432

2533
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -29,6 +37,7 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
2937
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
3038

3139
var VirtualList = function VirtualList(options) {
40+
var mapVirtualToProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _defaultMapVirtualToProps2.default;
3241
return function (InnerComponent) {
3342
var _class, _temp;
3443

@@ -58,21 +67,7 @@ var VirtualList = function VirtualList(options) {
5867

5968
// if requestAnimationFrame is available, use it to throttle refreshState
6069
if (window && 'requestAnimationFrame' in window) {
61-
(function () {
62-
var refreshState = _this.refreshState;
63-
64-
_this.refreshState = function () {
65-
if (_this.isRefreshingState) return;
66-
67-
_this.isRefreshingState = true;
68-
69-
window.requestAnimationFrame(function () {
70-
refreshState();
71-
72-
_this.isRefreshingState = false;
73-
});
74-
};
75-
})();
70+
_this.refreshState = (0, _throttleWithRAF2.default)(_this.refreshState);
7671
}
7772
return _this;
7873
}
@@ -134,33 +129,7 @@ var VirtualList = function VirtualList(options) {
134129
}, {
135130
key: 'render',
136131
value: function render() {
137-
var _state = this.state,
138-
firstItemIndex = _state.firstItemIndex,
139-
lastItemIndex = _state.lastItemIndex;
140-
var _props2 = this.props,
141-
items = _props2.items,
142-
itemHeight = _props2.itemHeight;
143-
144-
145-
var visibleItems = lastItemIndex > -1 ? items.slice(firstItemIndex, lastItemIndex + 1) : [];
146-
// would be nice to make this not break shallowCompare with items.slice
147-
// but theoretically we're only rendering if we need to
148-
149-
// style
150-
var height = items.length * itemHeight;
151-
var paddingTop = firstItemIndex * itemHeight;
152-
153-
var virtual = {
154-
items: visibleItems,
155-
style: {
156-
height: height,
157-
paddingTop: paddingTop
158-
}
159-
};
160-
161-
return _react2.default.createElement(InnerComponent, _extends({}, this.props, {
162-
virtual: virtual
163-
}));
132+
return _react2.default.createElement(InnerComponent, _extends({}, this.props, mapVirtualToProps(this.props, this.state)));
164133
}
165134
}]);
166135

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
var defaultMapToVirtualProps = function defaultMapToVirtualProps(_ref, _ref2) {
7+
var items = _ref.items,
8+
itemHeight = _ref.itemHeight;
9+
var firstItemIndex = _ref2.firstItemIndex,
10+
lastItemIndex = _ref2.lastItemIndex;
11+
12+
var visibleItems = lastItemIndex > -1 ? items.slice(firstItemIndex, lastItemIndex + 1) : [];
13+
14+
// style
15+
var height = items.length * itemHeight;
16+
var paddingTop = firstItemIndex * itemHeight;
17+
18+
return {
19+
virtual: {
20+
items: visibleItems,
21+
style: {
22+
height: height,
23+
paddingTop: paddingTop
24+
}
25+
}
26+
};
27+
};
28+
29+
exports.default = defaultMapToVirtualProps;

lib/utils/throttleWithRAF.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
var _arguments = arguments;
7+
8+
exports.default = function (fn) {
9+
var running = false;
10+
11+
return function () {
12+
if (running) return;
13+
14+
running = true;
15+
16+
window.requestAnimationFrame(function () {
17+
fn.apply(undefined, _arguments);
18+
19+
running = false;
20+
});
21+
};
22+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-virtual-list",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Super simple virtualized list React higher-order component",
55
"main": "dist/VirtualList.js",
66
"directories": {

0 commit comments

Comments
 (0)