UNPKG

@orca-fe/pocket

Version:

UI components by orca-team

162 lines 9.48 kB
var _excluded = ["defaultPage"], _excluded2 = ["className", "children", "loading", "hasMore", "onLoadMore", "page", "loadOnMount", "disabled", "loadMoreText", "noMoreText", "loadingDelay", "hideNoMore", "loadingComponent"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import React, { useEffect, useImperativeHandle, useRef, useState } from 'react'; import { useControllableProps, useMemorizedFn, usePassThroughEvents, useThis } from '@orca-fe/hooks'; import cn from 'classnames'; import useStyles from "./LoadMore.style"; import { jsxs as _jsxs } from "react/jsx-runtime"; var ef = () => false; var LoadMore = /*#__PURE__*/React.forwardRef((_ref, ref) => { var _ref$defaultPage = _ref.defaultPage, defaultPage = _ref$defaultPage === void 0 ? 0 : _ref$defaultPage, props = _objectWithoutProperties(_ref, _excluded); var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), debounceLoadLock = _useState2[0], setDebounceLoadLock = _useState2[1]; var _useControllableProps = useControllableProps(props, { page: defaultPage, loading: false, hasMore: true }), _useControllableProps2 = _slicedToArray(_useControllableProps, 2), _useControllableProps3 = _useControllableProps2[0], _useControllableProps4 = _useControllableProps3.className, className = _useControllableProps4 === void 0 ? '' : _useControllableProps4, children = _useControllableProps3.children, loading = _useControllableProps3.loading, hasMore = _useControllableProps3.hasMore, _useControllableProps5 = _useControllableProps3.onLoadMore, onLoadMore = _useControllableProps5 === void 0 ? ef : _useControllableProps5, page = _useControllableProps3.page, _useControllableProps6 = _useControllableProps3.loadOnMount, loadOnMount = _useControllableProps6 === void 0 ? true : _useControllableProps6, _useControllableProps7 = _useControllableProps3.disabled, disabled = _useControllableProps7 === void 0 ? false : _useControllableProps7, _useControllableProps8 = _useControllableProps3.loadMoreText, loadMoreText = _useControllableProps8 === void 0 ? '下拉加载更多' : _useControllableProps8, _useControllableProps9 = _useControllableProps3.noMoreText, noMoreText = _useControllableProps9 === void 0 ? '没有更多了' : _useControllableProps9, _useControllableProps10 = _useControllableProps3.loadingDelay, loadingDelay = _useControllableProps10 === void 0 ? 500 : _useControllableProps10, hideNoMore = _useControllableProps3.hideNoMore, _useControllableProps11 = _useControllableProps3.loadingComponent, loadingComponent = _useControllableProps11 === void 0 ? 'Loading...' : _useControllableProps11, otherProps = _objectWithoutProperties(_useControllableProps3, _excluded2), changeProps = _useControllableProps2[1]; var styles = useStyles(); var isDebounceLoading = loading || debounceLoadLock; var _this = useThis({ loadLock: false }); var rootRef = useRef(null); var loadingRef = useRef(null); useImperativeHandle(ref, () => rootRef.current); useEffect(() => { var timer; if (!loading) { _this.loadLock = false; timer = window.setTimeout(() => { setDebounceLoadLock(false); }, loadingDelay); } return () => { if (timer) { window.clearTimeout(timer); } }; }, [loading]); var triggerLoadMore = useMemorizedFn(() => { if (!hasMore) return; if (!_this.loadLock) { _this.loadLock = true; setDebounceLoadLock(true); changeProps({ loading: true }); var res = onLoadMore(page + 1); if (res === false) { changeProps({ loading: false, hasMore: false }); } else if (res instanceof Promise) { res.then(result => { if (result === false) { changeProps({ loading: false, hasMore: false }); } else { changeProps({ loading: false, hasMore: true, page: page + 1 }); } }); } else { changeProps({ loading: false, page: page + 1 }); } } }); var checkNeedLoadMore = useMemorizedFn(() => { if (!hasMore || isDebounceLoading || disabled) return; if (rootRef.current && loadingRef.current) { var _rootRef$current = rootRef.current, clientHeight = _rootRef$current.clientHeight, scrollHeight = _rootRef$current.scrollHeight, scrollTop = _rootRef$current.scrollTop; var loadingHeight = loadingRef.current.clientHeight; var needLoadMore = scrollHeight <= scrollTop + clientHeight + loadingHeight; if (needLoadMore) { triggerLoadMore(); } } }); useEffect(() => { if (loadOnMount) { triggerLoadMore(); } }, []); var rootEvents = _objectSpread(_objectSpread({}, usePassThroughEvents(props, 'onScroll', () => { checkNeedLoadMore(); return undefined; })), usePassThroughEvents(props, 'onWheel', event => { if (event.deltaY > 0) { checkNeedLoadMore(); } return undefined; })); return /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread(_objectSpread({ ref: rootRef, className: cn(styles.root, className) }, otherProps), rootEvents), {}, { children: [children, !disabled && !(hideNoMore && !hasMore) && /*#__PURE__*/_jsxs("div", { ref: loadingRef, className: cn(styles.loadingContent, { [styles.canLoadMore]: hasMore, [styles.noMore]: !hasMore }), onClick: triggerLoadMore, children: [isDebounceLoading && loadingComponent, !isDebounceLoading && (hasMore ? loadMoreText : noMoreText)] })] })); }); export default LoadMore;