@jdcfe/yep-react
Version:
一套移动端的React组件库
180 lines (147 loc) • 8.3 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var React = _interopRequireWildcard(require("react"));
var _throttle = _interopRequireDefault(require("lodash/throttle"));
var _noop = _interopRequireDefault(require("../_utils/noop"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var InfiniteLoader = /*#__PURE__*/function (_React$PureComponent) {
(0, _inherits2.default)(InfiniteLoader, _React$PureComponent);
var _super = _createSuper(InfiniteLoader);
function InfiniteLoader(props) {
var _this;
(0, _classCallCheck2.default)(this, InfiniteLoader);
_this = _super.call(this, props);
_this.state = {
showLoader: false,
lastScrollTop: 0,
actionTriggered: false
}; // will be populated in componentDidMount
// based on the height of the pull down element
_this.onScrollListener = _this.onScrollListener.bind((0, _assertThisInitialized2.default)(_this));
_this.throttledOnScrollListener = (0, _throttle.default)(_this.onScrollListener, 150).bind((0, _assertThisInitialized2.default)(_this));
_this.getScrollableTarget = _this.getScrollableTarget.bind((0, _assertThisInitialized2.default)(_this));
return _this;
}
(0, _createClass2.default)(InfiniteLoader, [{
key: "componentDidMount",
value: function componentDidMount() {
this._scrollableNode = this.getScrollableTarget();
this.el = this.props.height ? this._infScroll : this.props.scrollableTarget ? this._scrollableNode : window;
this.el.addEventListener('scroll', this.throttledOnScrollListener);
if (typeof this.props.initialScrollY === 'number' && this.el.scrollHeight > this.props.initialScrollY) {
//this.el.scrollTo(0, this.props.initialScrollY) scrollTo is not a function in andriod webview
if (typeof this.el.scrollTo === 'function') {
this.el.scrollTo(0, this.props.initialScrollY);
} else {
this.el.scrollTop = this.props.initialScrollY;
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.el.removeEventListener('scroll', this.throttledOnScrollListener);
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(props) {
// do nothing when dataLength is unchanged
if (this.props.dataLength === props.dataLength) return; // update state when new data was sent in
this.setState({
showLoader: false,
actionTriggered: false
});
}
}, {
key: "getScrollableTarget",
value: function getScrollableTarget() {
if (this.props.scrollableTarget instanceof HTMLElement) return this.props.scrollableTarget;
if (typeof this.props.scrollableTarget === 'string') {
return document.getElementById(this.props.scrollableTarget);
}
return this.el && this.el.parentNode;
}
}, {
key: "isElementAtBottom",
value: function isElementAtBottom(target) {
var scrollThreshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.8;
var clientHeight = target.clientHeight;
return target.scrollTop + clientHeight >= scrollThreshold * target.scrollHeight;
}
}, {
key: "onScrollListener",
value: function onScrollListener(event) {
var onScroll = this.props.onScroll;
if (onScroll && typeof onScroll === 'function') {
// Execute this callback in next tick so that it does not affect the
// functionality of the library.
setTimeout(function () {
return onScroll(event);
}, 0);
}
var target = this.props.height || this._scrollableNode ? event.target : document.documentElement.scrollTop ? document.documentElement : document.body; // return immediately if the action has already been triggered,
// prevents multiple triggers.
if (this.state.actionTriggered) return;
var atBottom = this.isElementAtBottom(target, this.props.scrollThreshold); // call the `next` function in the props to trigger the next data fetch
if (atBottom && this.props.hasMore) {
this.setState({
actionTriggered: true,
showLoader: true
});
this.props.loadMore();
}
this.setState({
lastScrollTop: target.scrollTop
});
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
className = _this$props.className,
style = _this$props.style,
hasChildren = _this$props.hasChildren,
children = _this$props.children,
height = _this$props.height,
hasMore = _this$props.hasMore,
endMessage = _this$props.endMessage,
loader = _this$props.loader;
var componentStyle = Object.assign({
height: height || 'auto',
overflow: 'auto',
WebkitOverflowScrolling: 'touch'
}, style);
var hasChild = hasChildren || !!(children && React.Children.count(children));
return /*#__PURE__*/React.createElement("div", {
className: className,
ref: function ref(infScroll) {
return _this2._infScroll = infScroll;
},
style: componentStyle
}, children, !this.state.showLoader && !hasChild && hasMore && loader, this.state.showLoader && hasMore && loader, !hasMore && endMessage);
}
}]);
return InfiniteLoader;
}(React.PureComponent);
InfiniteLoader.defaultProps = {
style: {},
className: 'Yep-infinite-loader',
onScroll: _noop.default
};
var _default = InfiniteLoader;
exports.default = _default;