Skip to content

Commit 2815607

Browse files
crisbetotinayuangao
authored andcommitted
fix(autocomplete): return consistent output from panelClosingActions (#8533)
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 afa6fa6 commit 2815607

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
@@ -18,7 +18,7 @@ import {
1818
} from '@angular/cdk/overlay';
1919
import {TemplatePortal} from '@angular/cdk/portal';
2020
import {DOCUMENT} from '@angular/common';
21-
import {filter, take, switchMap, delay, tap} from 'rxjs/operators';
21+
import {filter, take, switchMap, delay, tap, map} from 'rxjs/operators';
2222
import {
2323
ChangeDetectorRef,
2424
Directive,
@@ -226,7 +226,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
226226
* A stream of actions that should close the autocomplete panel, including
227227
* when an option is selected, on blur, and when TAB is pressed.
228228
*/
229-
get panelClosingActions(): Observable<MatOptionSelectionChange> {
229+
get panelClosingActions(): Observable<MatOptionSelectionChange|null> {
230230
return merge(
231231
this.optionSelections,
232232
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._overlayAttached)),
@@ -235,6 +235,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
235235
this._overlayRef ?
236236
this._overlayRef.detachments().pipe(filter(() => this._overlayAttached)) :
237237
observableOf()
238+
).pipe(
239+
// Normalize the output so we return a consistent type.
240+
map(event => event instanceof MatOptionSelectionChange ? event : null)
238241
);
239242
}
240243

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ describe('MatAutocomplete', () => {
16151615
it('should emit panel close event when clicking away', () => {
16161616
expect(closingActionSpy).not.toHaveBeenCalled();
16171617
dispatchFakeEvent(document, 'click');
1618-
expect(closingActionSpy).toHaveBeenCalled();
1618+
expect(closingActionSpy).toHaveBeenCalledWith(null);
16191619
});
16201620

16211621
it('should emit panel close event when tabbing out', () => {
@@ -1624,7 +1624,7 @@ describe('MatAutocomplete', () => {
16241624

16251625
expect(closingActionSpy).not.toHaveBeenCalled();
16261626
trigger._handleKeydown(tabEvent);
1627-
expect(closingActionSpy).toHaveBeenCalled();
1627+
expect(closingActionSpy).toHaveBeenCalledWith(null);
16281628
});
16291629

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

16501650
expect(closingActionSpy).not.toHaveBeenCalled();
16511651
option.click();
1652-
expect(closingActionSpy).toHaveBeenCalled();
1652+
expect(closingActionSpy).toHaveBeenCalledWith(jasmine.any(MatOptionSelectionChange));
16531653
});
16541654

16551655
it('should close the panel when pressing escape', () => {
16561656
const escapeEvent = createKeyboardEvent('keydown', ESCAPE);
16571657

16581658
expect(closingActionSpy).not.toHaveBeenCalled();
16591659
trigger._handleKeydown(escapeEvent);
1660-
expect(closingActionSpy).toHaveBeenCalled();
1660+
expect(closingActionSpy).toHaveBeenCalledWith(null);
16611661
});
16621662
});
16631663

0 commit comments

Comments
 (0)