Skip to content

Commit

Permalink
Change isSubmitting behaviour to mimic v1 (#1987)
Browse files Browse the repository at this point in the history
In version 2 the behaviour of isSubmitting was changed from having the
user handle its state after submitting to always resetting it to false
after the onSubmit handler had returned (or it's promise).

This adds back to the version 1 behaviour of not touching the
isSubmitting state after the onSubmit handler has been called if we
don't return a promise in the onSubmit handler.

Solves: #1957
  • Loading branch information
Tigge authored and jaredpalmer committed Nov 21, 2019
1 parent c0fe1b9 commit 3ce6551
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Form.tsx
Expand Up @@ -8,7 +8,7 @@ export type FormikFormProps = Pick<
'onReset' | 'onSubmit'
>
>;

type FormProps = React.ComponentPropsWithoutRef<'form'>;

// @todo tests
Expand Down
7 changes: 6 additions & 1 deletion src/Formik.tsx
Expand Up @@ -710,7 +710,12 @@ export function useFormik<Values extends FormikValues = FormikValues>({
(combinedErrors: FormikErrors<Values>) => {
const isActuallyValid = Object.keys(combinedErrors).length === 0;
if (isActuallyValid) {
return Promise.resolve(executeSubmit())
const promiseOrUndefined = executeSubmit();
if (promiseOrUndefined === undefined) {
return;
}

return Promise.resolve(promiseOrUndefined)
.then(() => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_SUCCESS' });
Expand Down
5 changes: 4 additions & 1 deletion src/types.tsx
Expand Up @@ -203,7 +203,10 @@ export interface FormikConfig<Values> extends FormikSharedConfig {
/**
* Submission handler
*/
onSubmit: (values: Values, formikHelpers: FormikHelpers<Values>) => void;
onSubmit: (
values: Values,
formikHelpers: FormikHelpers<Values>
) => void | Promise<void>;
/**
* A Yup Schema or a function that returns a Yup schema
*/
Expand Down
30 changes: 29 additions & 1 deletion test/Formik.test.tsx
Expand Up @@ -1144,7 +1144,7 @@ describe('<Formik>', () => {
expect(getProps().submitCount).toEqual(1);
});

it('isSubmitting is fired when submit is attempted', async () => {
it('isSubmitting is fired when submit is attempted (v1)', async () => {
const onSubmit = jest.fn();
const validate = jest.fn(() => Promise.resolve({}));

Expand All @@ -1164,6 +1164,34 @@ describe('<Formik>', () => {
// do it again async
await validatePromise;
// done validating and submitting
expect(getProps().isSubmitting).toBe(true);
expect(getProps().isValidating).toBe(false);
expect(validate).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
expect(getProps().submitCount).toEqual(1);
});

it('isSubmitting is fired when submit is attempted (v2, promise)', async () => {
const onSubmit = jest.fn().mockResolvedValue(undefined);
const validate = jest.fn(() => Promise.resolve({}));

const { getProps } = renderFormik({
onSubmit,
validate,
});

expect(getProps().submitCount).toEqual(0);
expect(getProps().isSubmitting).toBe(false);
expect(getProps().isValidating).toBe(false);
// we call set isValidating synchronously
const validatePromise = getProps().submitForm();
// so it should change
expect(getProps().isSubmitting).toBe(true);
expect(getProps().isValidating).toBe(true);
// do it again async
await validatePromise;
// done validating and submitting
expect(getProps().isSubmitting).toBe(false);
expect(getProps().isValidating).toBe(false);
expect(validate).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
Expand Down

1 comment on commit 3ce6551

@vercel
Copy link

@vercel vercel bot commented on 3ce6551 Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.