react-show-more-list
Version:
A unopinionated component to handle showing more results
156 lines (114 loc) • 3.83 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) :
typeof define === 'function' && define.amd ? define(['react'], factory) :
(global.reactShowMoreList = factory(global.react));
}(this, (function (react) { 'use strict';
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = 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);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var inherits = function (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;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var ShowMore = function (_Component) {
inherits(ShowMore, _Component);
function ShowMore(props) {
classCallCheck(this, ShowMore);
var _this = possibleConstructorReturn(this, (ShowMore.__proto__ || Object.getPrototypeOf(ShowMore)).call(this, props));
_this.getNext = function () {
return [_this.props.replace ? _this.state.current[1] + 1 : 0, _this.state.current[1] + _this.props.by];
};
_this.onMore = function () {
_this.setState(function (state) {
return {
current: _this.getNext()
};
});
};
_this.getItemsChunk = function (current) {
return function (items) {
return items.slice(current[0], current[1] + 1);
};
};
_this.isLastChunk = function (by) {
return function (chunk) {
var onEnd = _this.props.onEnd;
var isLast = chunk[1] >= _this.props.items.length - 1;
if (isLast && typeof onEnd === 'function') {
onEnd();
}
return isLast;
};
};
_this.state = {
current: [0, props.by - 1]
};
return _this;
}
createClass(ShowMore, [{
key: "render",
value: function render() {
var onMore = this.onMore;
var current = this.state.current;
var _props = this.props,
children = _props.children,
items = _props.items,
by = _props.by,
onEnd = _props.onEnd;
var chunk = this.getItemsChunk(current)(items);
var isLastPage = this.isLastChunk(by)(current);
if (typeof children === "function") {
return children({
current: chunk,
by: by,
all: items,
onMore: isLastPage ? null : onMore
});
}
return null;
}
}]);
return ShowMore;
}(react.Component);
ShowMore.defaultProps = {
by: 1,
onEnd: null,
replace: false,
items: []
};
return ShowMore;
})));