UNPKG

wix-style-react

Version:
172 lines (147 loc) 6.39 kB
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 _class, _temp; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(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; } 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; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; // This is a copy of https://github.com/CassetteRocks/react-infinite-scroller with https://github.com/CassetteRocks/react-infinite-scroller/pull/38/files merged var InfiniteScroll = (_temp = _class = function (_Component) { _inherits(InfiniteScroll, _Component); function InfiniteScroll(props) { _classCallCheck(this, InfiniteScroll); var _this = _possibleConstructorReturn(this, (InfiniteScroll.__proto__ || Object.getPrototypeOf(InfiniteScroll)).call(this, props)); _this.detachScrollListener = function () {}; _this.scrollListener = _this.scrollListener.bind(_this); return _this; } _createClass(InfiniteScroll, [{ key: 'componentDidMount', value: function componentDidMount() { this.pageLoaded = this.props.pageStart; this.attachScrollListener(); if (this.props.initialLoad) { this.scrollListener(); } } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.attachScrollListener(); } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props, children = _props.children, hasMore = _props.hasMore, loader = _props.loader, scrollElement = _props.scrollElement; var ref = void 0; if (scrollElement) { ref = function ref() { return _this2.scrollComponent = scrollElement; }; } else { ref = function ref(node) { return _this2.scrollComponent = node; }; } return React.createElement('div', { ref: ref }, children, hasMore && (loader || this._defaultLoader)); } }, { key: 'calculateTopPosition', value: function calculateTopPosition(el) { if (!el) { return 0; } return el.offsetTop + this.calculateTopPosition(el.offsetParent); } }, { key: 'scrollListener', value: function scrollListener() { var el = this.scrollComponent; var offset = void 0; if (this.props.scrollElement) { if (this.props.isReverse) { offset = el.scrollTop; } else { offset = el.scrollHeight - el.scrollTop - el.clientHeight; } } else if (this.props.useWindow) { var scrollTop = window.pageYOffset !== undefined ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop; if (this.props.isReverse) { offset = scrollTop; } else { offset = this.calculateTopPosition(el) + el.offsetHeight - scrollTop - window.innerHeight; } } else if (this.props.isReverse) { offset = el.parentNode.scrollTop; } else { offset = el.scrollHeight - el.parentNode.scrollTop - el.parentNode.clientHeight; } if (offset < Number(this.props.threshold)) { this.detachScrollListener(); // Call loadMore after detachScrollListener to allow for non-async loadMore functions if (typeof this.props.loadMore === 'function') { this.props.loadMore(this.pageLoaded += 1); } } } }, { key: 'attachScrollListener', value: function attachScrollListener() { var _this3 = this; this.detachScrollListener(); if (!this.props.hasMore) { return; } var scrollEl = window; if (this.props.scrollElement) { scrollEl = this.scrollComponent; } else if (this.props.useWindow === false) { scrollEl = this.scrollComponent.parentNode; } scrollEl.addEventListener('scroll', this.scrollListener); scrollEl.addEventListener('resize', this.scrollListener); this.detachScrollListener = function () { scrollEl.removeEventListener('scroll', _this3.scrollListener); scrollEl.removeEventListener('resize', _this3.scrollListener); _this3.detachScrollListener = function () {}; }; } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.detachScrollListener(); } // Set a defaut loader for all your `InfiniteScroll` components }, { key: 'setDefaultLoader', value: function setDefaultLoader(loader) { this._defaultLoader = loader; } }]); return InfiniteScroll; }(Component), _class.propTypes = { hasMore: PropTypes.bool, initialLoad: PropTypes.bool, loadMore: PropTypes.func.isRequired, pageStart: PropTypes.number, threshold: PropTypes.number, useWindow: PropTypes.bool, isReverse: PropTypes.bool, scrollElement: PropTypes.object, children: PropTypes.node, loader: PropTypes.node }, _class.defaultProps = { hasMore: false, initialLoad: true, pageStart: 0, threshold: 250, useWindow: true, isReverse: false, scrollElement: null }, _temp); export { InfiniteScroll as default };