@rxap/forms 13.0.1 → 13.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -215,25 +215,12 @@ class RxapFormArray extends FormArray {
215
215
  markAllAsPristine() {
216
216
  markAllPristine(this);
217
217
  }
218
- coerceValue(value) {
219
- for (let index = 0; index < value.length; index++) {
220
- if (!this.at(index)) {
221
- this.insertAt(index, value[index]);
222
- }
223
- }
224
- }
225
218
  setValue(valueOrObservable, options) {
226
219
  if (isObservable(valueOrObservable)) {
227
220
  return valueOrObservable.subscribe((value) => {
228
- if (options?.coerce) {
229
- this.coerceValue(value);
230
- }
231
221
  super.setValue(value, options);
232
222
  });
233
223
  }
234
- if (options?.coerce) {
235
- this.coerceValue(valueOrObservable);
236
- }
237
224
  super.setValue(valueOrObservable, options);
238
225
  }
239
226
  markAllAsUntouched() {
@@ -247,19 +234,36 @@ class RxapFormArray extends FormArray {
247
234
  hasErrorAndTouched(errorCode, path) {
248
235
  return hasErrorAndTouched(this, errorCode, path);
249
236
  }
237
+ _patchValue(value, options) {
238
+ // Even though the `value` argument type doesn't allow `null` and `undefined` values, the
239
+ // `patchValue` can be called recursively and inner data structures might have these values, so
240
+ // we just ignore such cases when a field containing FormArray instance receives `null` or
241
+ // `undefined` as a value.
242
+ if (value == null /* both `null` and `undefined` */)
243
+ return;
244
+ const largesIndex = value.length === 0 ? -1 : Object.keys(value).map(Number).sort().reverse()[0];
245
+ value.forEach((newValue, index) => {
246
+ if (options?.strict && index > largesIndex) {
247
+ this.removeAt(index);
248
+ }
249
+ else {
250
+ if (this.at(index)) {
251
+ this.at(index).patchValue(newValue, { ...(options ?? {}), onlySelf: true });
252
+ }
253
+ else if (options?.coerce) {
254
+ this.insertAt(index, value[index]);
255
+ }
256
+ }
257
+ });
258
+ this.updateValueAndValidity(options);
259
+ }
250
260
  patchValue(valueOrObservable, options) {
251
261
  if (isObservable(valueOrObservable)) {
252
262
  return valueOrObservable.subscribe((value) => {
253
- if (options?.coerce) {
254
- this.coerceValue(value);
255
- }
256
- super.patchValue(value, options);
263
+ this._patchValue(value, options);
257
264
  });
258
265
  }
259
- if (options?.coerce) {
260
- this.coerceValue(valueOrObservable);
261
- }
262
- super.patchValue(valueOrObservable, options);
266
+ this._patchValue(valueOrObservable, options);
263
267
  }
264
268
  hasErrorAndDirty(errorCode, path) {
265
269
  return hasErrorAndDirty(this, errorCode, path);
@@ -431,11 +435,25 @@ class RxapFormGroup extends FormGroup {
431
435
  }
432
436
  super.setValue(valueOrObservable, options);
433
437
  }
438
+ _patchValue(value, options) {
439
+ // Even though the `value` argument type doesn't allow `null` and `undefined` values, the
440
+ // `patchValue` can be called recursively and inner data structures might have these values, so
441
+ // we just ignore such cases when a field containing FormGroup instance receives `null` or
442
+ // `undefined` as a value.
443
+ if (value == null /* both `null` and `undefined` */)
444
+ return;
445
+ Object.keys(value).forEach(name => {
446
+ if (this.controls[name]) {
447
+ this.controls[name].patchValue(value[name], { ...(options ?? {}), onlySelf: true });
448
+ }
449
+ });
450
+ this.updateValueAndValidity(options);
451
+ }
434
452
  patchValue(valueOrObservable, options) {
435
453
  if (isObservable(valueOrObservable)) {
436
- return valueOrObservable.subscribe((value) => super.patchValue(value, options));
454
+ return valueOrObservable.subscribe((value) => this._patchValue(value, options));
437
455
  }
438
- super.patchValue(valueOrObservable, options);
456
+ this._patchValue(valueOrObservable, options);
439
457
  }
440
458
  disabledWhile(observable, options) {
441
459
  return controlDisabledWhile(this, observable, options);