UNPKG

yanzhen-design-vue

Version:

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