Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the formik submit handler returning a promise for synchronous submit handlers which in turn immediately sets the isSubmitting to false. #2357

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/formik/Form/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const Form = forwardRef(
) => {
const formikRef = useRef();

const handleSubmit = async (values, actions) => {
const handleSubmit = (values, actions) => {
const fieldsWithServerError = getFieldsWithServerError(
formikRef.current?.status
);

if (fieldsWithServerError.length > 0) {
actions.setSubmitting(false);

return;
return undefined;
}

await formikProps.onSubmit(values, actions);
return formikProps.onSubmit(values, actions);
};

return (
Expand Down
68 changes: 44 additions & 24 deletions tests/formik/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,24 @@ const FormikForm = ({
validateOnBlur,
validateOnChange,
scrollToErrorField,
}) => {
const handleSubmit = values => {
onSubmit(values);
};

return (
<Form
{...{ scrollToErrorField }}
className="nui-form-wrapper"
formikProps={{
initialValues: { name: "Oliver Smith" },
validationSchema: yup.object().shape({
name: yup.string().required("Name is required"),
}),
onSubmit: handleSubmit,
validateOnBlur,
validateOnChange,
}}
>
<Input label="First Name" name="name" />
<Button type="submit">Submit</Button>
</Form>
);
};
}) => (
<Form
{...{ scrollToErrorField }}
className="nui-form-wrapper"
formikProps={{
initialValues: { name: "Oliver Smith" },
validationSchema: yup.object().shape({
name: yup.string().required("Name is required"),
}),
onSubmit: values => onSubmit(values),
validateOnBlur,
validateOnChange,
}}
>
<Input label="First Name" name="name" />
<Button type="submit">Submit</Button>
</Form>
);

const EmptyMultiLineForm = ({ onSubmit }) => {
const handleSubmit = values => {
Expand Down Expand Up @@ -162,4 +156,30 @@ describe("formik/Form", () => {
expect(screen.queryByText("Address is required")).not.toBeInTheDocument();
await waitFor(() => expect(onSubmit).not.toHaveBeenCalled());
});

it("should prevent submit form until the async submit handler is resolved", async () => {
const onSubmit = jest.fn().mockImplementation(
() =>
new Promise(resolve => {
setTimeout(resolve, 2000);
})
);

render(<FormikForm {...{ onSubmit }} />);
const input = screen.getByLabelText("First Name");
const button = screen.getByRole("button");

await userEvent.clear(input);
await userEvent.type(input, "Oliver");

expect(button).not.toBeDisabled();
await userEvent.click(button);

jest.useFakeTimers();
await jest.advanceTimersByTime(1000);
await expect(button).toBeDisabled();
jest.useRealTimers();

waitFor(expect(button).not.toBeDisabled);
});
});
Loading