Skip to content

Commit 360a28f

Browse files
committed
fix(autocomplete): not closing when tapping away on mobile
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 49dfe60 commit 360a28f

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
@@ -206,7 +206,10 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
206206
/** Stream of clicks outside of the autocomplete panel. */
207207
private get _outsideClickStream(): Observable<any> {
208208
if (this._document) {
209-
return Observable.fromEvent(this._document, 'click').filter((event: MouseEvent) => {
209+
return Observable.merge(
210+
Observable.fromEvent(this._document, 'click'),
211+
Observable.fromEvent(this._document, 'touchend')
212+
).filter((event: MouseEvent | TouchEvent) => {
210213
const clickTarget = event.target as HTMLElement;
211214
const inputContainer = this._inputContainer ?
212215
this._inputContainer._elementRef.nativeElement : null;
@@ -435,4 +438,3 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
435438
}
436439

437440
}
438-

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, 'focus');
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, 'focus');
165179
fixture.detectChanges();

0 commit comments

Comments
 (0)