UNPKG

yanzhen-design-vue

Version:

137 lines (136 loc) 5.02 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const lodashEs = require("lodash-es"); const utils = require("@yanzhen-libs/utils"); const utilsVue = require("@yanzhen-libs/utils-vue"); require("../constants/index.cjs"); const useCascaderAreaApis = require("./use-cascader-area-apis.cjs"); const useCascaderOptions = require("./use-cascader-options.cjs"); const option = require("../constants/option.cjs"); const provinceData = vue.ref([]); const cityData = vue.reactive({}); const districtData = vue.reactive({}); function useCascaderArea(props, emit) { const modelValue = utilsVue.useModelValue(props, null, emit); const { getAreaList } = useCascaderAreaApis.provideCascaderAreaApi(props); const cascaderOptions = useCascaderOptions.useCascaderOptions(props); const optionKey = utilsVue.useOptionKey([...option.cascaderOptionType]); function handleOptionsProxy(target, mode = "city") { const { value: valueKey, children: childrenKey } = vue.unref(optionKey); const map = utilsVue.option.toOptionsMap(vue.unref(cascaderOptions)); return new Proxy(target, { get(target2, p, receiver) { const value = Reflect.get(target2, p, receiver); if (utils.isNumeral(p)) { return new Proxy(value, { get(target3, p2, receiver2) { var _a; const pid = target3[valueKey]; if (childrenKey === p2) { let children; switch (mode) { case "city": children = cityData[pid]; if (!utils.isEmptyArray(children)) { return handleOptionsProxy(children, "district"); } break; case "district": children = districtData[pid]; break; } if (utils.isEmptyArray(children) && !utils.isEmptyValue(map == null ? void 0 : map[pid])) { switch (props.mode) { case "province": { return []; } case "city": { if (mode === "district") return []; break; } default: { return ((_a = map == null ? void 0 : map[pid]) == null ? void 0 : _a[childrenKey]) ?? children; } } } return children; } return Reflect.get(target3, p2, receiver2); } }); } return value; } }); } const options = vue.computed(() => { const optionsData = vue.unref(provinceData); if (utils.isEmptyArray(optionsData)) { return vue.unref(cascaderOptions); } return handleOptionsProxy(optionsData); }); function handleAreaTransform([id, ...ids], children = vue.unref(options)) { const { value: valueKey, children: childrenKey } = vue.unref(optionKey); if (utils.isEmptyArray(children)) return []; const find = children.find((item) => id === item[valueKey]); const selectedOptions = !utils.isEmptyValue(find) ? [find] : []; const chids = find == null ? void 0 : find[childrenKey]; if (find && !utils.isEmptyArray(chids)) { const items = handleAreaTransform(ids, chids); return selectedOptions.concat(items); } return selectedOptions; } const handleLoadData = async (selectedOptions) => { if (lodashEs.isFunction(props.loadData)) { return props.loadData(selectedOptions); } const option2 = selectedOptions[selectedOptions.length - 1]; const level = selectedOptions.length; const pid = String((option2 == null ? void 0 : option2.value) ?? -1); const result = await getAreaList({ data: { mode: props.mode, pid: option2 == null ? void 0 : option2.value, values: selectedOptions } }); if ((result == null ? void 0 : result.code) === 0) { const list = !utils.isEmptyArray(result.data) ? result.data : []; switch (level) { case 2: districtData[pid] = list; break; case 1: cityData[pid] = list; break; default: provinceData.value = list; } } }; const handleVisibleChange = async (visible) => { if (utils.isEmptyArray(vue.unref(provinceData)) && visible === true) { const value = vue.unref(modelValue); const selectedOptions = handleAreaTransform(value ?? []); if (selectedOptions.length > 0) { while (selectedOptions.length > 0) { selectedOptions.pop(); handleLoadData(selectedOptions); } } else { handleLoadData([]); } } emit("dropdownVisibleChange", visible); }; return { options, handleLoadData, handleVisibleChange, handleAreaTransform }; } exports.useCascaderArea = useCascaderArea;