Skip to content

Commit

Permalink
fix: FormApi's deleteField method now updates the store
Browse files Browse the repository at this point in the history
* Add basic tests for arrays

* Ran prettier

* Ran prettier

* Add new test for bug

* Fix bug regarding preserved values even if field is umounted

* Run prettier

* Update store subscription when removingFields

* Fix delete field

* Fix delete field

* chore: fix linting

---------

Co-authored-by: Corbin Crutchley <[email protected]>
  • Loading branch information
Christian24 and crutchcorn authored Nov 5, 2023
1 parent 90bd06f commit 1a37fab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,12 @@ export class FormApi<TFormData, ValidatorType> {
}

deleteField = <TField extends DeepKeys<TFormData>>(field: TField) => {
const newState = { ...this.state }
delete newState.values[field as keyof TFormData]
delete newState.fieldMeta[field]

this.store.setState((_) => newState)
this.store.setState((prev) => {
const newState = { ...prev }
delete newState.values[field as keyof TFormData]
delete newState.fieldMeta[field]
return newState
})
}

pushFieldValue = <TField extends DeepKeys<TFormData>>(
Expand Down
2 changes: 2 additions & 0 deletions packages/form-core/src/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,5 +611,7 @@ describe('field api', () => {
// Field should have been removed from the form as well
expect(form.state.values.name).toBeUndefined()
expect(form.state.fieldMeta.name).toBeUndefined()
expect(form.store.state.values.name).toBeUndefined()
expect(form.store.state.fieldMeta.name).toBeUndefined()
})
})

0 comments on commit 1a37fab

Please sign in to comment.