@ngx-formly/core 6.3.1 → 6.3.2
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.
- package/esm2020/lib/components/formly.form.mjs +4 -3
- package/esm2020/lib/extensions/core/core.mjs +7 -15
- package/esm2020/lib/extensions/field-expression/field-expression.mjs +2 -2
- package/esm2020/lib/extensions/field-form/field-form.mjs +7 -1
- package/esm2020/lib/services/formly.builder.mjs +7 -2
- package/esm2020/lib/utils.mjs +16 -1
- package/fesm2015/ngx-formly-core.mjs +37 -19
- package/fesm2015/ngx-formly-core.mjs.map +1 -1
- package/fesm2020/ngx-formly-core.mjs +34 -16
- package/fesm2020/ngx-formly-core.mjs.map +1 -1
- package/lib/utils.d.ts +3 -0
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import * as i0 from '@angular/core';
|
|
|
2
2
|
import { Type, TemplateRef, ComponentRef, ChangeDetectorRef, InjectionToken, Injectable, Optional, Directive, Input, ViewContainerRef, Component, ViewChild, EventEmitter, ChangeDetectionStrategy, Output, ContentChildren, Inject, ViewChildren, NgModule } from '@angular/core';
|
|
3
3
|
import * as i2 from '@angular/forms';
|
|
4
4
|
import { AbstractControl, FormGroup, FormArray, FormControl, NgControl, Validators } from '@angular/forms';
|
|
5
|
-
import { isObservable,
|
|
5
|
+
import { isObservable, of, merge, Observable, Subject } from 'rxjs';
|
|
6
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';
|
|
@@ -298,6 +298,21 @@ function markFieldForCheck(field) {
|
|
|
298
298
|
}
|
|
299
299
|
});
|
|
300
300
|
}
|
|
301
|
+
function isNoopNgZone(ngZone) {
|
|
302
|
+
return ngZone.constructor.name === 'NoopNgZone';
|
|
303
|
+
}
|
|
304
|
+
function isHiddenField(field) {
|
|
305
|
+
const isHidden = (f) => f.hide || f.expressions?.hide || f.hideExpression;
|
|
306
|
+
let setDefaultValue = !field.resetOnHide || !isHidden(field);
|
|
307
|
+
if (!isHidden(field) && field.resetOnHide) {
|
|
308
|
+
let parent = field.parent;
|
|
309
|
+
while (parent && !isHidden(parent)) {
|
|
310
|
+
parent = parent.parent;
|
|
311
|
+
}
|
|
312
|
+
setDefaultValue = !parent || !isHidden(parent);
|
|
313
|
+
}
|
|
314
|
+
return !setDefaultValue;
|
|
315
|
+
}
|
|
301
316
|
|
|
302
317
|
/**
|
|
303
318
|
* An InjectionToken for registering additional formly config options (types, wrappers ...).
|
|
@@ -548,6 +563,11 @@ class FormlyFormBuilder {
|
|
|
548
563
|
if (!field.parent || field.fieldArray) {
|
|
549
564
|
// detect changes early to avoid reset value by hidden fields
|
|
550
565
|
const options = field.options;
|
|
566
|
+
if (field.parent && isHiddenField(field)) {
|
|
567
|
+
// when hide is used in expression set defaul value will not be set until detect hide changes
|
|
568
|
+
// which causes default value not set on new item is added
|
|
569
|
+
options._hiddenFieldsForCheck?.push(field);
|
|
570
|
+
}
|
|
551
571
|
options.checkExpressions?.(field, true);
|
|
552
572
|
options._detectChanges?.(field);
|
|
553
573
|
}
|
|
@@ -1071,7 +1091,7 @@ class FormlyForm {
|
|
|
1071
1091
|
valueChanges() {
|
|
1072
1092
|
this.valueChangesUnsubscribe();
|
|
1073
1093
|
const sub = this.field.options.fieldChanges
|
|
1074
|
-
.pipe(filter(({ field, type }) => hasKey(field) && type === 'valueChanges'), switchMap(() => this.ngZone.onStable.asObservable().pipe(take(1))))
|
|
1094
|
+
.pipe(filter(({ field, type }) => hasKey(field) && type === 'valueChanges'), switchMap(() => (isNoopNgZone(this.ngZone) ? of(null) : this.ngZone.onStable.asObservable().pipe(take(1)))))
|
|
1075
1095
|
.subscribe(() => this.ngZone.runGuarded(() => {
|
|
1076
1096
|
// runGuarded is used to keep in sync the expression changes
|
|
1077
1097
|
// https://github.com/ngx-formly/ngx-formly/issues/2095
|
|
@@ -1706,7 +1726,7 @@ class FieldExpressionExtension {
|
|
|
1706
1726
|
changeHideState(field, hide, resetOnHide) {
|
|
1707
1727
|
if (field.fieldGroup) {
|
|
1708
1728
|
field.fieldGroup
|
|
1709
|
-
.filter((f) => !f._expressions.hide)
|
|
1729
|
+
.filter((f) => f && !f._expressions.hide)
|
|
1710
1730
|
.forEach((f) => this.changeHideState(f, hide, resetOnHide));
|
|
1711
1731
|
}
|
|
1712
1732
|
if (field.formControl && hasKey(field)) {
|
|
@@ -1993,6 +2013,12 @@ class FieldFormExtension {
|
|
|
1993
2013
|
control = new FormControl({ value, disabled: !!field.props.disabled }, { ...controlOptions, initialValueIsDefault: true });
|
|
1994
2014
|
}
|
|
1995
2015
|
}
|
|
2016
|
+
else {
|
|
2017
|
+
if (control instanceof FormControl) {
|
|
2018
|
+
const value = hasKey(field) ? getFieldValue(field) : field.defaultValue;
|
|
2019
|
+
control.defaultValue = value;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
1996
2022
|
registerControl(field, control);
|
|
1997
2023
|
}
|
|
1998
2024
|
setValidators(field, disabled = false) {
|
|
@@ -2188,19 +2214,11 @@ class CoreExtension {
|
|
|
2188
2214
|
if (field.type) {
|
|
2189
2215
|
this.config.getMergedField(field);
|
|
2190
2216
|
}
|
|
2191
|
-
if (hasKey(field) &&
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
while (parent && !isHidden(parent)) {
|
|
2197
|
-
parent = parent.parent;
|
|
2198
|
-
}
|
|
2199
|
-
setDefaultValue = !parent || !isHidden(parent);
|
|
2200
|
-
}
|
|
2201
|
-
if (setDefaultValue) {
|
|
2202
|
-
assignFieldValue(field, field.defaultValue);
|
|
2203
|
-
}
|
|
2217
|
+
if (hasKey(field) &&
|
|
2218
|
+
!isUndefined(field.defaultValue) &&
|
|
2219
|
+
isUndefined(getFieldValue(field)) &&
|
|
2220
|
+
!isHiddenField(field)) {
|
|
2221
|
+
assignFieldValue(field, field.defaultValue);
|
|
2204
2222
|
}
|
|
2205
2223
|
field.wrappers = field.wrappers || [];
|
|
2206
2224
|
}
|