diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 38b811224..1cefa603e 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -360,8 +360,11 @@ export class FormApi { } deleteField = >(field: TField) => { - delete this.state.values[field as keyof TFormData] - delete this.state.fieldMeta[field] + const newState = { ...this.state } + delete newState.values[field as keyof TFormData] + delete newState.fieldMeta[field] + + this.store.setState((_) => newState) } pushFieldValue = >( diff --git a/packages/form-core/src/tests/FieldApi.spec.ts b/packages/form-core/src/tests/FieldApi.spec.ts index 04698a46f..0196cb846 100644 --- a/packages/form-core/src/tests/FieldApi.spec.ts +++ b/packages/form-core/src/tests/FieldApi.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'vitest' +import { expect, vitest } from 'vitest' import { FormApi } from '../FormApi' import { FieldApi } from '../FieldApi' @@ -597,11 +597,17 @@ describe('field api', () => { }) const unmount = field.mount() + const callback = vitest.fn() + const subscription = form.store.subscribe(callback) unmount() const info = form.getFieldInfo(field.name) + subscription() expect(info.instances[field.uid]).toBeUndefined() expect(Object.keys(info.instances).length).toBe(0) + // Check that form store has been updated + expect(callback).toHaveBeenCalledOnce() + // Field should have been removed from the form as well expect(form.state.values.name).toBeUndefined() expect(form.state.fieldMeta.name).toBeUndefined()