Skip to content

Commit 2c37a6f

Browse files
committed
Remove comparePrimitiveObjects, use JSON.stringify comparison instead
1 parent 5f2bcd4 commit 2c37a6f

File tree

1 file changed

+4
-31
lines changed

1 file changed

+4
-31
lines changed

src/form.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,6 @@ function addDistinct<T extends any[]>(arr1: T, arr2: T) {
4040
}
4141
}
4242

43-
/**
44-
* Compares 2 objects that only contain primitive fields (no object fields)
45-
* @returns true when different, false when 'equal', undefined when an object field was found.
46-
*/
47-
function comparePrimitiveObject<T>(a: T, b: T): boolean | undefined {
48-
// Compare null and undefined
49-
if (!a || !b) return a === b;
50-
let ak = Object.keys(a),
51-
bk = Object.keys(b);
52-
let lk = ak.length > bk.length ? ak : bk;
53-
for (let i = 0; i < lk.length; i++) {
54-
let k = lk[i];
55-
let av = a[k],
56-
bv = b[k];
57-
if ((typeof av === "object" && av !== null) || (typeof bv === "object" && bv !== null)) return undefined;
58-
if (av !== bv) return true;
59-
}
60-
return false;
61-
}
62-
6343
export class FormState<T extends object, State = DefaultState, Error extends string = DefaultError> {
6444
/**
6545
* The id of this form, for debugging purposes.
@@ -230,17 +210,10 @@ export class FormState<T extends object, State = DefaultState, Error extends str
230210
if (value instanceof Date) {
231211
// Compare date objects
232212
dirty = value?.getTime() !== (isDefault ? this.values[key] : (this.defaultValues[key] as any))?.getTime();
233-
} else if (fireAny) {
234-
// Compare primitive objects (objects containing only primitive fields), but only is setValues was not used (dirty value will be determined by child forms)
235-
dirty = comparePrimitiveObject(value, isDefault ? this.values[key] : this.defaultValues[key]); // Is switched intentionally
236-
if (dirty === undefined) {
237-
console.warn(
238-
"Do not use setValue for object in object fields, use setValueInternal instead (dirty value can not be determined), ",
239-
key,
240-
value
241-
);
242-
dirty = true;
243-
}
213+
} else if (!(key in this.childMap)) {
214+
// Compare objects if there is no child form, because it calculates the dirty value for us
215+
let other = isDefault ? this.values[key] : this.defaultValues[key];
216+
dirty = JSON.stringify(value) !== JSON.stringify(other);
244217
}
245218

246219
this.setValueInternal(key, value, dirty, validate, isDefault, notifyChild, notifyParent, fireAny);

0 commit comments

Comments
 (0)