@ngx-formly/core 6.0.2 → 6.0.4

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.
@@ -3,7 +3,7 @@ import { Type, TemplateRef, ComponentRef, ChangeDetectorRef, InjectionToken, Inj
3
3
  import * as i2 from '@angular/forms';
4
4
  import { AbstractControl, FormGroup, FormArray, FormControl, Validators } from '@angular/forms';
5
5
  import { isObservable, merge, of, Observable, Subject } from 'rxjs';
6
- import { distinctUntilChanged, startWith, debounceTime, filter, switchMap, take, map } from 'rxjs/operators';
6
+ import { distinctUntilChanged, startWith, debounceTime, filter, switchMap, take, tap, map } from 'rxjs/operators';
7
7
  import * as i2$1 from '@angular/common';
8
8
  import { DOCUMENT, CommonModule } from '@angular/common';
9
9
  import * as i1 from '@angular/platform-browser';
@@ -410,7 +410,12 @@ class FormlyConfig {
410
410
  }
411
411
  const componentRef = _viewContainerRef.createComponent(type.component, { injector: _injector });
412
412
  defineHiddenProp(type, '_componentRef', componentRef);
413
- componentRef.destroy();
413
+ try {
414
+ componentRef.destroy();
415
+ }
416
+ catch (e) {
417
+ console.error(`An error occurred while destroying the Formly component type "${field.type}"`, e);
418
+ }
414
419
  return type._componentRef;
415
420
  }
416
421
  setWrapper(options) {
@@ -889,6 +894,23 @@ class FormlyField {
889
894
  observeDeep(field, ['props'], () => field.options.detectChanges(field)),
890
895
  observeDeep(field.options, ['formState'], () => field.options.detectChanges(field)),
891
896
  ];
897
+ for (const key of Object.keys(field._expressions)) {
898
+ const expressionObserver = observe(field, ['_expressions', key], ({ currentValue, previousValue }) => {
899
+ if (previousValue?.subscription) {
900
+ previousValue.subscription.unsubscribe();
901
+ previousValue.subscription = null;
902
+ }
903
+ if (isObservable(currentValue.value$)) {
904
+ currentValue.subscription = currentValue.value$.subscribe();
905
+ }
906
+ });
907
+ subscribes.push(() => {
908
+ if (field._expressions[key]?.subscription) {
909
+ field._expressions[key].subscription.unsubscribe();
910
+ }
911
+ expressionObserver.unsubscribe();
912
+ });
913
+ }
892
914
  for (const path of [['template'], ['fieldGroupClassName'], ['validation', 'show']]) {
893
915
  const fieldObserver = observe(field, path, ({ firstChange }) => !firstChange && field.options.detectChanges(field));
894
916
  subscribes.push(() => fieldObserver.unsubscribe());
@@ -1408,7 +1430,8 @@ class FieldArrayType extends FieldType {
1408
1430
  markAsDirty && this.formControl.markAsDirty();
1409
1431
  }
1410
1432
  _build() {
1411
- this.options.build(this.field);
1433
+ const fields = this.field.formControl._fields ?? [this.field];
1434
+ fields.forEach((f) => this.options.build(f));
1412
1435
  this.options.fieldChanges.next({
1413
1436
  field: this.field,
1414
1437
  value: getFieldValue(this.field),
@@ -1510,22 +1533,11 @@ class FieldExpressionExtension {
1510
1533
  field._expressions[key] = this.parseExpressions(field, key, expr);
1511
1534
  }
1512
1535
  else if (expr instanceof Observable) {
1513
- const subscribe = () => expr.subscribe((v) => {
1514
- this.evalExpr(field, key, v);
1515
- });
1516
- let subscription = null;
1517
- const onInit = field.hooks.onInit;
1518
- field.hooks.onInit = () => {
1519
- if (subscription === null) {
1520
- subscription = subscribe();
1521
- }
1522
- return onInit?.(field);
1523
- };
1524
- const onDestroy = field.hooks.onDestroy;
1525
- field.hooks.onDestroy = () => {
1526
- onDestroy?.(field);
1527
- subscription.unsubscribe();
1528
- subscription = null;
1536
+ field._expressions[key] = {
1537
+ value$: expr.pipe(tap((v) => {
1538
+ this.evalExpr(field, key, v);
1539
+ field.options.detectChanges(field);
1540
+ })),
1529
1541
  };
1530
1542
  }
1531
1543
  };
@@ -1619,7 +1631,7 @@ class FieldExpressionExtension {
1619
1631
  let fieldChanged = false;
1620
1632
  if (field._expressions) {
1621
1633
  for (const key of Object.keys(field._expressions)) {
1622
- field._expressions[key].callback(ignoreCache) && (fieldChanged = true);
1634
+ field._expressions[key].callback?.(ignoreCache) && (fieldChanged = true);
1623
1635
  }
1624
1636
  }
1625
1637
  field.fieldGroup?.forEach((f) => this.checkExpressions(f, ignoreCache) && (fieldChanged = true));