Skip to content

Commit 063c12c

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 541a95e commit 063c12c

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 {first} from 'rxjs/operators/first';
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
@@ -21,7 +21,7 @@ import {
2121
} from '@angular/core';
2222
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
2323
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
24-
import {MatOption} from '@angular/material/core';
24+
import {MatOption, MatOptionSelectionChange} from '@angular/material/core';
2525
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
2626
import {By} from '@angular/platform-browser';
2727
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
@@ -1361,7 +1361,7 @@ describe('MatAutocomplete', () => {
13611361
fixture.whenStable().then(() => {
13621362
expect(closingActionSpy).not.toHaveBeenCalled();
13631363
dispatchFakeEvent(document, 'click');
1364-
expect(closingActionSpy).toHaveBeenCalled();
1364+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13651365
});
13661366
}));
13671367

@@ -1372,7 +1372,7 @@ describe('MatAutocomplete', () => {
13721372
fixture.whenStable().then(() => {
13731373
expect(closingActionSpy).not.toHaveBeenCalled();
13741374
trigger._handleKeydown(tabEvent);
1375-
expect(closingActionSpy).toHaveBeenCalled();
1375+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13761376
});
13771377
}));
13781378

@@ -1382,7 +1382,7 @@ describe('MatAutocomplete', () => {
13821382

13831383
expect(closingActionSpy).not.toHaveBeenCalled();
13841384
option.click();
1385-
expect(closingActionSpy).toHaveBeenCalled();
1385+
expect(closingActionSpy).toHaveBeenCalledWith(jasmine.any(MatOptionSelectionChange));
13861386
});
13871387
}));
13881388

@@ -1392,7 +1392,7 @@ describe('MatAutocomplete', () => {
13921392
fixture.whenStable().then(() => {
13931393
expect(closingActionSpy).not.toHaveBeenCalled();
13941394
trigger._handleKeydown(escapeEvent);
1395-
expect(closingActionSpy).toHaveBeenCalled();
1395+
expect(closingActionSpy).toHaveBeenCalledWith(null);
13961396
});
13971397
}));
13981398
});

0 commit comments

Comments
 (0)