Skip to content

Commit

Permalink
fix(json-schema): ignore resetOnHide for array typ on build
Browse files Browse the repository at this point in the history
fix #3910
  • Loading branch information
Abdellatif Ait Boudad committed May 12, 2024
1 parent 67c6783 commit fc8d6d1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,12 @@ export class FormlyJsonschema {
}

let field = schema._field;
let model = root.model ? clone(root.model) : root.fieldArray ? [] : {};
let model = root.model ? root.model : root.fieldArray ? [] : {};
if (root.model && hasKey(root)) {
model = { [Array.isArray(root.key) ? root.key.join('.') : root.key]: getFieldValue(root) };
}

model = clone(model);
if (!field) {
field = schema._field = root.options.build({
form: Array.isArray(model) ? new FormArray([]) : new FormGroup({}),
Expand Down
4 changes: 3 additions & 1 deletion src/core/src/lib/services/formly.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class FormlyFormBuilder {

disableTreeValidityCall(field.form, () => {
this._build(field);
if (!field.parent) {
// TODO: add test for https://github.com/ngx-formly/ngx-formly/issues/3910
if (!field.parent || (field as FormlyFieldConfigCache).fieldArray) {
// detect changes early to avoid reset value by hidden fields
const options = (field as FormlyFieldConfigCache).options;
options.checkExpressions?.(field, true);
options._detectChanges?.(field);
Expand Down
3 changes: 1 addition & 2 deletions src/core/src/lib/templates/field-array.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export abstract class FieldArrayType<F extends FormlyFieldConfig = FieldArrayTyp

private _build() {
const fields = (this.field as FormlyFieldConfigCache).formControl._fields ?? [this.field];
fields.forEach((f) => this.options.build(f));
this.field.options.detectChanges(this.field);
fields.forEach((f) => (this.options as any).build(f));
this.options.fieldChanges.next({
field: this.field,
value: getFieldValue(this.field),
Expand Down

0 comments on commit fc8d6d1

Please sign in to comment.