Skip to content

Commit 6e63246

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 b488b39 commit 6e63246

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {take} from 'rxjs/operators/take';
2222
import {switchMap} from 'rxjs/operators/switchMap';
2323
import {tap} from 'rxjs/operators/tap';
2424
import {delay} from 'rxjs/operators/delay';
25+
import {map} from 'rxjs/operators/map';
2526
import {
2627
ChangeDetectorRef,
2728
Directive,
@@ -190,12 +191,15 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
190191
* A stream of actions that should close the autocomplete panel, including
191192
* when an option is selected, on blur, and when TAB is pressed.
192193
*/
193-
get panelClosingActions(): Observable<MatOptionSelectionChange> {
194+
get panelClosingActions(): Observable<MatOptionSelectionChange|null> {
194195
return merge(
195196
this.optionSelections,
196197
this.autocomplete._keyManager.tabOut,
197198
this._escapeEventStream,
198199
this._outsideClickStream
200+
).pipe(
201+
// Normalize the output so we return a consistent type.
202+
map(event => event instanceof MatOptionSelectionChange ? event : null)
199203
);
200204
}
201205

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
flush,
3232
} from '@angular/core/testing';
3333
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
34-
import {MatOption} from '@angular/material/core';
34+
import {MatOption, MatOptionSelectionChange} from '@angular/material/core';
3535
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
3636
import {By} from '@angular/platform-browser';
3737
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
@@ -1321,7 +1321,7 @@ describe('MatAutocomplete', () => {
13211321
it('should emit panel close event when clicking away', () => {
13221322
expect(closingActionSpy).not.toHaveBeenCalled();
13231323
dispatchFakeEvent(document, 'click');
1324-
expect(closingActionSpy).toHaveBeenCalled();
1324+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13251325
});
13261326

13271327
it('should emit panel close event when tabbing out', () => {
@@ -1330,23 +1330,23 @@ describe('MatAutocomplete', () => {
13301330

13311331
expect(closingActionSpy).not.toHaveBeenCalled();
13321332
trigger._handleKeydown(tabEvent);
1333-
expect(closingActionSpy).toHaveBeenCalled();
1333+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13341334
});
13351335

13361336
it('should emit panel close event when selecting an option', () => {
13371337
const option = overlayContainerElement.querySelector('mat-option') as HTMLElement;
13381338

13391339
expect(closingActionSpy).not.toHaveBeenCalled();
13401340
option.click();
1341-
expect(closingActionSpy).toHaveBeenCalled();
1341+
expect(closingActionSpy).toHaveBeenCalledWith(jasmine.any(MatOptionSelectionChange));
13421342
});
13431343

13441344
it('should close the panel when pressing escape', () => {
13451345
const escapeEvent = createKeyboardEvent('keydown', ESCAPE);
13461346

13471347
expect(closingActionSpy).not.toHaveBeenCalled();
13481348
trigger._handleKeydown(escapeEvent);
1349-
expect(closingActionSpy).toHaveBeenCalled();
1349+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13501350
});
13511351
});
13521352

0 commit comments

Comments
 (0)