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.

200 lines
5.3 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 usePaginated(service, options) {
var paginated = options.paginated,
_a = options.defaultPageSize,
defaultPageSize = _a === void 0 ? 10 : _a,
_b = options.refreshDeps,
refreshDeps = _b === void 0 ? [] : _b,
fetchKey = options.fetchKey,
restOptions = __rest(options, ["paginated", "defaultPageSize", "refreshDeps", "fetchKey"]);
react_1.useEffect(function () {
if (fetchKey) {
console.error("useRequest pagination's fetchKey will not work!");
}
}, []);
var _c = useAsync_1["default"](service, __assign({
defaultParams: [{
current: 1,
pageSize: defaultPageSize
}]
}, restOptions)),
data = _c.data,
params = _c.params,
run = _c.run,
loading = _c.loading,
rest = __rest(_c, ["data", "params", "run", "loading"]);
var _d = params && params[0] ? params[0] : {},
_e = _d.current,
current = _e === void 0 ? 1 : _e,
_f = _d.pageSize,
pageSize = _f === void 0 ? defaultPageSize : _f,
_g = _d.sorter,
sorter = _g === void 0 ? {} : _g,
_h = _d.filters,
filters = _h === void 0 ? {} : _h; // 只改变 pagination其他参数原样传递
var runChangePaination = react_1.useCallback(function (paginationParams) {
var _a = __read(params),
oldPaginationParams = _a[0],
restParams = _a.slice(1);
run.apply(void 0, __spread([__assign(__assign({}, oldPaginationParams), paginationParams)], restParams));
}, [run, params]);
var total = (data === null || data === void 0 ? void 0 : data.total) || 0;
var totalPage = react_1.useMemo(function () {
return Math.ceil(total / pageSize);
}, [pageSize, total]);
var onChange = react_1.useCallback(function (c, p) {
var toCurrent = c <= 0 ? 1 : c;
var toPageSize = p <= 0 ? 1 : p;
var tempTotalPage = Math.ceil(total / toPageSize);
if (toCurrent > tempTotalPage) {
toCurrent = tempTotalPage;
}
runChangePaination({
current: c,
pageSize: p
});
}, [total, runChangePaination]);
var changeCurrent = react_1.useCallback(function (c) {
onChange(c, pageSize);
}, [onChange, pageSize]);
var changePageSize = react_1.useCallback(function (p) {
onChange(current, p);
}, [onChange, current]);
var changeCurrentRef = react_1.useRef(changeCurrent);
changeCurrentRef.current = changeCurrent;
/* 分页场景下,如果 refreshDeps 变化,重置分页 */
useUpdateEffect_1["default"](function () {
/* 只有自动执行的场景, refreshDeps 才有效 */
if (!options.manual) {
changeCurrentRef.current(1);
}
}, __spread(refreshDeps)); // 表格翻页 排序 筛选等
var changeTable = react_1.useCallback(function (p, f, s) {
runChangePaination({
current: p.current,
pageSize: p.pageSize || defaultPageSize,
filters: f,
sorter: s
});
}, [filters, sorter, runChangePaination]);
return __assign({
loading: loading,
data: data,
params: params,
run: run,
pagination: {
current: current,
pageSize: pageSize,
total: total,
totalPage: totalPage,
onChange: onChange,
changeCurrent: changeCurrent,
changePageSize: changePageSize
},
tableProps: {
dataSource: (data === null || data === void 0 ? void 0 : data.list) || [],
loading: loading,
onChange: changeTable,
pagination: {
current: current,
pageSize: pageSize,
total: total
}
},
sorter: sorter,
filters: filters
}, rest);
}
exports["default"] = usePaginated;