Skip to content

Commit

Permalink
cleanup arrays if the last value, which is not undefined,
Browse files Browse the repository at this point in the history
gets removed from it
  • Loading branch information
Christoph Gruber committed Nov 9, 2021
1 parent b9cc253 commit dcc63d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/fair-students-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'formik': patch
'formik-native': patch
---

Fixes array fields not beeing cleanup up properly if the get deleted
7 changes: 6 additions & 1 deletion packages/formik/src/FieldArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ class FieldArrayInner<Values = {}> extends React.Component<
if (isFunction(copy.splice)) {
copy.splice(index, 1);
}
return copy;
// if the array only includes undefined values we have to return an empty array
return isFunction(copy.every)
? copy.every(v => v === undefined)
? []
: copy
: copy;
},
true,
true
Expand Down
8 changes: 8 additions & 0 deletions packages/formik/test/FieldArray.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ describe('<FieldArray />', () => {
expect(formikBag.errors.friends).toEqual(undefined);
expect(formikBag.touched.friends).toEqual(undefined);
});
it('should clean up errors', () => {
act(() => {
formikBag.setFieldError('friends.1', 'Field error');
arrayHelpers.remove(1);
});

expect(formikBag.errors.friends).toEqual(undefined);
});
});

describe('given array-like object representing errors', () => {
Expand Down

0 comments on commit dcc63d9

Please sign in to comment.