|
|
|
@ -69,6 +69,14 @@ |
|
|
|
displayRenderArray: { |
|
|
|
type: Array, |
|
|
|
}, |
|
|
|
beforeFetch: { |
|
|
|
type: Function as PropType<Fn>, |
|
|
|
default: null, |
|
|
|
}, |
|
|
|
afterFetch: { |
|
|
|
type: Function as PropType<Fn>, |
|
|
|
default: null, |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
const emit = defineEmits(['change', 'defaultChange']); |
|
|
|
@ -112,19 +120,25 @@ |
|
|
|
}, [] as Option[]); |
|
|
|
} |
|
|
|
|
|
|
|
async function initialFetch() { |
|
|
|
const api = props.api; |
|
|
|
async function fetch() { |
|
|
|
let { api, beforeFetch, initFetchParams, afterFetch, resultField } = props; |
|
|
|
if (!api || !isFunction(api)) return; |
|
|
|
apiData.value = []; |
|
|
|
loading.value = true; |
|
|
|
try { |
|
|
|
const res = await api(props.initFetchParams); |
|
|
|
if (beforeFetch && isFunction(beforeFetch)) { |
|
|
|
initFetchParams = (await beforeFetch(initFetchParams)) || initFetchParams; |
|
|
|
} |
|
|
|
let res = await api(initFetchParams); |
|
|
|
if (afterFetch && isFunction(afterFetch)) { |
|
|
|
res = (await afterFetch(res)) || res; |
|
|
|
} |
|
|
|
if (Array.isArray(res)) { |
|
|
|
apiData.value = res; |
|
|
|
return; |
|
|
|
} |
|
|
|
if (props.resultField) { |
|
|
|
apiData.value = get(res, props.resultField) || []; |
|
|
|
if (resultField) { |
|
|
|
apiData.value = get(res, resultField) || []; |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.warn(error); |
|
|
|
@ -136,20 +150,26 @@ |
|
|
|
const loadData: CascaderProps['loadData'] = async (selectedOptions) => { |
|
|
|
const targetOption = selectedOptions[selectedOptions.length - 1]; |
|
|
|
targetOption.loading = true; |
|
|
|
|
|
|
|
const api = props.api; |
|
|
|
let { api, beforeFetch, afterFetch, resultField, apiParamKey } = props; |
|
|
|
if (!api || !isFunction(api)) return; |
|
|
|
try { |
|
|
|
const res = await api({ |
|
|
|
[props.apiParamKey]: Reflect.get(targetOption, 'value'), |
|
|
|
}); |
|
|
|
let param = { |
|
|
|
[apiParamKey]: Reflect.get(targetOption, 'value'), |
|
|
|
}; |
|
|
|
if (beforeFetch && isFunction(beforeFetch)) { |
|
|
|
param = (await beforeFetch(param)) || param; |
|
|
|
} |
|
|
|
let res = await api(param); |
|
|
|
if (afterFetch && isFunction(afterFetch)) { |
|
|
|
res = (await afterFetch(res)) || res; |
|
|
|
} |
|
|
|
if (Array.isArray(res)) { |
|
|
|
const children = generatorOptions(res); |
|
|
|
targetOption.children = children; |
|
|
|
return; |
|
|
|
} |
|
|
|
if (props.resultField) { |
|
|
|
const children = generatorOptions(get(res, props.resultField) || []); |
|
|
|
if (resultField) { |
|
|
|
const children = generatorOptions(get(res, resultField) || []); |
|
|
|
targetOption.children = children; |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
@ -162,7 +182,7 @@ |
|
|
|
watch( |
|
|
|
() => props.immediate, |
|
|
|
() => { |
|
|
|
props.immediate && initialFetch(); |
|
|
|
props.immediate && fetch(); |
|
|
|
}, |
|
|
|
{ |
|
|
|
immediate: true, |
|
|
|
@ -172,7 +192,7 @@ |
|
|
|
watch( |
|
|
|
() => props.initFetchParams, |
|
|
|
() => { |
|
|
|
!unref(isFirstLoad) && initialFetch(); |
|
|
|
!unref(isFirstLoad) && fetch(); |
|
|
|
}, |
|
|
|
{ deep: true }, |
|
|
|
); |
|
|
|
|