Skip to content

Commit efe4c46

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 aa09628 commit efe4c46

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/lib/select/select.spec.ts

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

2820+
it('should not emit the change event multiple times when a reset option is ' +
2821+
'selected twice in a row', fakeAsync(() => {
2822+
const fixture = TestBed.createComponent(BasicSelectWithoutForms);
2823+
const instance = fixture.componentInstance;
2824+
const spy = jasmine.createSpy('change spy');
2825+
2826+
instance.foods[0].value = null;
2827+
fixture.detectChanges();
2828+
2829+
const subscription = instance.select.selectionChange.subscribe(spy);
2830+
2831+
fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
2832+
fixture.detectChanges();
2833+
flush();
2834+
2835+
(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
2836+
fixture.detectChanges();
2837+
flush();
2838+
2839+
expect(spy).not.toHaveBeenCalled();
2840+
2841+
fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click();
2842+
fixture.detectChanges();
2843+
flush();
2844+
2845+
(overlayContainerElement.querySelector('mat-option') as HTMLElement).click();
2846+
fixture.detectChanges();
2847+
flush();
2848+
2849+
expect(spy).not.toHaveBeenCalled();
2850+
2851+
subscription.unsubscribe();
2852+
}));
2853+
28202854
});
28212855

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

src/lib/select/select.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
880880
private _onSelect(option: MatOption, isUserInput: boolean): void {
881881
const wasSelected = this._selectionModel.isSelected(option);
882882

883-
if (option.value == null && !this._multiple) {
884-
option.deselect();
885-
this._selectionModel.clear();
886-
this._propagateChanges(option.value);
887-
} else {
883+
if (option.value != null || this._multiple) {
888884
option.selected ? this._selectionModel.select(option) : this._selectionModel.deselect(option);
889885

890886
if (this.multiple) {
@@ -900,6 +896,10 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
900896
this.focus();
901897
}
902898
}
899+
} else if (option.value == null && this.value != option.value) {
900+
option.deselect();
901+
this._selectionModel.clear();
902+
this._propagateChanges(option.value);
903903
}
904904

905905
if (wasSelected !== this._selectionModel.isSelected(option)) {

0 commit comments

Comments
 (0)