Skip to content

Commit

Permalink
fix(core): emit valueChanges when fieldArray length is decreasing (#3279
Browse files Browse the repository at this point in the history
)

fix #3250
  • Loading branch information
aitboudad committed Apr 22, 2022
1 parent 17c0bd6 commit 169bc62
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/core/src/lib/templates/field-array.type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,34 @@ describe('Array Field Type', () => {
subscription.unsubscribe();
});

it('should emit `valueChanges` on model change', () => {
app.fields = [
{
key: 'foo',
type: 'array',
fieldArray: { key: 'title' },
},
];

const fixture = createFormlyTestComponent();
const spy = jasmine.createSpy('model change spy');
const subscription = app.form.get('foo').valueChanges.subscribe(spy);

// add
fixture.componentInstance.model = { foo: [{ title: 1 }] };
fixture.detectChanges();

expect(spy).toHaveBeenCalledWith([{ title: 1 }]);

// remove
spy.calls.reset();
fixture.componentInstance.model = { foo: [] };
fixture.detectChanges();

expect(spy).toHaveBeenCalledWith([]);
subscription.unsubscribe();
});

it('should not triggers valueChanges for all fields on add/remove', () => {
app.fields = [
{
Expand Down
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 @@ -46,7 +46,7 @@ export abstract class FieldArrayType<F extends FormlyFieldConfig = FormlyFieldCo
const length = field.model ? field.model.length : 0;
if (field.fieldGroup.length > length) {
for (let i = field.fieldGroup.length - 1; i >= length; --i) {
unregisterControl(field.fieldGroup[i]);
unregisterControl(field.fieldGroup[i], true);
field.fieldGroup.splice(i, 1);
}
}
Expand Down

0 comments on commit 169bc62

Please sign in to comment.