fix: anti double submit in form
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { PageActionType } from '@/config/types';
|
||||
import useSubmitLock from '@/hooks/use-submit-lock';
|
||||
import { ModalFooter } from '@gpustack/core-ui';
|
||||
import { useRef } from 'react';
|
||||
import FormDrawer from '../../../_components/form-drawer';
|
||||
@@ -23,9 +24,10 @@ const AddModal: 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 handleCancel = () => {
|
||||
@@ -34,9 +36,11 @@ const AddModal: React.FC<AddModalProps> = ({
|
||||
};
|
||||
|
||||
const onFinish = async (values: FormData) => {
|
||||
onOk({
|
||||
...values
|
||||
});
|
||||
await run(() =>
|
||||
onOk({
|
||||
...values
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -50,6 +54,7 @@ const AddModal: React.FC<AddModalProps> = ({
|
||||
<ModalFooter
|
||||
onOk={handleSubmit}
|
||||
onCancel={handleCancel}
|
||||
loading={loading}
|
||||
style={{
|
||||
padding: '16px 24px 8px',
|
||||
display: 'flex',
|
||||
@@ -63,6 +68,7 @@ const AddModal: React.FC<AddModalProps> = ({
|
||||
action={action}
|
||||
currentData={currentData}
|
||||
onFinish={onFinish}
|
||||
onFinishFailed={release}
|
||||
open={open}
|
||||
/>
|
||||
</FormDrawer>
|
||||
|
||||
@@ -12,11 +12,12 @@ interface TemplateFormProps {
|
||||
action: PageActionType;
|
||||
currentData?: ListItem | null;
|
||||
onFinish: (values: FormData) => Promise<void>;
|
||||
onFinishFailed?: (errorInfo: any) => void;
|
||||
}
|
||||
|
||||
const GPUServiceTemplateForm: React.FC<TemplateFormProps> = forwardRef(
|
||||
(props, ref) => {
|
||||
const { action, currentData, open, onFinish } = props;
|
||||
const { action, currentData, open, onFinish, onFinishFailed } = props;
|
||||
const [form] = Form.useForm<FormData>();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -58,6 +59,7 @@ const GPUServiceTemplateForm: React.FC<TemplateFormProps> = forwardRef(
|
||||
name="gpuServiceTemplateForm"
|
||||
form={form}
|
||||
onFinish={handleFinish}
|
||||
onFinishFailed={onFinishFailed}
|
||||
preserve={false}
|
||||
scrollToFirstError
|
||||
initialValues={{
|
||||
|
||||
Reference in New Issue
Block a user