From 16592aadad9409eefc74dd10a936817bab24b9ae Mon Sep 17 00:00:00 2001 From: Jens Ravens Date: Thu, 19 Oct 2023 13:06:54 +0200 Subject: [PATCH] Null Exception for Model Updates (#31) * initial commit for Null Exception for Model Updates * add null fix --- src/field.ts | 2 +- src/form.spec.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/field.ts b/src/field.ts index 6ccfec5..d6efe43 100644 --- a/src/field.ts +++ b/src/field.ts @@ -180,7 +180,7 @@ export class FieldImplementation }; updateOriginalValue(value: Partial): void { - if (typeof value === 'object') { + if (value && typeof value === 'object') { this.#originalValue = { ...this.#originalValue, ...value }; Object.keys(value).forEach((key) => { const field = this.fields[key]; diff --git a/src/form.spec.ts b/src/form.spec.ts index 0a1f2fa..b6bccd1 100644 --- a/src/form.spec.ts +++ b/src/form.spec.ts @@ -535,6 +535,12 @@ describe(Form, () => { expect(form.fields.name.value).toEqual('Jorge'); }); + it('allows updating the underlying model even with null values', async () => { + const form = createForm({ value: { name: undefined } }); + form.updateOriginalModel({ name: null }); // null counts as an object, check that it doesn't break + expect(form.model.name).toBeNull(); + }); + it('does not change the dirty status when changing the underlying model', async () => { const form = createForm({ value: { name: 'Klaus' } }); form.fields.name.onChange('Claudia');