@@ -141,9 +141,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
141
141
}
142
142
143
143
/**
144
- * Sets a value the advanced way.
145
- * @param key The field to set.
146
- * @param value The value to set in the field.
144
+ * Sets a value without calculating whether is has been modified.
147
145
* @param dirty Is this field dirty? Leave undefined to not set any dirty value. (can always be overridden by child forms)
148
146
* @param validate Should the form validate after value set? Overrides `validateOnChange`.
149
147
* @param isDefault Is this the default value for the said field?
@@ -163,8 +161,12 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
163
161
) {
164
162
let valueMap = isDefault ? this . defaultValues : this . values ;
165
163
if ( value === undefined ) {
166
- if ( Array . isArray ( valueMap ) ) valueMap . splice ( key as number , 1 ) ;
167
- else delete valueMap [ key ] ;
164
+ if ( Array . isArray ( valueMap ) ) {
165
+ // Deleting a key in an array doesn't work, splice instead
166
+ valueMap . splice ( key as number , 1 ) ;
167
+ } else {
168
+ delete valueMap [ key ] ;
169
+ }
168
170
} else {
169
171
valueMap [ key ] = value ;
170
172
}
@@ -173,8 +175,8 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
173
175
174
176
if ( notifyChild ) {
175
177
let child = this . childMap [ key ] ;
176
- if ( child && value !== undefined && value !== null ) {
177
- child . setValues ( value ! , validate , isDefault , true , false ) ;
178
+ if ( child ) {
179
+ child . setValues ( value , validate , isDefault , true , false ) ;
178
180
this . dirtyMap [ key ] = child . dirty ;
179
181
}
180
182
}
@@ -258,36 +260,47 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
258
260
* @param notifyChild Should this form notify the child form about this change?
259
261
* @param notifyParent Should this form notify the parent form about this change?
260
262
*/
261
- public setValues ( values : Partial < T > , validate ?: boolean , isDefault : boolean = false , notifyChild : boolean = true , notifyParent : boolean = true ) {
263
+ public setValues (
264
+ values : Partial < T > | undefined ,
265
+ validate ?: boolean ,
266
+ isDefault : boolean = false ,
267
+ notifyChild : boolean = true ,
268
+ notifyParent : boolean = true
269
+ ) {
262
270
let keys = Object . keys ( isDefault ? this . defaultValues : this . values ) ;
263
- addDistinct ( keys , Object . keys ( values ) ) ;
271
+ let v : typeof values = values ?? { } ;
272
+ addDistinct ( keys , Object . keys ( v ) ) ;
264
273
265
274
// Traverse backwards, so when removing array items, the whole array gets shifted in the right direction
266
275
for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
267
276
let key = keys [ i ] as keyof T ;
268
277
this . setValue (
269
278
key ,
270
- values [ key ] ,
279
+ v [ key ] ,
271
280
false , // Will validate after all values are copied
272
281
isDefault ,
273
282
notifyChild ,
274
283
false , // Will call updateParentValues by itself after all values are copied, see 3 lines down
275
284
false // Will call fireAnyListener by itself after all values are copied, see 3 lines down
276
285
) ;
277
286
}
287
+
278
288
this . fireAnyListeners ( ) ;
279
289
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
- ) ;
290
+ if ( typeof values === "object" && values !== null ) {
291
+ this . parent . setValueInternal (
292
+ this . name ,
293
+ isDefault ? memberCopy ( this . defaultValues ) : memberCopy ( this . values ) ,
294
+ this . dirty ,
295
+ validate ,
296
+ isDefault ,
297
+ false ,
298
+ true ,
299
+ true
300
+ ) ;
301
+ } else {
302
+ this . parent . setValueInternal ( this . name , values , this . dirty , validate , isDefault , false , true , true ) ;
303
+ }
291
304
}
292
305
293
306
if ( validate ?? ( this . validateOnChange && this . validator ) ) this . validate ( ) ;
0 commit comments