Skip to content

Commit

Permalink
Null Exception for Model Updates (#31)
Browse files Browse the repository at this point in the history
* initial commit for Null Exception for Model Updates

* add null fix
  • Loading branch information
JensRavens authored Oct 19, 2023
1 parent d57a95f commit 16592aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class FieldImplementation<T, Model>
};

updateOriginalValue(value: Partial<T>): 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];
Expand Down
6 changes: 6 additions & 0 deletions src/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 16592aa

Please sign in to comment.