Skip to content

Commit 886dad9

Browse files
authored
fix(core): rely on __build__ to check expression instead of parentFormlyForm. (#751)
fix #745
1 parent e37ae81 commit 886dad9

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

src/core/src/components/formly.form.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ export class FormlyForm implements DoCheck, OnChanges {
8787
}
8888

8989
private checkExpressionChange() {
90-
// only eval expressions it's a root component
91-
if (!this.parentFormlyForm) {
92-
this.formlyExpression.checkFields(this.form, this.fields, this.model, this.options);
93-
}
90+
this.formlyExpression.checkFields(this.form, this.fields, this.model, this.options);
9491
}
9592

9693
private resetModel(model?: any) {

src/core/src/services/formly.form.builder.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ describe('FormlyFormBuilder service', () => {
4040
// first build
4141
builder.buildForm(form, fields, {}, {});
4242
expect(fields['__build__']).toBeTruthy();
43-
expect(fields[0].fieldGroup['__build__']).toBeTruthy();
43+
expect(fields[0].fieldGroup['__build__']).toBeFalsy();
44+
expect(fields[0].fieldGroup['__build_child__']).toBeTruthy();
4445
expect((<any> builder).formId).toEqual(2);
4546

4647
builder.buildForm(form, fields, {}, {});

src/core/src/services/formly.form.builder.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export class FormlyFormBuilder {
2121
return;
2222
}
2323

24+
this.markAsBuilded(fields);
2425
this._buildForm(form, fields, model, options);
2526
this.formlyFormExpression.checkFields(form, fields, model, options);
2627
}
2728

2829
private _buildForm(form: FormGroup | FormArray, fields: FormlyFieldConfig[] = [], model: any, options: FormlyFormOptions) {
29-
this.markAsBuilded(fields);
30-
3130
this.formId++;
31+
this.markAsBuilded(fields, true);
3232

3333
options = options || {};
3434
options.formState = options.formState || {};
@@ -316,10 +316,14 @@ export class FormlyFormBuilder {
316316

317317
/* to avoid rebuild fields */
318318
private isBuilded(fields: any): boolean {
319-
return fields && fields['__build__'];
319+
return fields && (!!fields['__build__'] || !!fields['__build_child__']);
320320
}
321321

322-
private markAsBuilded(fields: any) {
323-
fields['__build__'] = true;
322+
private markAsBuilded(fields: any, isChild = false) {
323+
if (this.isBuilded(fields)) {
324+
return;
325+
}
326+
327+
fields[`__build${isChild ? '_child' : ''}__`] = true;
324328
}
325329
}

src/core/src/templates/field-array.type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export abstract class FieldArrayType extends FieldType {
3838

3939
remove(i) {
4040
this.formControl.removeAt(i);
41-
this.model.splice(i, 1);
4241
this.field.fieldGroup.splice(i, 1);
42+
this.field.fieldGroup.forEach((f, index) => f.key = `${index}`);
43+
this.model.splice(i, 1);
4344
}
4445
}

src/core/src/utils.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ describe('FormlyUtils service', () => {
108108
it('should attach the key path to the field config', () => {
109109
let fieldConfig = {key: 'property1.property2[4].property3'};
110110
getKeyPath(fieldConfig);
111-
expect(fieldConfig['_formlyKeyPath']).toEqual(['property1', 'property2', 4, 'property3']);
111+
expect(fieldConfig['_formlyKeyPath'].path).toEqual(['property1', 'property2', 4, 'property3']);
112+
});
113+
114+
it('should refresh formlyKeyPath on key updated', () => {
115+
const fieldConfig = { key: 'property1.property2[4].property3' };
116+
expect(getKeyPath(fieldConfig)).toEqual(['property1', 'property2', 4, 'property3']);
117+
118+
fieldConfig.key = 'ddd';
119+
expect(getKeyPath(fieldConfig)).toEqual(['ddd']);
112120
});
113121

114122
});

src/core/src/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function getFieldId(formId: string, options: FormlyFieldConfig, index: st
99

1010
export function getKeyPath(field: {key?: string|string[], fieldGroup?: any, fieldArray?: any}): (string|number)[] {
1111
/* We store the keyPath in the field for performance reasons. This function will be called frequently. */
12-
if (!(<any> field)['_formlyKeyPath']) {
12+
if (!(<any> field)['_formlyKeyPath'] || (<any> field)['_formlyKeyPath'].key !== field.key) {
1313
let keyPath: (string|number)[] = [];
1414
if (field.key) {
1515
/* Also allow for an array key, hence the type check */
@@ -30,10 +30,13 @@ export function getKeyPath(field: {key?: string|string[], fieldGroup?: any, fiel
3030
}
3131
}
3232
}
33-
(<any> field)['_formlyKeyPath'] = keyPath;
33+
(<any> field)['_formlyKeyPath'] = {
34+
key: field.key,
35+
path: keyPath,
36+
};
3437
}
3538

36-
return (<any> field)['_formlyKeyPath'].slice(0);
39+
return (<any> field)['_formlyKeyPath'].path.slice(0);
3740
}
3841

3942
function stringIsInteger(str: string) {

0 commit comments

Comments
 (0)