Skip to content

Commit

Permalink
fix(autocomplete): return consistent output from panelClosingActions
Browse files Browse the repository at this point in the history
Currently the `panelClosingActions` stream is typed to return a `MatOptionSelectionChange` event, however the real return data is `MatOptionSelectionChange|void|MouseEvent`, which makes it hard to use. These changes switch to emitting a `null` if nothing was selected or `MatOptionSelectionChange` if the user selected something.

Fixes angular#7553.
  • Loading branch information
crisbeto committed Mar 30, 2018
1 parent bcff93e commit 7d8bd7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
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

0 comments on commit 7d8bd7e

Please sign in to comment.