Skip to content

Commit 169bc62

Browse files
authored
fix(core): emit valueChanges when fieldArray length is decreasing (#3279)
fix #3250
1 parent 17c0bd6 commit 169bc62

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,34 @@ describe('Array Field Type', () => {
205205
subscription.unsubscribe();
206206
});
207207

208+
it('should emit `valueChanges` on model change', () => {
209+
app.fields = [
210+
{
211+
key: 'foo',
212+
type: 'array',
213+
fieldArray: { key: 'title' },
214+
},
215+
];
216+
217+
const fixture = createFormlyTestComponent();
218+
const spy = jasmine.createSpy('model change spy');
219+
const subscription = app.form.get('foo').valueChanges.subscribe(spy);
220+
221+
// add
222+
fixture.componentInstance.model = { foo: [{ title: 1 }] };
223+
fixture.detectChanges();
224+
225+
expect(spy).toHaveBeenCalledWith([{ title: 1 }]);
226+
227+
// remove
228+
spy.calls.reset();
229+
fixture.componentInstance.model = { foo: [] };
230+
fixture.detectChanges();
231+
232+
expect(spy).toHaveBeenCalledWith([]);
233+
subscription.unsubscribe();
234+
});
235+
208236
it('should not triggers valueChanges for all fields on add/remove', () => {
209237
app.fields = [
210238
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export abstract class FieldArrayType<F extends FormlyFieldConfig = FormlyFieldCo
4646
const length = field.model ? field.model.length : 0;
4747
if (field.fieldGroup.length > length) {
4848
for (let i = field.fieldGroup.length - 1; i >= length; --i) {
49-
unregisterControl(field.fieldGroup[i]);
49+
unregisterControl(field.fieldGroup[i], true);
5050
field.fieldGroup.splice(i, 1);
5151
}
5252
}

0 commit comments

Comments
 (0)