Skip to content

Commit 28097e9

Browse files
committed
Use instanceof ChildFormState check instead of function override
1 parent efac3d3 commit 28097e9

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export function Form() {
287287
<hr />
288288
<h3>Togglable object field</h3>
289289
<label>
290-
<FormInput form={form} name="author" type="checkbox" setUndefinedOnUncheck value={{ name: "", age: 0 }} />
290+
<FormInput form={form} name="author" type="checkbox" setNullOnUncheck value={{ name: "", age: 0 }} />
291291
Enable author
292292
</label>
293293
<ChildForm

src/form.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function addDistinct<T extends any[]>(arr1: T, arr2: T) {
3939
* Compares 2 objects that only contain primitive fields (no object fields)
4040
* @returns true when different, false when 'equal', undefined when an object field was found.
4141
*/
42-
export function comparePrimitiveObject<T>(a: T, b: T): boolean | undefined {
42+
function comparePrimitiveObject<T>(a: T, b: T): boolean | undefined {
4343
// Compare null and undefined
4444
if (!a || !b) return a === b;
4545
let ak = Object.keys(a),
@@ -181,7 +181,19 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
181181

182182
this.fireListeners(key);
183183
if (fireAny) this.fireAnyListeners(); // Will be false when using setValues, he will call fireAnyListeners and notifyParentValues itself
184-
if (notifyParent) this.updateParentValues(isDefault, validate); // Will call setValueInternal on parent
184+
185+
if (notifyParent && this instanceof ChildFormState) {
186+
this.parent.setValueInternal(
187+
this.name,
188+
Object.keys(valueMap).length > 0 ? memberCopy(valueMap) : undefined,
189+
this.dirty,
190+
validate,
191+
isDefault,
192+
false,
193+
true,
194+
true
195+
);
196+
}
185197

186198
if (validate ?? (this.validateOnChange && this.validator)) this.validate();
187199
}
@@ -239,7 +251,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
239251
}
240252

241253
/**
242-
* Set all values OR default values on this form.
254+
* Set multiple values OR default values on this form.
243255
* @param values The new values to set on this form.
244256
* @param validate Validate? Overrides `validateOnChange`.
245257
* @param isDefault Are these values the default values for this form? This function only updates values or defaultValues, not both! To set both, use `form.setDefaultValues()`.
@@ -264,13 +276,25 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
264276
);
265277
}
266278
this.fireAnyListeners();
267-
if (notifyParent) this.updateParentValues(isDefault, validate);
279+
if (notifyParent && this instanceof ChildFormState) {
280+
let values = isDefault ? memberCopy(this.defaultValues) : memberCopy(this.values);
281+
this.parent.setValueInternal(
282+
this.name,
283+
Object.keys(values).length > 0 ? values : undefined,
284+
this.dirty,
285+
validate,
286+
isDefault,
287+
false,
288+
true,
289+
true
290+
);
291+
}
268292

269293
if (validate ?? (this.validateOnChange && this.validator)) this.validate();
270294
}
271295

272296
/**
273-
* Set both values and default values for this form. If you only want to set default values, use `setValues(...,...,true)`.
297+
* Set both values and default values for this form. If you only want to set values, use setValues(...). If you only want to set default values, use `setValues(...,...,true)`.
274298
* @param values The new default values to set on this form.
275299
* @param validate Validate? Overrides `validateOnChange`.
276300
* @param notifyChild Should this form notify the child form about this change?
@@ -413,7 +437,10 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
413437

414438
c.forEach((e) => this.fireListeners(e));
415439
this.fireAnyListeners();
416-
if (notifyParent) this.updateParentState();
440+
441+
if (notifyParent && this instanceof ChildFormState) {
442+
this.parent.setState(memberCopy(this.state), false, true);
443+
}
417444
}
418445

419446
/**
@@ -508,14 +535,6 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
508535
let al = Object.keys(this.anyListeners);
509536
al.forEach((e) => this.anyListeners[e]!());
510537
}
511-
512-
protected updateParentValues(_isDefault: boolean, _validate: boolean | undefined) {
513-
// Not implemented for root form, as it does not have a parent
514-
}
515-
516-
protected updateParentState() {
517-
// Not implemented for root form, as it does not have a parent
518-
}
519538
}
520539

521540
export class ChildFormState<Parent, Key extends keyof Parent, ParentState, ParentError extends string> extends FormState<
@@ -538,22 +557,4 @@ export class ChildFormState<Parent, Key extends keyof Parent, ParentState, Paren
538557
this.parent = parent;
539558
this.name = name;
540559
}
541-
542-
protected updateParentValues(isDefault: boolean, validate: boolean | undefined) {
543-
let values = isDefault ? memberCopy(this.defaultValues) : memberCopy(this.values);
544-
this.parent.setValueInternal(
545-
this.name,
546-
Object.keys(values).length > 0 ? values : undefined,
547-
this.dirty,
548-
validate,
549-
isDefault,
550-
false,
551-
true,
552-
true
553-
);
554-
}
555-
556-
protected updateParentState() {
557-
this.parent.setState(memberCopy(this.state), false, true);
558-
}
559560
}

0 commit comments

Comments
 (0)