Skip to content

Commit 9c6dfd6

Browse files
crisbetoannieyw
authored andcommitted
fix(material/autocomplete): not closing when clicking outside using auxilliary button (#21397)
Fixes that the autocomplete panel doesn't close when the user presses outside using the middle or right mouse button. (cherry picked from commit bed1a6f)
1 parent 421a849 commit 9c6dfd6

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/material-experimental/mdc-autocomplete/autocomplete.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ describe('MDC-based MatAutocomplete', () => {
187187
.toEqual('', `Expected clicking outside the panel to close the panel.`);
188188
}));
189189

190+
it('should close the panel when the user clicks away via auxilliary button', fakeAsync(() => {
191+
dispatchFakeEvent(input, 'focusin');
192+
fixture.detectChanges();
193+
zone.simulateZoneExit();
194+
dispatchFakeEvent(document, 'auxclick');
195+
196+
expect(fixture.componentInstance.trigger.panelOpen)
197+
.toBe(false, `Expected clicking outside the panel to set its state to closed.`);
198+
expect(overlayContainerElement.textContent)
199+
.toEqual('', `Expected clicking outside the panel to close the panel.`);
200+
}));
201+
190202
it('should close the panel when the user taps away on a touch device', fakeAsync(() => {
191203
dispatchFakeEvent(input, 'focus');
192204
fixture.detectChanges();

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
349349
private _getOutsideClickStream(): Observable<any> {
350350
return merge(
351351
fromEvent(this._document, 'click') as Observable<MouseEvent>,
352+
fromEvent(this._document, 'auxclick') as Observable<MouseEvent>,
352353
fromEvent(this._document, 'touchend') as Observable<TouchEvent>)
353354
.pipe(filter(event => {
354355
// If we're in the Shadow DOM, the event target will be the shadow root, so we have to

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ describe('MatAutocomplete', () => {
188188
.toEqual('', `Expected clicking outside the panel to close the panel.`);
189189
}));
190190

191+
it('should close the panel when the user clicks away via auxilliary button', fakeAsync(() => {
192+
dispatchFakeEvent(input, 'focusin');
193+
fixture.detectChanges();
194+
zone.simulateZoneExit();
195+
dispatchFakeEvent(document, 'auxclick');
196+
197+
expect(fixture.componentInstance.trigger.panelOpen)
198+
.toBe(false, `Expected clicking outside the panel to set its state to closed.`);
199+
expect(overlayContainerElement.textContent)
200+
.toEqual('', `Expected clicking outside the panel to close the panel.`);
201+
}));
202+
191203
it('should close the panel when the user taps away on a touch device', fakeAsync(() => {
192204
dispatchFakeEvent(input, 'focus');
193205
fixture.detectChanges();

0 commit comments

Comments
 (0)