Skip to content

Commit bffa4b3

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 5414480 commit bffa4b3

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
@@ -21,6 +21,7 @@ import {take} from 'rxjs/operators/take';
2121
import {switchMap} from 'rxjs/operators/switchMap';
2222
import {tap} from 'rxjs/operators/tap';
2323
import {delay} from 'rxjs/operators/delay';
24+
import {map} from 'rxjs/operators/map';
2425
import {
2526
ChangeDetectorRef,
2627
Directive,
@@ -189,7 +190,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
189190
* A stream of actions that should close the autocomplete panel, including
190191
* when an option is selected, on blur, and when TAB is pressed.
191192
*/
192-
get panelClosingActions(): Observable<MatOptionSelectionChange> {
193+
get panelClosingActions(): Observable<MatOptionSelectionChange|null> {
193194
return merge(
194195
this.optionSelections,
195196
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._panelOpen)),
@@ -198,6 +199,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
198199
this._overlayRef ?
199200
this._overlayRef.detachments().pipe(filter(() => this._panelOpen)) :
200201
observableOf()
202+
).pipe(
203+
// Normalize the output so we return a consistent type.
204+
map(event => event instanceof MatOptionSelectionChange ? event : null)
201205
);
202206
}
203207

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';
@@ -1340,7 +1340,7 @@ describe('MatAutocomplete', () => {
13401340
it('should emit panel close event when clicking away', () => {
13411341
expect(closingActionSpy).not.toHaveBeenCalled();
13421342
dispatchFakeEvent(document, 'click');
1343-
expect(closingActionSpy).toHaveBeenCalled();
1343+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13441344
});
13451345

13461346
it('should emit panel close event when tabbing out', () => {
@@ -1349,7 +1349,7 @@ describe('MatAutocomplete', () => {
13491349

13501350
expect(closingActionSpy).not.toHaveBeenCalled();
13511351
trigger._handleKeydown(tabEvent);
1352-
expect(closingActionSpy).toHaveBeenCalled();
1352+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13531353
});
13541354

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

13751375
expect(closingActionSpy).not.toHaveBeenCalled();
13761376
option.click();
1377-
expect(closingActionSpy).toHaveBeenCalled();
1377+
expect(closingActionSpy).toHaveBeenCalledWith(jasmine.any(MatOptionSelectionChange));
13781378
});
13791379

13801380
it('should close the panel when pressing escape', () => {
13811381
const escapeEvent = createKeyboardEvent('keydown', ESCAPE);
13821382

13831383
expect(closingActionSpy).not.toHaveBeenCalled();
13841384
trigger._handleKeydown(escapeEvent);
1385-
expect(closingActionSpy).toHaveBeenCalled();
1385+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13861386
});
13871387
});
13881388

0 commit comments

Comments
 (0)