UNPKG

@slan-health/taro-vueuse

Version:

taro vue版本hooks

103 lines (102 loc) 2.74 kB
import { inject, computed } from "vue"; import { GLOBAL_OPTIONS_PROVIDE_KEY, getGlobalOptions } from "./core/config.js"; import { get } from "./core/utils/index.js"; import useRequest from "./useRequest.js"; import merge from "./core/utils/lodash/merge.js"; function usePagination(service, options = {}) { const defaultPaginationOptions = { currentKey: "current", pageSizeKey: "pageSize", totalKey: "total", totalPageKey: "totalPage" }; const injectedGlobalOptions = inject( GLOBAL_OPTIONS_PROVIDE_KEY, {} ); const { pagination, ...restOptions } = options; const { currentKey, pageSizeKey, totalKey, totalPageKey } = merge( defaultPaginationOptions, getGlobalOptions().pagination || {}, injectedGlobalOptions.pagination || {}, pagination || {} ); const finallyOptions = merge( { defaultParams: [ { [currentKey]: 1, [pageSizeKey]: 10 } ] }, restOptions ); const { data, params, run, ...rest } = useRequest( service, finallyOptions ); const paging = (paginationParams) => { const [oldPaginationParams, ...restParams] = params.value || []; const newPaginationParams = { ...oldPaginationParams, ...paginationParams }; const mergerParams = [newPaginationParams, ...restParams]; run(...mergerParams); }; const changeCurrent = (current2) => { paging({ [currentKey]: current2 }); }; const changePageSize = (pageSize2) => { paging({ [pageSizeKey]: pageSize2 }); }; const changePagination = (current2, pageSize2) => { paging({ [currentKey]: current2, [pageSizeKey]: pageSize2 }); }; const total = computed(() => get(data.value, totalKey, 0)); const current = computed({ get: () => { var _a, _b; return ( // @ts-ignore ((_b = (_a = params.value) == null ? void 0 : _a[0]) == null ? void 0 : _b[currentKey]) ?? finallyOptions.defaultParams[0][currentKey] ); }, set: (val) => { changeCurrent(val); } }); const pageSize = computed({ get: () => { var _a, _b; return ( // @ts-ignore ((_b = (_a = params.value) == null ? void 0 : _a[0]) == null ? void 0 : _b[pageSizeKey]) ?? finallyOptions.defaultParams[0][pageSizeKey] ); }, set: (val) => { changePageSize(val); } }); const totalPage = computed( () => get(data.value, totalPageKey, Math.ceil(total.value / pageSize.value)) ); return { data, params, current, pageSize, total, totalPage, run, changeCurrent, changePageSize, changePagination, ...rest }; } export { usePagination as default }; //# sourceMappingURL=usePagination.js.map