Skip to content

Commit f50b93a

Browse files
authored
fix(Form): set isSubmitting before start of the validation (#203)
1 parent 8e6767a commit f50b93a

File tree

3 files changed

+70
-25
lines changed

3 files changed

+70
-25
lines changed

.changeset/cuddly-horses-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
On form submission the `isSubmitting` flag now set to true before the start of the validation.

src/components/forms/Form/ComplexForm.stories.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,55 @@ import {
1919
import { NumberInput } from '../NumberInput/NumberInput';
2020
import { baseProps } from '../../../stories/lists/baseProps';
2121
import { Button } from '../../actions';
22+
import { timeout } from '../../../utils/promise';
2223

2324
export default {
2425
title: 'Forms/ComplexForm',
2526
component: Form,
2627
parameters: { controls: { exclude: baseProps } },
2728
};
2829

30+
const AsyncValidationTemplate: StoryFn<typeof Form> = (args) => {
31+
const [form] = Form.useForm();
32+
33+
return (
34+
<Form
35+
form={form}
36+
{...args}
37+
onSubmit={(v) => {
38+
console.log('onSubmit:', v);
39+
}}
40+
onValuesChange={(v) => {
41+
console.log('onChange', v);
42+
}}
43+
>
44+
<Field
45+
name="text"
46+
label="Text input"
47+
validateTrigger="onSubmit"
48+
rules={[
49+
() => ({
50+
async validator(rule, value) {
51+
await timeout(1000);
52+
53+
return value?.length >= 8
54+
? Promise.resolve()
55+
: Promise.reject(
56+
<>
57+
This field should be <b>at least 8 symbols</b> long
58+
</>,
59+
);
60+
},
61+
}),
62+
]}
63+
>
64+
<TextInput />
65+
</Field>
66+
<Submit>Submit</Submit>
67+
</Form>
68+
);
69+
};
70+
2971
const ComplexErrorTemplate: StoryFn<typeof Form> = (args) => {
3072
const [form] = Form.useForm();
3173

@@ -224,4 +266,6 @@ export const FormInsideDialog: StoryFn = () => {
224266

225267
export const Default = Template.bind({});
226268

227-
export const ComplexErrorMessage: StoryFn = ComplexErrorTemplate.bind({});
269+
export const ComplexErrorMessage = ComplexErrorTemplate.bind({});
270+
271+
export const AsyncValidation = AsyncValidationTemplate.bind({});

src/components/forms/Form/Form.tsx

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function Form<T extends FieldTypes>(
110110
let onSubmitCallback;
111111

112112
if ((onSubmit || onSubmitFailed) && !otherProps.action) {
113-
onSubmitCallback = (e) => {
113+
onSubmitCallback = async (e) => {
114114
if (e && e?.preventDefault) {
115115
e && e?.preventDefault && e?.preventDefault();
116116
e && e?.stopPropagation && e?.stopPropagation();
@@ -127,29 +127,25 @@ function Form<T extends FieldTypes>(
127127
}
128128
}
129129

130-
return form?.validateFields().then(
131-
async () => {
132-
await timeout();
133-
134-
if (form && !form.isSubmitting) {
135-
try {
136-
form.setSubmitting(true);
137-
await onSubmit?.(form.getFormData());
138-
} finally {
139-
form.setSubmitting(false);
140-
}
141-
}
142-
},
143-
async (e) => {
144-
await timeout();
145-
if (e instanceof Error) {
146-
throw e;
147-
}
148-
// errors are shown
149-
// transfer errors to the callback
150-
onSubmitFailed?.(e);
151-
},
152-
);
130+
if (!form || form.isSubmitting) return;
131+
132+
form.setSubmitting(true);
133+
134+
try {
135+
await form.validateFields();
136+
await timeout();
137+
await onSubmit?.(form.getFormData());
138+
} catch (e) {
139+
await timeout();
140+
if (e instanceof Error) {
141+
throw e;
142+
}
143+
// errors are shown
144+
// transfer errors to the callback
145+
onSubmitFailed?.(e);
146+
} finally {
147+
form?.setSubmitting(false);
148+
}
153149

154150
// output data from form directly
155151
// onSubmit(Object.fromEntries(new FormData(e.target).entries()));

0 commit comments

Comments
 (0)