Skip to content

Commit

Permalink
fix(core): allow using FieldArray without a key (#2142)
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/core/src/lib/templates/field-array.type.spec.ts
  • Loading branch information
aitboudad committed Mar 17, 2020
1 parent 1b41aaa commit 32a3a9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/core/src/lib/templates/field-array.type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function createFormlyTestComponent() {
}

let app: Partial<{
form: FormGroup;
form: FormGroup | FormArray;
fields: FormlyFieldConfig[];
options: FormlyFormOptions;
model: any;
Expand Down Expand Up @@ -63,6 +63,25 @@ describe('Array Field Type', () => {
expect(app.form.dirty).toBeTruthy();
});

it('should support field without key', () => {
app.form = new FormArray([]);
app.fields = [{ type: 'array' }];
app.model = [];

const fixture = createFormlyTestComponent();
expect(app.model).toEqual([]);

fixture.nativeElement.querySelector('#add').click();
fixture.detectChanges();

expect(app.model).toEqual([undefined]);

fixture.nativeElement.querySelector('#remove-0').click();
fixture.detectChanges();

expect(app.model).toEqual([]);
});

it('should work with nullable model', () => {
app.model = { array: null };
app.fields = [{
Expand Down Expand Up @@ -174,7 +193,7 @@ class TestComponent {
@ViewChild(FormlyForm) formlyForm: FormlyForm;

fields = app.fields;
form: FormGroup = app.form;
form = app.form;
model = app.model;
options = app.options;
}
Expand All @@ -189,4 +208,4 @@ class TestComponent {
<button id="add" type="button" (click)="add()">Add</button>
`,
})
class ArrayTypeComponent extends FieldArrayType {}
class ArrayTypeComponent extends FieldArrayType {}
2 changes: 1 addition & 1 deletion src/core/src/lib/templates/field-array.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export abstract class FieldArrayType<F extends FormlyFieldConfig = FormlyFieldCo
}

onPopulate(field: FormlyFieldConfig) {
if (!field.formControl) {
if (!field.formControl && field.key) {
registerControl(field, new FormArray([], { updateOn: field.modelOptions.updateOn }));
}

Expand Down

0 comments on commit 32a3a9d

Please sign in to comment.