|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
<template> |
|
|
|
|
<a-tree-select v-bind="getAttrs" @change="handleChange"> |
|
|
|
|
<a-tree-select v-bind="getAttrs" @change="handleChange" @dropdown-visible-change="handleFetch"> |
|
|
|
|
<template v-for="item in Object.keys($slots)" #[item]="data"> |
|
|
|
|
<slot :name="item" v-bind="data || {}"/> |
|
|
|
|
</template> |
|
|
|
@ -10,7 +10,7 @@
@@ -10,7 +10,7 @@
|
|
|
|
|
</template> |
|
|
|
|
|
|
|
|
|
<script lang="ts"> |
|
|
|
|
import { computed, defineComponent, watch, ref, onMounted, unref } from 'vue'; |
|
|
|
|
import { computed, defineComponent, watch, ref, unref, watchEffect } from 'vue'; |
|
|
|
|
import { TreeSelect } from 'ant-design-vue'; |
|
|
|
|
import { isArray, isFunction } from '/@/utils/is'; |
|
|
|
|
import { get } from 'lodash-es'; |
|
|
|
@ -27,13 +27,14 @@
@@ -27,13 +27,14 @@
|
|
|
|
|
immediate: { type: Boolean, default: true }, |
|
|
|
|
resultField: propTypes.string.def(''), |
|
|
|
|
listToTree: { type: Boolean, default: false }, |
|
|
|
|
treeConfig: { type: Object as PropType<TreeHelperConfig>,} |
|
|
|
|
treeConfig: { type: Object as PropType<TreeHelperConfig> }, |
|
|
|
|
alwaysLoad: propTypes.bool.def(false), |
|
|
|
|
}, |
|
|
|
|
emits: ['options-change', 'change'], |
|
|
|
|
setup(props, { attrs, emit }) { |
|
|
|
|
const treeData = ref<Recordable[]>([]); |
|
|
|
|
const isFirstLoaded = ref<Boolean>(false); |
|
|
|
|
const loading = ref(false); |
|
|
|
|
const isFirstLoad = ref(true); |
|
|
|
|
const getAttrs = computed(() => { |
|
|
|
|
return { |
|
|
|
|
...(props.api ? { treeData: unref(treeData) } : {}), |
|
|
|
@ -41,29 +42,18 @@
@@ -41,29 +42,18 @@
|
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
function handleChange(...args) { |
|
|
|
|
emit('change', ...args); |
|
|
|
|
} |
|
|
|
|
watchEffect(() => { |
|
|
|
|
props.immediate && !props.alwaysLoad && fetch(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
watch( |
|
|
|
|
() => props.params, |
|
|
|
|
() => { |
|
|
|
|
!unref(isFirstLoaded) && fetch(); |
|
|
|
|
!unref(isFirstLoad) && fetch(); |
|
|
|
|
}, |
|
|
|
|
{ deep: true }, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
watch( |
|
|
|
|
() => props.immediate, |
|
|
|
|
(v) => { |
|
|
|
|
v && !isFirstLoaded.value && fetch(); |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
|
props.immediate && fetch(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
async function fetch() { |
|
|
|
|
const { api } = props; |
|
|
|
|
if (!api || !isFunction(api)) return; |
|
|
|
@ -77,15 +67,28 @@
@@ -77,15 +67,28 @@
|
|
|
|
|
} |
|
|
|
|
loading.value = false; |
|
|
|
|
if (!result) return; |
|
|
|
|
if (!isArray(result)) { |
|
|
|
|
result = get(result, props.resultField); |
|
|
|
|
if (!isArray(result)) result = get(result, props.resultField); |
|
|
|
|
props.listToTree && (result = listToTree(result, props.treeConfig)); |
|
|
|
|
} |
|
|
|
|
treeData.value = (result as Recordable[]) || []; |
|
|
|
|
isFirstLoaded.value = true; |
|
|
|
|
emit('options-change', treeData.value); |
|
|
|
|
} |
|
|
|
|
return { getAttrs, loading, handleChange, fetch }; |
|
|
|
|
|
|
|
|
|
async function handleFetch(visible: boolean) { |
|
|
|
|
if (visible) { |
|
|
|
|
if (props.alwaysLoad) { |
|
|
|
|
await fetch(); |
|
|
|
|
} else if (!props.immediate && unref(isFirstLoad)) { |
|
|
|
|
await fetch(); |
|
|
|
|
isFirstLoad.value = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function handleChange(...args) { |
|
|
|
|
emit('change', ...args); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { getAttrs, loading, handleChange, handleFetch, fetch }; |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|