fix: anti double submit in form

This commit is contained in:
jialin
2026-06-11 20:02:27 +08:00
committed by jialin
parent 1218c6d096
commit a7b33a928b
23 changed files with 246 additions and 93 deletions
@@ -1,4 +1,5 @@
import { PageActionType } from '@/config/types';
import useSubmitLock from '@/hooks/use-submit-lock';
import { FormDrawer } from '@gpustack/core-ui';
import React, { useRef } from 'react';
import { FormData, MaasProviderItem as ListItem } from '../config/types';
@@ -22,16 +23,19 @@ const AddProvider: React.FC<AddModalProps> = ({
onCancel
}) => {
const form = useRef<any>(null);
const { loading, guard, run, release } = useSubmitLock();
const handleSubmit = () => {
form.current?.submit();
guard(() => form.current?.submit());
};
const handleOnFinish = async (data: FormData) => {
console.log('handleOnFinish', data);
onOk({
...data
});
await run(() =>
onOk({
...data
})
);
};
const handleCancel = () => {
@@ -46,12 +50,14 @@ const AddProvider: React.FC<AddModalProps> = ({
onCancel={handleCancel}
onSubmit={handleSubmit}
width={600}
loading={loading}
>
<ProviderForm
ref={form}
action={action}
currentData={currentData}
onFinish={handleOnFinish}
onFinishFailed={release}
/>
</FormDrawer>
);
+8 -2
View File
@@ -31,6 +31,7 @@ interface ProviderFormProps {
action: PageActionType;
currentData?: ListItem; // Used when action is EDIT
onFinish: (values: FormData) => Promise<void>;
onFinishFailed?: (errorInfo: any) => void;
}
const TABKeysMap = {
@@ -51,7 +52,7 @@ const requiredFields = {
};
const ProviderForm: React.FC<ProviderFormProps> = forwardRef((props, ref) => {
const { action, currentData, onFinish } = props;
const { action, currentData, onFinish, onFinishFailed } = props;
const intl = useIntl();
const providerRequiredFieldsMap = useProviderRequiredFields();
const [form] = Form.useForm();
@@ -146,6 +147,11 @@ const ProviderForm: React.FC<ProviderFormProps> = forwardRef((props, ref) => {
updateActiveKey
});
const handleFinishFailed = (errorInfo: any) => {
handleOnFinishFailed(errorInfo);
onFinishFailed?.(errorInfo);
};
useImperativeHandle(ref, () => ({
submit: () => {
form.submit();
@@ -217,7 +223,7 @@ const ProviderForm: React.FC<ProviderFormProps> = forwardRef((props, ref) => {
<Form
form={form}
onFinish={handleOnFinish}
onFinishFailed={handleOnFinishFailed}
onFinishFailed={handleFinishFailed}
initialValues={{
proxy_enabled: false,
proxy_url: '',