UNPKG

yanzhen-design-vue

Version:

68 lines (67 loc) 2.18 kB
import { unref } from "vue"; import { buildProps, definePropType, useProp, useOptions } from "@yanzhen-libs/utils-vue"; import { isNumeral, isEmptyArray } from "@yanzhen-libs/utils"; import { optionsProps } from "../../../select/src/hooks/select-common-props.mjs"; import "../constants/index.mjs"; import { cascaderOptionType } from "../constants/option.mjs"; function cascaderOptionsProps() { return optionsProps([...cascaderOptionType]); } function useCascaderItemProps() { return buildProps({ value: { type: definePropType([String, Number]) }, avatar: String, label: String, type: String }); } const cascaderMap = /* @__PURE__ */ new WeakMap(); function useCascaderOptions(props) { const key = useProp("options"); if (cascaderMap.has(key)) { return cascaderMap.get(key); } const options = useOptions([...cascaderOptionType]); cascaderMap.set(key, options); function handleOptionsProxy(target, mode = "city") { const childrenKey = "children"; return new Proxy(target, { get(target2, p, receiver) { const value = Reflect.get(target2, p, receiver); if (isNumeral(p)) { return new Proxy(value, { get(target3, p2, receiver2) { if (childrenKey === p2) { let children = target3[childrenKey]; switch (mode) { case "city": children = target3[childrenKey]; if (props.mode === "province") return []; if (!isEmptyArray(children)) { return handleOptionsProxy(children, "district"); } break; case "district": children = target3[childrenKey]; if (props.mode === "city") return []; break; } return children; } return Reflect.get(target3, p2, receiver2); } }); } return value; } }); } return handleOptionsProxy(unref(options.value)); } export { cascaderOptionsProps, useCascaderItemProps, useCascaderOptions };