Skip to content

Commit

Permalink
fix(core): clear value on hide for array type (#2522)
Browse files Browse the repository at this point in the history
fix #2480
  • Loading branch information
aitboudad committed Sep 26, 2020
1 parent a8fe97d commit 49b3775
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/core/src/lib/components/formly.form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ describe('FormlyForm Component', () => {
];

const { model, fields } = fixture.componentInstance;
fixture.detectChanges();
expect(model).toEqual({ foo: [null] });
expect(fields[0].fieldGroup.length).toEqual(1);

fields[0].hide = true;
fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { evalExpression, evalStringExpression } from './utils';
import { Observable, Subscription } from 'rxjs';
import { FormlyExtension } from '../../services/formly.config';
import { unregisterControl, registerControl, updateValidity } from '../field-form/utils';
import { FormArray } from '@angular/forms';

/** @experimental */
export class FieldExpressionExtension implements FormlyExtension {
Expand Down Expand Up @@ -228,13 +229,17 @@ export class FieldExpressionExtension implements FormlyExtension {
unregisterControl(field);
if (resetOnHide && field['autoClear']) {
field.formControl.reset({ value: undefined, disabled: field.formControl.disabled });
if (field.formControl instanceof FormArray) {
assignFieldValue(field, undefined);
field.fieldGroup.length = 0;
}
}
} else if (hide === false) {
if (field['autoClear'] && field.parent && !isUndefined(field.defaultValue) && isUndefined(getFieldValue(field))) {
assignFieldValue(field, field.defaultValue);
}
registerControl(field);
if (field.fieldArray && (field.fieldGroup || []).length !== (field.model || []).length) {
if (field['autoClear'] && field.fieldArray && (field.fieldGroup || []).length !== (field.model || []).length) {
(<any> field.options)._buildForm(true);
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/core/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,20 @@ export function assignFieldValue(field: FormlyFieldConfigCache, value: any) {
return;
}

let root = field;
while (root.parent) {
root = root.parent;
paths = [...getKeyPath(root), ...paths];
}

if (value === undefined && field['autoClear']) {
const k = paths.pop();
const m = paths.reduce((model, path) => model[path] || {}, field.parent.model);
const m = paths.reduce((model, path) => model[path] || {}, root.model);
delete m[k];
return;
}

while (field.parent) {
field = field.parent;
paths = [...getKeyPath(field), ...paths];
}

assignModelValue(field.model, paths, value);
assignModelValue(root.model, paths, value);
}

export function assignModelValue(model: any, paths: string[], value: any) {
Expand Down

0 comments on commit 49b3775

Please sign in to comment.