Skip to content

fix(autocomplete): return consistent output from panelClosingActions #8533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<MatOptionSelectionChange> {
get panelClosingActions(): Observable<MatOptionSelectionChange|null> {
return merge(
this.optionSelections,
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._overlayAttached)),
Expand All @@ -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)
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -1603,15 +1603,15 @@ 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', () => {
const escapeEvent = createKeyboardEvent('keydown', ESCAPE);

expect(closingActionSpy).not.toHaveBeenCalled();
trigger._handleKeydown(escapeEvent);
expect(closingActionSpy).toHaveBeenCalled();
expect(closingActionSpy).toHaveBeenCalledWith(null);
});
});

Expand Down