From f9b46c26ccb667e9ed38bc028405387b1ea166ff Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 14 Apr 2016 13:43:56 -0500 Subject: [PATCH] fix(input): blur when tapping outside input on iOS Closes #5020 --- ionic/components/input/native-input.ts | 34 +++++++++++++++++++++++++- ionic/platform/registry.ts | 1 + 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ionic/components/input/native-input.ts b/ionic/components/input/native-input.ts index 8c87d4e439c..7073a665e2b 100644 --- a/ionic/components/input/native-input.ts +++ b/ionic/components/input/native-input.ts @@ -14,6 +14,8 @@ import {CSS, hasFocus, raf} from '../../util/dom'; export class NativeInput { private _relocated: boolean; private _clone: boolean; + private _blurring: boolean; + private _unrefBlur: Function; @Output() focusChange: EventEmitter = new EventEmitter(); @Output() valueChange: EventEmitter = new EventEmitter(); @@ -25,6 +27,7 @@ export class NativeInput { public ngControl: NgControl ) { this._clone = config.getBoolean('inputCloning', false); + this._blurring = config.getBoolean('inputBlurring', false); } @HostListener('input', ['$event']) @@ -34,13 +37,38 @@ export class NativeInput { @HostListener('focus') private _focus() { - this.focusChange.emit(true); + var self = this; + + self.focusChange.emit(true); + + if (self._blurring) { + // automatically blur input if: + // 1) this input has focus + // 2) the newly tapped document element is not an input + console.debug('input blurring enabled'); + function docTouchEnd(ev) { + var tappedElement: any = ev.target; + if (tappedElement && self.element()) { + if (tappedElement.tagName !== 'INPUT' && tappedElement.tagName !== 'TEXTAREA') { + self.element().blur(); + } + } + } + document.addEventListener('touchend', docTouchEnd, true); + self._unrefBlur = function() { + console.debug('input blurring disabled'); + document.removeEventListener('touchend', docTouchEnd, true); + }; + } } @HostListener('blur') private _blur() { this.focusChange.emit(false); this.hideFocus(false); + + this._unrefBlur && this._unrefBlur(); + this._unrefBlur = null; } labelledBy(val: string) { @@ -138,6 +166,10 @@ export class NativeInput { return this._elementRef.nativeElement; } + ngOnDestroy() { + this._unrefBlur && this._unrefBlur(); + } + } function cloneInput(focusedInputEle, addCssClass) { diff --git a/ionic/platform/registry.ts b/ionic/platform/registry.ts index add3d50280d..f4d0a56c3cb 100644 --- a/ionic/platform/registry.ts +++ b/ionic/platform/registry.ts @@ -97,6 +97,7 @@ Platform.register({ autoFocusAssist: 'delay', clickBlock: true, hoverCSS: false, + inputBlurring: isIOSDevice, inputCloning: isIOSDevice, keyboardHeight: 300, mode: 'ios',