Skip to content

Commit c7d4114

Browse files
authored
hotfix: Revert throwing combined errors (#2103)
* Revert submission regression in 2.0.7 * Stub formik-native test
1 parent 2917d5b commit c7d4114

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { sum } from '../src';
2-
31
describe('blah', () => {
42
it('works', () => {
5-
expect(sum(1, 1)).toEqual(2);
3+
expect(1).toEqual(1);
64
});
75
});

packages/formik/src/Formik.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,10 @@ export function useFormik<Values extends FormikValues = FormikValues>({
564564
if ((eventOrTextValue as React.ChangeEvent<any>).persist) {
565565
(eventOrTextValue as React.ChangeEvent<any>).persist();
566566
}
567-
const target = eventOrTextValue.target ? (eventOrTextValue as React.ChangeEvent<any>).target : (eventOrTextValue as React.ChangeEvent<any>).currentTarget;
568-
567+
const target = eventOrTextValue.target
568+
? (eventOrTextValue as React.ChangeEvent<any>).target
569+
: (eventOrTextValue as React.ChangeEvent<any>).currentTarget;
570+
569571
const {
570572
type,
571573
name,
@@ -715,7 +717,10 @@ export function useFormik<Values extends FormikValues = FormikValues>({
715717
(combinedErrors: FormikErrors<Values>) => {
716718
const isActuallyValid = Object.keys(combinedErrors).length === 0;
717719
if (isActuallyValid) {
720+
// Proceed with submit
718721
const promiseOrUndefined = executeSubmit();
722+
// Bail if it's sync, consumer is responsible for cleaning up
723+
// via setSubmitting(false)
719724
if (promiseOrUndefined === undefined) {
720725
return;
721726
}
@@ -729,13 +734,14 @@ export function useFormik<Values extends FormikValues = FormikValues>({
729734
.catch(_errors => {
730735
if (!!isMounted.current) {
731736
dispatch({ type: 'SUBMIT_FAILURE' });
737+
// This is a legit error rejected by the onSubmit fn
738+
// so we don't want to break the promise chain
732739
throw _errors;
733740
}
734741
});
735742
} else if (!!isMounted.current) {
736743
// ^^^ Make sure Formik is still mounted before calling setState
737744
dispatch({ type: 'SUBMIT_FAILURE' });
738-
throw combinedErrors;
739745
}
740746
return;
741747
}

packages/formik/test/Formik.test.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -580,18 +580,6 @@ describe('<Formik>', () => {
580580
fireEvent.submit(getByTestId('form'));
581581
expect(validate).toHaveBeenCalled();
582582
});
583-
584-
it('should not submit the form if validate function rejects with an error', async () => {
585-
const onSubmit = jest.fn();
586-
const validationSchema = Yup.object().shape({
587-
field: Yup.string().required('required'),
588-
});
589-
590-
const { getProps } = renderFormik({ onSubmit, validationSchema });
591-
await expect(getProps().submitForm()).rejects.toEqual({
592-
field: 'required',
593-
});
594-
});
595583
});
596584

597585
describe('FormikHelpers', () => {

0 commit comments

Comments
 (0)