Skip to content

Commit 1dcaca7

Browse files
crisbetojelbourn
authored andcommitted
fix(autocomplete): not closing when tapping away on mobile (#5260)
Currently the autocomplete panel won't close when tapping away on iOS, because the browser doesn't fire the click event on anything that it doesn't consider clickable. These changes add a `touchend` listener that will handle the functionality on mobile.
1 parent 8feddd4 commit 1dcaca7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
211211
/** Stream of clicks outside of the autocomplete panel. */
212212
private get _outsideClickStream(): Observable<any> {
213213
if (this._document) {
214-
return Observable.fromEvent(this._document, 'click').filter((event: MouseEvent) => {
214+
return Observable.merge(
215+
Observable.fromEvent(this._document, 'click'),
216+
Observable.fromEvent(this._document, 'touchend')
217+
).filter((event: MouseEvent | TouchEvent) => {
215218
const clickTarget = event.target as HTMLElement;
216219
const inputContainer = this._inputContainer ?
217220
this._inputContainer._elementRef.nativeElement : null;
@@ -442,4 +445,3 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
442445
}
443446

444447
}
445-

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('MdAutocomplete', () => {
146146
});
147147
}));
148148

149-
it('should close the panel when input loses focus', async(() => {
149+
it('should close the panel when the user clicks away', async(() => {
150150
dispatchFakeEvent(input, 'focusin');
151151
fixture.detectChanges();
152152

@@ -160,6 +160,20 @@ describe('MdAutocomplete', () => {
160160
});
161161
}));
162162

163+
it('should close the panel when the user taps away on a touch device', async(() => {
164+
dispatchFakeEvent(input, 'focus');
165+
fixture.detectChanges();
166+
167+
fixture.whenStable().then(() => {
168+
dispatchFakeEvent(document, 'touchend');
169+
170+
expect(fixture.componentInstance.trigger.panelOpen)
171+
.toBe(false, `Expected tapping outside the panel to set its state to closed.`);
172+
expect(overlayContainerElement.textContent)
173+
.toEqual('', `Expected tapping outside the panel to close the panel.`);
174+
});
175+
}));
176+
163177
it('should close the panel when an option is clicked', async(() => {
164178
dispatchFakeEvent(input, 'focusin');
165179
fixture.detectChanges();

0 commit comments

Comments
 (0)