Skip to content

Commit 8f96467

Browse files
committed
Fix date comparison error
1 parent 2433464 commit 8f96467

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/Field.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ export function defaultSerializer<T>(currentValue: T, props: SerializeProps<T>):
158158
case "datetime-local":
159159
case "date": {
160160
let dateValue = currentValue as any;
161+
if (dateValue === null || dateValue === undefined || dateValue === "") {
162+
return "";
163+
}
161164
if (typeof dateValue === "string") {
162165
let ni = parseInt(dateValue);
163166
if (!isNaN(ni)) dateValue = ni;

src/form.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,24 @@ export class FormState<T extends object, State = DefaultState, Error extends str
209209
fireAny: boolean = true
210210
) {
211211
// value can contain the default value or normal value. (Determined by isDefault)
212+
let other = isDefault ? this.values[key] : this.defaultValues[key];
212213
if (typeof value === "object" && value !== null) {
213214
let dirty: boolean | undefined = false;
214-
if (value instanceof Date) {
215-
// Compare date objects
216-
dirty = value?.getTime() !== (isDefault ? this.values[key] : (this.defaultValues[key] as any))?.getTime();
217-
} else if (!(key in this.childMap)) {
218-
// Compare objects if there is no child form, because it calculates the dirty value for us
219-
let other = isDefault ? this.values[key] : this.defaultValues[key];
220-
dirty = JSON.stringify(value) !== JSON.stringify(other);
215+
216+
// Compare objects if there is no child form, because it calculates the dirty value for us
217+
if (!(key in this.childMap)) {
218+
if (value instanceof Date && other instanceof Date) {
219+
// Compare date objects
220+
dirty = value?.getTime() !== other?.getTime();
221+
} else {
222+
dirty = JSON.stringify(value) !== JSON.stringify(other);
223+
}
221224
}
222225

223226
this.setValueInternal(key, value, dirty, validate, isDefault, notifyChild, notifyParent, fireAny);
224227
} else {
225228
// Compare value and existing value/defaultValue which determines dirty
226-
let dirty = value !== (isDefault ? this.values[key] : this.defaultValues[key]);
229+
let dirty = value !== other;
227230
if ((isDefault ? this.defaultValues[key] : this.values[key]) === value && this.dirtyMap[key] === dirty) {
228231
return;
229232
}

0 commit comments

Comments
 (0)