@orca-fe/pocket
Version:
UI components by orca-team
173 lines (172 loc) • 11.2 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _hooks = require("@orca-fe/hooks");
var _classnames = _interopRequireDefault(require("classnames"));
var _LoadMore = _interopRequireDefault(require("./LoadMore.style"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["defaultPage"],
_excluded2 = ["className", "children", "loading", "hasMore", "onLoadMore", "page", "loadOnMount", "disabled", "loadMoreText", "noMoreText", "loadingDelay", "hideNoMore", "loadingComponent"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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; }
var ef = function ef() {
return false;
};
var LoadMore = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
var _ref$defaultPage = _ref.defaultPage,
defaultPage = _ref$defaultPage === void 0 ? 0 : _ref$defaultPage,
props = _objectWithoutProperties(_ref, _excluded);
var _useState = (0, _react.useState)(false),
_useState2 = _slicedToArray(_useState, 2),
debounceLoadLock = _useState2[0],
setDebounceLoadLock = _useState2[1];
var _useControllableProps = (0, _hooks.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 = (0, _LoadMore.default)();
var isDebounceLoading = loading || debounceLoadLock;
var _this = (0, _hooks.useThis)({
loadLock: false
});
var rootRef = (0, _react.useRef)(null);
var loadingRef = (0, _react.useRef)(null);
(0, _react.useImperativeHandle)(ref, function () {
return rootRef.current;
});
(0, _react.useEffect)(function () {
var timer;
if (!loading) {
_this.loadLock = false;
timer = window.setTimeout(function () {
setDebounceLoadLock(false);
}, loadingDelay);
}
return function () {
if (timer) {
window.clearTimeout(timer);
}
};
}, [loading]);
var triggerLoadMore = (0, _hooks.useMemorizedFn)(function () {
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(function (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 = (0, _hooks.useMemorizedFn)(function () {
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();
}
}
});
(0, _react.useEffect)(function () {
if (loadOnMount) {
triggerLoadMore();
}
}, []);
var rootEvents = _objectSpread(_objectSpread({}, (0, _hooks.usePassThroughEvents)(props, 'onScroll', function () {
checkNeedLoadMore();
return undefined;
})), (0, _hooks.usePassThroughEvents)(props, 'onWheel', function (event) {
if (event.deltaY > 0) {
checkNeedLoadMore();
}
return undefined;
}));
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread(_objectSpread({
ref: rootRef,
className: (0, _classnames.default)(styles.root, className)
}, otherProps), rootEvents), {}, {
children: [children, !disabled && !(hideNoMore && !hasMore) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
ref: loadingRef,
className: (0, _classnames.default)(styles.loadingContent, _defineProperty(_defineProperty({}, styles.canLoadMore, hasMore), styles.noMore, !hasMore)),
onClick: triggerLoadMore,
children: [isDebounceLoading && loadingComponent, !isDebounceLoading && (hasMore ? loadMoreText : noMoreText)]
})]
}));
});
var _default = exports.default = LoadMore;