You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use strict";
var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var __read = this && this.__read || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
ar.push(r.value);
}
} catch (error) {
e = {
error: error
};
} finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
};
var __spread = this && this.__spread || function () {
for (var ar = [], i = 0; i < arguments.length; i++) {
ar = ar.concat(__read(arguments[i]));
}
return ar;
};
var __importDefault = this && this.__importDefault || function (mod) {
return mod && mod.__esModule ? mod : {
"default": mod
};
};
Object.defineProperty(exports, "__esModule", {
value: true
});
var react_1 = require("react");
var useAsync_1 = __importDefault(require("./useAsync"));
var useUpdateEffect_1 = __importDefault(require("./utils/useUpdateEffect"));
function useLoadMore(service, options) {
var _a = options.refreshDeps,
refreshDeps = _a === void 0 ? [] : _a,
ref = options.ref,
isNoMore = options.isNoMore,
_b = options.threshold,
threshold = _b === void 0 ? 100 : _b,
fetchKey = options.fetchKey,
restOptions = __rest(options, ["refreshDeps", "ref", "isNoMore", "threshold", "fetchKey"]);
var _c = __read(react_1.useState(false), 2),
loadingMore = _c[0],
setLoadingMore = _c[1];
react_1.useEffect(function () {
if (options.fetchKey) {
console.warn("useRequest loadMore mode don't need fetchKey!");
}
}, []);
var result = useAsync_1["default"](service, __assign(__assign({}, restOptions), {
fetchKey: function fetchKey(d) {
var _a;
return ((_a = d === null || d === void 0 ? void 0 : d.list) === null || _a === void 0 ? void 0 : _a.length) || 0;
},
onSuccess: function onSuccess() {
var params = [];
for (var _i = 0; _i < arguments.length; _i++) {
params[_i] = arguments[_i];
}
setLoadingMore(false);
if (options.onSuccess) {
options.onSuccess.apply(options, __spread(params));
}
}
}));
var data = result.data,
run = result.run,
params = result.params,
reset = result.reset,
loading = result.loading,
fetches = result.fetches;
var reload = react_1.useCallback(function () {
reset();
var _a = __read(params),
restParams = _a.slice(1);
run.apply(void 0, __spread([undefined], restParams));
}, [run, reset, params]);
var reloadRef = react_1.useRef(reload);
reloadRef.current = reload;
/* loadMore 场景下,如果 refreshDeps 变化,重置到第一页 */
useUpdateEffect_1["default"](function () {
/* 只有自动执行的场景, refreshDeps 才有效 */
if (!options.manual) {
reloadRef.current();
}
}, __spread(refreshDeps));
var dataGroup = react_1.useMemo(function () {
var listGroup = []; // 在 loadMore 时,不希望清空上一次的 data。需要把最后一个 非 loading 的请求 data放回去。
var lastNoLoadingData = data;
Object.values(fetches).forEach(function (h) {
var _a, _b;
if ((_a = h.data) === null || _a === void 0 ? void 0 : _a.list) {
listGroup = listGroup.concat((_b = h.data) === null || _b === void 0 ? void 0 : _b.list);
}
if (!h.loading) {
lastNoLoadingData = h.data;
}
});
return __assign(__assign({}, lastNoLoadingData), {
list: listGroup
});
}, [fetches, data]);
var noMore = isNoMore ? !loading && !loadingMore && isNoMore(dataGroup) : false;
var loadMore = react_1.useCallback(function () {
if (noMore) {
return;
}
setLoadingMore(true);
var _a = __read(params),
restParams = _a.slice(1);
run.apply(void 0, __spread([dataGroup], restParams));
}, [noMore, run, dataGroup, params]);
/* 上拉加载的方法 */
var scrollMethod = react_1.useCallback(function () {
if (loading || loadingMore || !ref || !ref.current) {
return;
}
if (ref.current.scrollHeight - ref.current.scrollTop <= ref.current.clientHeight + threshold) {
loadMore();
}
}, [loading, ref, loadMore]);
/* 如果有 ref则会上拉加载更多 */
react_1.useEffect(function () {
if (!ref || !ref.current) {
return function () {};
}
ref.current.addEventListener('scroll', scrollMethod);
return function () {
if (ref && ref.current) {
ref.current.removeEventListener('scroll', scrollMethod);
}
};
}, [scrollMethod]);
return __assign(__assign({}, result), {
data: dataGroup,
reload: reload,
loading: loading && dataGroup.list.length === 0,
loadMore: loadMore,
loadingMore: loadingMore,
noMore: noMore
});
}
exports["default"] = useLoadMore;