diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index 8ccea2c91341..890af325d894 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -17,7 +17,7 @@ import { } from '@angular/cdk/overlay'; import {TemplatePortal} from '@angular/cdk/portal'; import {DOCUMENT} from '@angular/common'; -import {filter, take, switchMap, delay, tap} from 'rxjs/operators'; +import {filter, take, switchMap, delay, tap, map} from 'rxjs/operators'; import { ChangeDetectorRef, Directive, @@ -196,7 +196,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy { * A stream of actions that should close the autocomplete panel, including * when an option is selected, on blur, and when TAB is pressed. */ - get panelClosingActions(): Observable { + get panelClosingActions(): Observable { return merge( this.optionSelections, this.autocomplete._keyManager.tabOut.pipe(filter(() => this._overlayAttached)), @@ -205,6 +205,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy { this._overlayRef ? this._overlayRef.detachments().pipe(filter(() => this._overlayAttached)) : observableOf() + ).pipe( + // Normalize the output so we return a consistent type. + map(event => event instanceof MatOptionSelectionChange ? event : null) ); } diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index d98576933023..f4956730693a 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -1569,7 +1569,7 @@ describe('MatAutocomplete', () => { it('should emit panel close event when clicking away', () => { expect(closingActionSpy).not.toHaveBeenCalled(); dispatchFakeEvent(document, 'click'); - expect(closingActionSpy).toHaveBeenCalled(); + expect(closingActionSpy).toHaveBeenCalledWith(null); }); it('should emit panel close event when tabbing out', () => { @@ -1578,7 +1578,7 @@ describe('MatAutocomplete', () => { expect(closingActionSpy).not.toHaveBeenCalled(); trigger._handleKeydown(tabEvent); - expect(closingActionSpy).toHaveBeenCalled(); + expect(closingActionSpy).toHaveBeenCalledWith(null); }); it('should not emit when tabbing away from a closed panel', () => { @@ -1603,7 +1603,7 @@ describe('MatAutocomplete', () => { expect(closingActionSpy).not.toHaveBeenCalled(); option.click(); - expect(closingActionSpy).toHaveBeenCalled(); + expect(closingActionSpy).toHaveBeenCalledWith(jasmine.any(MatOptionSelectionChange)); }); it('should close the panel when pressing escape', () => { @@ -1611,7 +1611,7 @@ describe('MatAutocomplete', () => { expect(closingActionSpy).not.toHaveBeenCalled(); trigger._handleKeydown(escapeEvent); - expect(closingActionSpy).toHaveBeenCalled(); + expect(closingActionSpy).toHaveBeenCalledWith(null); }); });