Browse Source

feat: ApiSelect增加shouldFetch控制,在api请求之前的判断是否允许请求的回调函数 (#7713)

pull/7714/head^2
xueyitt 5 days ago
committed by GitHub
parent
commit
47a853330d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 13
      packages/effects/common-ui/src/components/api-component/api-component.vue
  2. 5
      playground/src/views/examples/form/basic.vue

13
packages/effects/common-ui/src/components/api-component/api-component.vue

@ -46,6 +46,8 @@ interface Props {
alwaysLoad?: boolean;
/** 在api请求之前的回调函数 */
beforeFetch?: AnyPromiseFunction<any, any>;
/** 在api请求之前的判断是否允许请求的回调函数 */
shouldFetch?: AnyPromiseFunction<any, boolean>;
/** 在api请求之后的回调函数 */
afterFetch?: AnyPromiseFunction<any, any>;
/** 直接传入选项数据,也作为api返回空数据时的后备数据 */
@ -88,6 +90,7 @@ const props = withDefaults(defineProps<Props>(), {
alwaysLoad: false,
loadingSlot: '',
beforeFetch: undefined,
shouldFetch: undefined,
afterFetch: undefined,
modelPropName: 'modelValue',
api: undefined,
@ -159,7 +162,7 @@ const bindProps = computed(() => {
});
async function fetchApi() {
const { api, beforeFetch, afterFetch, resultField } = props;
const { api, beforeFetch, shouldFetch, afterFetch, resultField } = props;
if (!api || !isFunction(api)) {
return;
@ -178,6 +181,14 @@ async function fetchApi() {
if (beforeFetch && isFunction(beforeFetch)) {
finalParams = (await beforeFetch(cloneDeep(finalParams))) || finalParams;
}
//
if (
shouldFetch &&
isFunction(shouldFetch) &&
!(await shouldFetch(finalParams))
) {
return;
}
let res = await api(finalParams);
if (afterFetch && isFunction(afterFetch)) {
res = (await afterFetch(res)) || res;

5
playground/src/views/examples/form/basic.vue

@ -113,6 +113,10 @@ const [BaseForm, baseFormApi] = useVbenForm({
params: {
keyword: keyword.value || undefined,
},
// trueapi
shouldFetch: (params: any) => {
return !!params?.keyword;
},
showSearch: true,
};
},
@ -120,6 +124,7 @@ const [BaseForm, baseFormApi] = useVbenForm({
fieldName: 'remoteSearch',
// label
label: '远程搜索',
help: '远程查询,仅有输入时方进行查询',
renderComponentContent: () => {
return {
notFoundContent: fetching.value ? h(Spin) : undefined,

Loading…
Cancel
Save