Skip to content

Commit f9b46c2

Browse files
committed
fix(input): blur when tapping outside input on iOS
Closes #5020
1 parent 7fe3f80 commit f9b46c2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

ionic/components/input/native-input.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {CSS, hasFocus, raf} from '../../util/dom';
1414
export class NativeInput {
1515
private _relocated: boolean;
1616
private _clone: boolean;
17+
private _blurring: boolean;
18+
private _unrefBlur: Function;
1719

1820
@Output() focusChange: EventEmitter<boolean> = new EventEmitter();
1921
@Output() valueChange: EventEmitter<string> = new EventEmitter();
@@ -25,6 +27,7 @@ export class NativeInput {
2527
public ngControl: NgControl
2628
) {
2729
this._clone = config.getBoolean('inputCloning', false);
30+
this._blurring = config.getBoolean('inputBlurring', false);
2831
}
2932

3033
@HostListener('input', ['$event'])
@@ -34,13 +37,38 @@ export class NativeInput {
3437

3538
@HostListener('focus')
3639
private _focus() {
37-
this.focusChange.emit(true);
40+
var self = this;
41+
42+
self.focusChange.emit(true);
43+
44+
if (self._blurring) {
45+
// automatically blur input if:
46+
// 1) this input has focus
47+
// 2) the newly tapped document element is not an input
48+
console.debug('input blurring enabled');
49+
function docTouchEnd(ev) {
50+
var tappedElement: any = ev.target;
51+
if (tappedElement && self.element()) {
52+
if (tappedElement.tagName !== 'INPUT' && tappedElement.tagName !== 'TEXTAREA') {
53+
self.element().blur();
54+
}
55+
}
56+
}
57+
document.addEventListener('touchend', docTouchEnd, true);
58+
self._unrefBlur = function() {
59+
console.debug('input blurring disabled');
60+
document.removeEventListener('touchend', docTouchEnd, true);
61+
};
62+
}
3863
}
3964

4065
@HostListener('blur')
4166
private _blur() {
4267
this.focusChange.emit(false);
4368
this.hideFocus(false);
69+
70+
this._unrefBlur && this._unrefBlur();
71+
this._unrefBlur = null;
4472
}
4573

4674
labelledBy(val: string) {
@@ -138,6 +166,10 @@ export class NativeInput {
138166
return this._elementRef.nativeElement;
139167
}
140168

169+
ngOnDestroy() {
170+
this._unrefBlur && this._unrefBlur();
171+
}
172+
141173
}
142174

143175
function cloneInput(focusedInputEle, addCssClass) {

ionic/platform/registry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Platform.register({
9797
autoFocusAssist: 'delay',
9898
clickBlock: true,
9999
hoverCSS: false,
100+
inputBlurring: isIOSDevice,
100101
inputCloning: isIOSDevice,
101102
keyboardHeight: 300,
102103
mode: 'ios',

0 commit comments

Comments
 (0)