Skip to content

Commit 5d71da1

Browse files
committed
fix(select): don't emit change event multiple times when a reset option is selected twice in a row
Fixes the select's change events being fired when a reset option is selected twice in a row. Fixes #10675.
1 parent 6a82c65 commit 5d71da1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/lib/select/select.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,40 @@ describe('MatSelect', () => {
27022702
expect(spy).toHaveBeenCalledWith('steak-0');
27032703
}));
27042704

2705+
it('should not emit the change event multiple times when a reset option is ' +
2706+
'selected twice in a row', fakeAsync(() => {
2707+
const fixture = TestBed.createComponent(BasicSelectWithoutForms);
2708+
const instance = fixture.componentInstance;
2709+
const spy = jasmine.createSpy('change spy');
2710+
2711+
instance.foods[0].value = null;
2712+
fixture.detectChanges();
2713+
2714+
const subscription = instance.select.selectionChange.subscribe(spy);
2715+
2716+
fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
2717+
fixture.detectChanges();
2718+
flush();
2719+
2720+
(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
2721+
fixture.detectChanges();
2722+
flush();
2723+
2724+
expect(spy).toHaveBeenCalledTimes(1);
2725+
2726+
fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
2727+
fixture.detectChanges();
2728+
flush();
2729+
2730+
(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
2731+
fixture.detectChanges();
2732+
flush();
2733+
2734+
expect(spy).toHaveBeenCalledTimes(1);
2735+
2736+
subscription.unsubscribe();
2737+
}));
2738+
27052739
});
27062740

27072741
describe('with option centering disabled', () => {

src/lib/select/select.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,11 +877,11 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
877877
} else {
878878
this._clearSelection(option.value == null ? undefined : option);
879879

880-
if (option.value == null) {
881-
this._propagateChanges(option.value);
882-
} else {
880+
if (option.value != null) {
883881
this._selectionModel.select(option);
884882
this.stateChanges.next();
883+
} else if (this.value !== option.value) {
884+
this._propagateChanges(option.value);
885885
}
886886
}
887887

0 commit comments

Comments
 (0)