Skip to content

Commit

Permalink
Change isSubmitting behaviour to mimic v1
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 reverts
it back to the version 1 behaviour of not touching the isSubmitting
state after the onSubmit handler has been called.

Solves: #1957
  • Loading branch information
Tigge committed Nov 8, 2019
1 parent 5bae8e0 commit ae48d6d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 31 deletions.
19 changes: 1 addition & 18 deletions src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { LowPriority, unstable_runWithPriority } from 'scheduler';
type FormikMessage<Values> =
| { type: 'SUBMIT_ATTEMPT' }
| { type: 'SUBMIT_FAILURE' }
| { type: 'SUBMIT_SUCCESS' }
| { type: 'SET_ISVALIDATING'; payload: boolean }
| { type: 'SET_ISSUBMITTING'; payload: boolean }
| { type: 'SET_VALUES'; payload: Values }
Expand Down Expand Up @@ -102,11 +101,6 @@ function formikReducer<Values>(
...state,
isSubmitting: false,
};
case 'SUBMIT_SUCCESS':
return {
...state,
isSubmitting: false,
};
default:
return state;
}
Expand Down Expand Up @@ -710,18 +704,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
(combinedErrors: FormikErrors<Values>) => {
const isActuallyValid = Object.keys(combinedErrors).length === 0;
if (isActuallyValid) {
return Promise.resolve(executeSubmit())
.then(() => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_SUCCESS' });
}
})
.catch(_errors => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
throw _errors;
}
});
return executeSubmit();
} else if (!!isMounted.current) {
// ^^^ Make sure Formik is still mounted before calling setState
dispatch({ type: 'SUBMIT_FAILURE' });
Expand Down
14 changes: 1 addition & 13 deletions test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,19 +489,6 @@ describe('<Formik>', () => {
);
});
});

describe('submitForm helper should not break promise chain if handleSubmit has returned rejected Promise', () => {
it('submitForm helper should not break promise chain if handleSubmit has returned rejected Promise', async () => {
const error = new Error('This Error is typeof Error');
const handleSubmit = () => {
return Promise.reject(error);
};
const { getProps } = renderFormik({ onSubmit: handleSubmit });

const { submitForm } = getProps();
await expect(submitForm()).rejects.toEqual(error);
});
});
});

describe('with validate (ASYNC)', () => {
Expand Down Expand Up @@ -1136,6 +1123,7 @@ 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();
Expand Down

0 comments on commit ae48d6d

Please sign in to comment.