Skip to content

Commit 7d8bd7e

Browse files
committed
fix(autocomplete): return consistent output from panelClosingActions
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 #7553.
1 parent bcff93e commit 7d8bd7e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from '@angular/cdk/overlay';
1818
import {TemplatePortal} from '@angular/cdk/portal';
1919
import {DOCUMENT} from '@angular/common';
20-
import {filter, take, switchMap, delay, tap} from 'rxjs/operators';
20+
import {filter, take, switchMap, delay, tap, map} from 'rxjs/operators';
2121
import {
2222
ChangeDetectorRef,
2323
Directive,
@@ -196,7 +196,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
196196
* A stream of actions that should close the autocomplete panel, including
197197
* when an option is selected, on blur, and when TAB is pressed.
198198
*/
199-
get panelClosingActions(): Observable<MatOptionSelectionChange> {
199+
get panelClosingActions(): Observable<MatOptionSelectionChange|null> {
200200
return merge(
201201
this.optionSelections,
202202
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._overlayAttached)),
@@ -205,6 +205,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
205205
this._overlayRef ?
206206
this._overlayRef.detachments().pipe(filter(() => this._overlayAttached)) :
207207
observableOf()
208+
).pipe(
209+
// Normalize the output so we return a consistent type.
210+
map(event => event instanceof MatOptionSelectionChange ? event : null)
208211
);
209212
}
210213

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ describe('MatAutocomplete', () => {
15691569
it('should emit panel close event when clicking away', () => {
15701570
expect(closingActionSpy).not.toHaveBeenCalled();
15711571
dispatchFakeEvent(document, 'click');
1572-
expect(closingActionSpy).toHaveBeenCalled();
1572+
expect(closingActionSpy).toHaveBeenCalledWith(null);
15731573
});
15741574

15751575
it('should emit panel close event when tabbing out', () => {
@@ -1578,7 +1578,7 @@ describe('MatAutocomplete', () => {
15781578

15791579
expect(closingActionSpy).not.toHaveBeenCalled();
15801580
trigger._handleKeydown(tabEvent);
1581-
expect(closingActionSpy).toHaveBeenCalled();
1581+
expect(closingActionSpy).toHaveBeenCalledWith(null);
15821582
});
15831583

15841584
it('should not emit when tabbing away from a closed panel', () => {
@@ -1603,15 +1603,15 @@ describe('MatAutocomplete', () => {
16031603

16041604
expect(closingActionSpy).not.toHaveBeenCalled();
16051605
option.click();
1606-
expect(closingActionSpy).toHaveBeenCalled();
1606+
expect(closingActionSpy).toHaveBeenCalledWith(jasmine.any(MatOptionSelectionChange));
16071607
});
16081608

16091609
it('should close the panel when pressing escape', () => {
16101610
const escapeEvent = createKeyboardEvent('keydown', ESCAPE);
16111611

16121612
expect(closingActionSpy).not.toHaveBeenCalled();
16131613
trigger._handleKeydown(escapeEvent);
1614-
expect(closingActionSpy).toHaveBeenCalled();
1614+
expect(closingActionSpy).toHaveBeenCalledWith(null);
16151615
});
16161616
});
16171617

0 commit comments

Comments
 (0)