feat: improved formApi for component instance support

* 改进表单API以支持组件实例的获取,以及焦点字段的获取
This commit is contained in:
Netfan
2025-03-10 02:24:58 +08:00
parent cfa18c2b8e
commit 04dff33ac5
7 changed files with 119 additions and 24 deletions

View File

@@ -1,8 +1,8 @@
<script lang="ts" setup>
import type { VbenFormSchema } from '@vben/common-ui';
import type { BasicOption } from '@vben/types';
import type { BasicOption, Recordable } from '@vben/types';
import { computed, markRaw } from 'vue';
import { computed, markRaw, useTemplateRef } from 'vue';
import { AuthenticationLogin, SliderCaptcha, z } from '@vben/common-ui';
import { $t } from '@vben/locales';
@@ -104,12 +104,28 @@ const formSchema = computed((): VbenFormSchema[] => {
},
];
});
const loginRef =
useTemplateRef<InstanceType<typeof AuthenticationLogin>>('loginRef');
async function onSubmit(params: Recordable<any>) {
authStore.authLogin(params).catch(() => {
// 登陆失败,刷新验证码的演示
// 使用表单API获取验证码组件实例并调用其resume方法来重置验证码
loginRef.value
?.getFormApi()
?.getFieldComponentRef<InstanceType<typeof SliderCaptcha>>('captcha')
?.resume();
});
}
</script>
<template>
<AuthenticationLogin
ref="loginRef"
:form-schema="formSchema"
:loading="authStore.loginLoading"
@submit="authStore.authLogin"
@submit="onSubmit"
/>
</template>

View File

@@ -1,4 +1,6 @@
<script lang="ts" setup>
import type { RefSelectProps } from 'ant-design-vue/es/select';
import { ref } from 'vue';
import { Page } from '@vben/common-ui';
@@ -82,6 +84,7 @@ function handleClick(
action:
| 'batchAddSchema'
| 'batchDeleteSchema'
| 'componentRef'
| 'disabled'
| 'hiddenAction'
| 'hiddenResetButton'
@@ -129,6 +132,11 @@ function handleClick(
});
break;
}
case 'componentRef': {
// 获取下拉组件的实例并调用它的focus方法
formApi.getFieldComponentRef<RefSelectProps>('fieldOptions')?.focus();
break;
}
case 'disabled': {
formApi.setState({ commonConfig: { disabled: true } });
break;
@@ -182,6 +190,7 @@ function handleClick(
formApi.setState({ submitButtonOptions: { show: true } });
break;
}
case 'updateActionAlign': {
formApi.setState({
// 可以自行调整class
@@ -189,7 +198,6 @@ function handleClick(
});
break;
}
case 'updateResetButton': {
formApi.setState({
resetButtonOptions: { disabled: true },
@@ -257,6 +265,7 @@ function handleClick(
<Button @click="handleClick('batchDeleteSchema')">
批量删除表单项
</Button>
<Button @click="handleClick('componentRef')">下拉组件获取焦点</Button>
</Space>
<Card title="操作示例">
<BaseForm />