@@ -39,7 +39,7 @@ function addDistinct<T extends any[]>(arr1: T, arr2: T) {
39
39
* Compares 2 objects that only contain primitive fields (no object fields)
40
40
* @returns true when different, false when 'equal', undefined when an object field was found.
41
41
*/
42
- export function comparePrimitiveObject < T > ( a : T , b : T ) : boolean | undefined {
42
+ function comparePrimitiveObject < T > ( a : T , b : T ) : boolean | undefined {
43
43
// Compare null and undefined
44
44
if ( ! a || ! b ) return a === b ;
45
45
let ak = Object . keys ( a ) ,
@@ -181,7 +181,19 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
181
181
182
182
this . fireListeners ( key ) ;
183
183
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
+ }
185
197
186
198
if ( validate ?? ( this . validateOnChange && this . validator ) ) this . validate ( ) ;
187
199
}
@@ -239,7 +251,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
239
251
}
240
252
241
253
/**
242
- * Set all values OR default values on this form.
254
+ * Set multiple values OR default values on this form.
243
255
* @param values The new values to set on this form.
244
256
* @param validate Validate? Overrides `validateOnChange`.
245
257
* @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
264
276
) ;
265
277
}
266
278
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
+ }
268
292
269
293
if ( validate ?? ( this . validateOnChange && this . validator ) ) this . validate ( ) ;
270
294
}
271
295
272
296
/**
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)`.
274
298
* @param values The new default values to set on this form.
275
299
* @param validate Validate? Overrides `validateOnChange`.
276
300
* @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
413
437
414
438
c . forEach ( ( e ) => this . fireListeners ( e ) ) ;
415
439
this . fireAnyListeners ( ) ;
416
- if ( notifyParent ) this . updateParentState ( ) ;
440
+
441
+ if ( notifyParent && this instanceof ChildFormState ) {
442
+ this . parent . setState ( memberCopy ( this . state ) , false , true ) ;
443
+ }
417
444
}
418
445
419
446
/**
@@ -508,14 +535,6 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
508
535
let al = Object . keys ( this . anyListeners ) ;
509
536
al . forEach ( ( e ) => this . anyListeners [ e ] ! ( ) ) ;
510
537
}
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
- }
519
538
}
520
539
521
540
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
538
557
this . parent = parent ;
539
558
this . name = name ;
540
559
}
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
- }
559
560
}
0 commit comments