Files
admin-vben5/packages/effects/common-ui/src/components/api-component/api-component.vue

272 lines
7.0 KiB
Vue
Raw Normal View History

2024-12-04 22:56:29 +08:00
<script lang="ts" setup>
import type { Component } from 'vue';
2024-12-04 22:56:29 +08:00
import type { AnyPromiseFunction } from '@vben/types';
import { computed, ref, unref, useAttrs, watch } from 'vue';
2024-12-04 22:56:29 +08:00
import { LoaderCircle } from '@vben/icons';
2024-12-04 22:56:29 +08:00
import { get, isEqual, isFunction } from '@vben-core/shared/utils';
import { objectOmit } from '@vueuse/core';
type OptionsItem = {
[name: string]: any;
children?: OptionsItem[];
2024-12-04 22:56:29 +08:00
disabled?: boolean;
label?: string;
value?: string;
};
interface Props {
/** 组件 */
component: Component;
/** 是否将value从数字转为string */
2024-12-04 22:56:29 +08:00
numberToString?: boolean;
/** 获取options数据的函数 */
2024-12-04 22:56:29 +08:00
api?: (arg?: any) => Promise<OptionsItem[] | Record<string, any>>;
/** 传递给api的参数 */
2024-12-04 22:56:29 +08:00
params?: Record<string, any>;
/** 从api返回的结果中提取options数组的字段名 */
2024-12-04 22:56:29 +08:00
resultField?: string;
/** label字段名 */
2024-12-04 22:56:29 +08:00
labelField?: string;
/** children字段名需要层级数据的组件可用 */
childrenField?: string;
/** value字段名 */
2024-12-04 22:56:29 +08:00
valueField?: string;
/** 组件接收options数据的属性名 */
optionsPropName?: string;
/** 是否立即调用api */
2024-12-04 22:56:29 +08:00
immediate?: boolean;
/** 每次`visibleEvent`事件发生时都重新请求数据 */
2024-12-04 22:56:29 +08:00
alwaysLoad?: boolean;
/** 在api请求之前的回调函数 */
2024-12-04 22:56:29 +08:00
beforeFetch?: AnyPromiseFunction<any, any>;
/** 在api请求之后的回调函数 */
2024-12-04 22:56:29 +08:00
afterFetch?: AnyPromiseFunction<any, any>;
/** 直接传入选项数据也作为api返回空数据时的后备数据 */
2024-12-04 22:56:29 +08:00
options?: OptionsItem[];
/** 组件的插槽名称,用来显示一个"加载中"的图标 */
2024-12-04 22:56:29 +08:00
loadingSlot?: string;
/** 触发api请求的事件名 */
2024-12-04 22:56:29 +08:00
visibleEvent?: string;
/** 组件的v-model属性名默认为modelValue。部分组件可能为value */
modelPropName?: string;
/**
* 自动选择
* - `first`自动选择第一个选项
* - `last`自动选择最后一个选项
* - `one`: 当请求的结果只有一个选项时自动选择该选项
* - 函数自定义选择逻辑函数的参数为请求的结果数组返回值为选择的选项
* - false不自动选择(默认)
*/
autoSelect?:
| 'first'
| 'last'
| 'one'
| ((item: OptionsItem[]) => OptionsItem)
| false;
2024-12-04 22:56:29 +08:00
}
defineOptions({ name: 'ApiComponent', inheritAttrs: false });
2024-12-04 22:56:29 +08:00
const props = withDefaults(defineProps<Props>(), {
labelField: 'label',
valueField: 'value',
childrenField: '',
optionsPropName: 'options',
2024-12-04 22:56:29 +08:00
resultField: '',
visibleEvent: '',
numberToString: false,
params: () => ({}),
immediate: true,
alwaysLoad: false,
loadingSlot: '',
beforeFetch: undefined,
afterFetch: undefined,
modelPropName: 'modelValue',
2024-12-04 22:56:29 +08:00
api: undefined,
autoSelect: false,
2024-12-04 22:56:29 +08:00
options: () => [],
});
const emit = defineEmits<{
optionsChange: [OptionsItem[]];
}>();
const modelValue = defineModel<any>({ default: undefined });
2024-12-04 22:56:29 +08:00
const attrs = useAttrs();
const innerParams = ref({});
2024-12-04 22:56:29 +08:00
const refOptions = ref<OptionsItem[]>([]);
const loading = ref(false);
// 首次是否加载过了
const isFirstLoaded = ref(false);
const getOptions = computed(() => {
const { labelField, valueField, childrenField, numberToString } = props;
2024-12-04 22:56:29 +08:00
const refOptionsData = unref(refOptions);
function transformData(data: OptionsItem[]): OptionsItem[] {
return data.map((item) => {
const value = get(item, valueField);
return {
...objectOmit(item, [labelField, valueField, childrenField]),
label: get(item, labelField),
2024-12-04 22:56:29 +08:00
value: numberToString ? `${value}` : value,
...(childrenField && item[childrenField]
? { children: transformData(item[childrenField]) }
: {}),
};
});
2024-12-04 22:56:29 +08:00
}
const data: OptionsItem[] = transformData(refOptionsData);
2024-12-04 22:56:29 +08:00
return data.length > 0 ? data : props.options;
});
const bindProps = computed(() => {
return {
[props.modelPropName]: unref(modelValue),
[props.optionsPropName]: unref(getOptions),
[`onUpdate:${props.modelPropName}`]: (val: string) => {
2024-12-04 22:56:29 +08:00
modelValue.value = val;
},
...objectOmit(attrs, [`onUpdate:${props.modelPropName}`]),
2024-12-04 22:56:29 +08:00
...(props.visibleEvent
? {
[props.visibleEvent]: handleFetchForVisible,
}
: {}),
};
});
async function fetchApi() {
let { api, beforeFetch, afterFetch, params, resultField } = props;
if (!api || !isFunction(api) || loading.value) {
return;
}
refOptions.value = [];
try {
loading.value = true;
if (beforeFetch && isFunction(beforeFetch)) {
params = (await beforeFetch(params)) || params;
}
let res = await api(params);
if (afterFetch && isFunction(afterFetch)) {
res = (await afterFetch(res)) || res;
}
isFirstLoaded.value = true;
if (Array.isArray(res)) {
refOptions.value = res;
emitChange();
return;
}
if (resultField) {
refOptions.value = get(res, resultField) || [];
}
emitChange();
} catch (error) {
console.warn(error);
// reset status
isFirstLoaded.value = false;
} finally {
loading.value = false;
}
}
async function handleFetchForVisible(visible: boolean) {
if (visible) {
if (props.alwaysLoad) {
await fetchApi();
} else if (!props.immediate && !unref(isFirstLoaded)) {
await fetchApi();
}
}
}
const params = computed(() => {
return {
...props.params,
...unref(innerParams),
};
});
2024-12-04 22:56:29 +08:00
watch(
params,
2024-12-04 22:56:29 +08:00
(value, oldValue) => {
if (isEqual(value, oldValue)) {
return;
}
fetchApi();
},
{ deep: true, immediate: props.immediate },
);
function emitChange() {
if (
modelValue.value === undefined &&
props.autoSelect &&
unref(getOptions).length > 0
) {
let firstOption;
if (isFunction(props.autoSelect)) {
firstOption = props.autoSelect(unref(getOptions));
} else {
switch (props.autoSelect) {
case 'first': {
firstOption = unref(getOptions)[0];
break;
}
case 'last': {
firstOption = unref(getOptions)[unref(getOptions).length - 1];
break;
}
case 'one': {
if (unref(getOptions).length === 1) {
firstOption = unref(getOptions)[0];
}
break;
}
}
}
if (firstOption) modelValue.value = firstOption.value;
}
2024-12-04 22:56:29 +08:00
emit('optionsChange', unref(getOptions));
}
const componentRef = ref();
defineExpose({
/** 获取options数据 */
getOptions: () => unref(getOptions),
/** 获取当前值 */
getValue: () => unref(modelValue),
/** 获取被包装的组件实例 */
getComponentRef: <T = any,>() => componentRef.value as T,
/** 更新Api参数 */
updateParam(newParams: Record<string, any>) {
innerParams.value = newParams;
},
});
2024-12-04 22:56:29 +08:00
</script>
<template>
<component
:is="component"
v-bind="bindProps"
:placeholder="$attrs.placeholder"
ref="componentRef"
>
<template v-for="item in Object.keys($slots)" #[item]="data">
<slot :name="item" v-bind="data || {}"></slot>
</template>
<template v-if="loadingSlot && loading" #[loadingSlot]>
<LoaderCircle class="animate-spin" />
</template>
</component>
2024-12-04 22:56:29 +08:00
</template>