Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hopefully fix deleteField #506

Merged
merged 14 commits into from
Nov 5, 2023
11 changes: 6 additions & 5 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,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()
})
})