UNPKG

yanzhen-design-vue

Version:

94 lines (93 loc) 2.97 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 useCascaderOptions = require("./hooks/use-cascader-options.cjs"); require("./hooks/index.cjs"); const useCascaderConfig = require("./hooks/use-cascader-config.cjs"); const useCascaderArea = require("./hooks/use-cascader-area.cjs"); const apis = require("./constants/apis.cjs"); function useCascader(props, emit) { const _ref = vue.ref(); const modelValue = utilsVue.useModelValue(props, "value", emit); const options = useCascaderOptions.useCascaderOptions(props); const { config, propsRef } = useCascaderConfig.useCascaderConfig(props, emit); const watcher = utilsVue.createWatcher(); const area = useCascaderArea.useCascaderArea(props, emit); const handleChange = (value, selectOptions) => { var _a; const { multiple } = props; switch (true) { case multiple !== true: value = (_a = lodashEs.union([value])) == null ? void 0 : _a[0]; break; } modelValue.value = utils.isEmptyArray(value) ? void 0 : value; emit("change", value, selectOptions); console.log("selectOptions-change", selectOptions); }; const handleLoadData = (selectedOptions) => { switch (props.mode) { case apis.Cascader.AREA: case apis.Cascader.CITY: return area.handleLoadData(selectedOptions); } if (lodashEs.isFunction(props.loadData)) { return props.loadData(selectedOptions); } }; const handleVisibleChange = (visible) => { switch (props.mode) { case apis.Cascader.AREA: case apis.Cascader.CITY: case apis.Cascader.PROVINCE: return area.handleVisibleChange(visible); } emit("dropdownVisibleChange", visible); }; const optionsRef = vue.computed(() => { switch (props.mode) { case apis.Cascader.AREA: case apis.Cascader.CITY: case apis.Cascader.PROVINCE: return vue.unref(area.options); } return vue.unref(options); }); watcher.set("modelValue", { source: () => { return { mode: props.mode, modelValue: vue.unref(modelValue) }; }, async handler() { if (!props.mode) return; if (utils.isEmptyArray(modelValue.value)) return; const modeLength = apis.ModeInfo[props.mode].len; if (modelValue.value.length > modeLength) { const resetValue = modelValue.value.slice(0, modeLength); modelValue.value = utils.isEmptyArray(resetValue) ? void 0 : resetValue; } }, immediate: true, debounce: 0 }); return [ { _ref, config, propsRef, modelValue, options, optionsRef, handleChange, handleLoadData, handleVisibleChange } ]; } exports.useCascader = useCascader;