Skip to content

Commit

Permalink
fix(core): ignore default debounce when using updateOn blur/submit (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad committed Nov 6, 2019
1 parent cb2ebda commit 2d73fb9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/core/src/lib/components/formly.form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ describe('FormlyForm Component', () => {
subscription.unsubscribe();
});

it('should ignore default debounce when using "blur" or "submit"', () => {
app.fields = [{
key: 'city',
type: 'text',
modelOptions: {
debounce: { default: 5 },
updateOn: 'blur',
},
}];

const fixture = createTestComponent('<formly-form [form]="form" [fields]="fields" [model]="model" [options]="options"></formly-form>');
const spy = jasmine.createSpy('model change spy');
const subscription = fixture.componentInstance.formlyForm.modelChange.subscribe(spy);

app.form.get('city').patchValue('***');

fixture.detectChanges();
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ city: '***' });
subscription.unsubscribe();
});

it('should emit `modelChange` after debounce time', fakeAsync(() => {
app.fields = [{
key: 'city',
Expand Down
10 changes: 7 additions & 3 deletions src/core/src/lib/components/formly.form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ export class FormlyForm implements DoCheck, OnChanges, OnDestroy {
fields.forEach(field => {
if (field.key && !field.fieldGroup) {
const control = field.formControl;
const valueChanges = field.modelOptions.debounce && field.modelOptions.debounce.default
? control.valueChanges.pipe(debounceTime(field.modelOptions.debounce.default))
: control.valueChanges;
let valueChanges = control.valueChanges;

const { updateOn, debounce } = field.modelOptions;
if ((!updateOn || updateOn === 'change') && debounce && debounce.default > 0) {
valueChanges = control.valueChanges.pipe(debounceTime(debounce.default));
}


this.modelChangeSubs.push(valueChanges.subscribe(value => {
// workaround for https://github.com/angular/angular/issues/13792
Expand Down

0 comments on commit 2d73fb9

Please sign in to comment.