diff --git a/libs/core/src/utils/createField.spec.ts b/libs/core/src/utils/createField.spec.ts index 6bb9df99..4efe3e45 100644 --- a/libs/core/src/utils/createField.spec.ts +++ b/libs/core/src/utils/createField.spec.ts @@ -93,5 +93,39 @@ describe('createField', () => { expect(wrapper.html()).toMatchSnapshot(); expect((await wrapper.findByRole('textbox')).value).toBe('value2'); }); + + it('should remove the field from the form when unmounted', async () => { + expect.assertions(2); + + const form = createForm('renderModular', {}); + const field = createField({ + name: 'modularTest', + label: 'fieldLabel', + ref: ref('value2'), + component: testComponent, + }); + + const wrapper = render(form.Component, { + slots: { + default: () => h('div', [h(field.Component), h(field.Label), h(field.Errors)]), + }, + }); + + await flushPromises(); + + expect(form.instance.fields.value).toHaveLength(1); + + await wrapper.unmount(); + + const wrapper2 = render(form.Component, { + slots: { + default: () => h('div', [h(field.Component), h(field.Label), h(field.Errors)]), + }, + }); + + await flushPromises(); + + expect(form.instance.fields.value).toHaveLength(1); + }); }); }); diff --git a/libs/core/src/utils/createField.ts b/libs/core/src/utils/createField.ts index 494009ef..b86203d9 100644 --- a/libs/core/src/utils/createField.ts +++ b/libs/core/src/utils/createField.ts @@ -6,6 +6,7 @@ import { h, markRaw, onMounted, + onUnmounted, type Ref, ref, reactive, @@ -37,6 +38,13 @@ const createMainComponent = { + if (element.value) { + form.removeElement(element.value.name); + element.value = undefined; + } + }); + return { element, form,