yanzhen-design-vue
Version:
75 lines (74 loc) • 2.53 kB
JavaScript
import { defineComponent, useModel, ref, useAttrs, computed, watch, toValue, openBlock, createBlock, unref, mergeProps } from "vue";
import { TreeSelect } from "ant-design-vue";
import { isEmpty, isEqual } from "lodash-es";
import { dfsFindNodeById } from "@yanzhen-libs/utils";
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "InnerTreeSelect",
props: {
"value": { default: [] },
"valueModifiers": {}
},
emits: ["update:value"],
setup(__props) {
const value = useModel(__props, "value");
const currentValue = ref([]);
const attrs = useAttrs();
const keyMaps = computed(() => {
var _a, _b;
return {
...attrs["field-names"] || {},
value: ((_a = attrs["field-names"]) == null ? void 0 : _a.value) || "id",
label: ((_b = attrs["field-names"]) == null ? void 0 : _b.label) || "label"
};
});
const treeCheckStrictly = computed(() => attrs["tree-check-strictly"] || false);
const cacheMap = /* @__PURE__ */ new Map([]);
watch(
value,
(newVal, oldVal) => {
if (isEmpty(newVal) || isEqual(newVal, oldVal) || isEqual(newVal, currentValue.value)) {
return;
}
if (toValue(treeCheckStrictly)) {
newVal == null ? void 0 : newVal.forEach((item) => {
if (cacheMap.get(item)) {
currentValue.value.push(cacheMap.get(item));
} else {
const targetNode = dfsFindNodeById(attrs["tree-data"], item);
if (newVal && targetNode) {
const nodeObj = {
label: targetNode[toValue(keyMaps).label],
value: targetNode[toValue(keyMaps).value],
disabled: void 0,
halfChecked: void 0
};
currentValue.value.push(nodeObj);
cacheMap.set(item, nodeObj);
}
}
});
}
},
{
immediate: true
}
);
watch(currentValue, (newVal, oldVal) => {
if (isEqual(newVal, oldVal)) {
return;
}
if (toValue(treeCheckStrictly)) {
value.value = newVal.map((item) => item["value"]);
}
});
return (_ctx, _cache) => {
return openBlock(), createBlock(unref(TreeSelect), mergeProps({
value: currentValue.value,
"onUpdate:value": _cache[0] || (_cache[0] = ($event) => currentValue.value = $event)
}, _ctx.$attrs), null, 16, ["value"]);
};
}
});
export {
_sfc_main as default
};