Skip to content

Commit

Permalink
fix(autocomplete): not closing when tapping away on mobile
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 committed Jun 20, 2017
1 parent 1a03ee1 commit b78babe
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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 @@ -435,4 +438,3 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
}

}

16 changes: 15 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
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, 'focus');
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, 'focus');
fixture.detectChanges();
Expand Down

0 comments on commit b78babe

Please sign in to comment.