Skip to content

Commit

Permalink
fix(autocomplete): not closing when tapping away on mobile (#5260)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and jelbourn committed Jun 22, 2017
1 parent 8feddd4 commit 1dcaca7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/lib/autocomplete/autocomplete-trigger.ts
Expand Up @@ -211,7 +211,10 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
/** Stream of clicks outside of the autocomplete panel. */
private get _outsideClickStream(): Observable<any> {
if (this._document) {
return Observable.fromEvent(this._document, 'click').filter((event: MouseEvent) => {
return Observable.merge(
Observable.fromEvent(this._document, 'click'),
Observable.fromEvent(this._document, 'touchend')
).filter((event: MouseEvent | TouchEvent) => {
const clickTarget = event.target as HTMLElement;
const inputContainer = this._inputContainer ?
this._inputContainer._elementRef.nativeElement : null;
Expand Down Expand Up @@ -442,4 +445,3 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
}

}

16 changes: 15 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Expand Up @@ -146,7 +146,7 @@ describe('MdAutocomplete', () => {
});
}));

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

Expand All @@ -160,6 +160,20 @@ describe('MdAutocomplete', () => {
});
}));

it('should close the panel when the user taps away on a touch device', async(() => {
dispatchFakeEvent(input, 'focus');
fixture.detectChanges();

fixture.whenStable().then(() => {
dispatchFakeEvent(document, 'touchend');

expect(fixture.componentInstance.trigger.panelOpen)
.toBe(false, `Expected tapping outside the panel to set its state to closed.`);
expect(overlayContainerElement.textContent)
.toEqual('', `Expected tapping outside the panel to close the panel.`);
});
}));

it('should close the panel when an option is clicked', async(() => {
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
Expand Down

0 comments on commit 1dcaca7

Please sign in to comment.