Skip to content

Commit 4d6faaf

Browse files
committed
fix(ngClick): prevent soft keyboard from disappearing on input click
Currently ngClick calls blur on all elements and this causes keyboards on touch devices to bounce and sometimes not appear. This is a fix for angular#11358 and should allow input fields to keep the keyboard visible without bouncing in and out of view or never appearing. Closes angular#11358
1 parent 9e6a9b9 commit 4d6faaf

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/ngTouch/directive/ngClick.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
125125
return; // Too old.
126126
}
127127

128+
// Get the node name of the target
129+
var nodeName = nodeName_(event.target);
130+
128131
var touches = event.touches && event.touches.length ? event.touches : [event];
129132
var x = touches[0].clientX;
130133
var y = touches[0].clientY;
@@ -144,7 +147,7 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
144147
lastLabelClickCoordinates = null;
145148
}
146149
// remember label click coordinates to prevent click busting of trigger click event on input
147-
if (nodeName_(event.target) === 'label') {
150+
if (nodeName === 'label') {
148151
lastLabelClickCoordinates = [x, y];
149152
}
150153

@@ -159,8 +162,14 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
159162
event.stopPropagation();
160163
event.preventDefault();
161164

162-
// Blur focused form elements
163-
event.target && event.target.blur && event.target.blur();
165+
// Check if the target node is not of the type input.
166+
// All targets should be blurred except input to prevent the keyboard from
167+
// bouncing in and out and sometimes not showing.
168+
if (nodeName !== 'input' && nodeName !== 'textarea') {
169+
// Blur focused form elements
170+
event.target && event.target.blur && event.target.blur();
171+
}
172+
164173
}
165174

166175

@@ -238,6 +247,9 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
238247
element.on('touchend', function(event) {
239248
var diff = Date.now() - startTime;
240249

250+
// Get the node name of the tapElement
251+
var nodeName = nodeName_(tapElement);
252+
241253
// Use jQuery originalEvent
242254
var originalEvent = event.originalEvent || event;
243255
var touches = (originalEvent.changedTouches && originalEvent.changedTouches.length) ?
@@ -255,7 +267,9 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
255267
// Blur the focused element (the button, probably) before firing the callback.
256268
// This doesn't work perfectly on Android Chrome, but seems to work elsewhere.
257269
// I couldn't get anything to work reliably on Android Chrome.
258-
if (tapElement) {
270+
// Make sure the tapElement isn't of type input to prevent the keyboard from
271+
// bouncing and possibly not showing without a few attempted clicks.
272+
if (tapElement && nodeName !== 'input' && nodeName !== 'textarea') {
259273
tapElement.blur();
260274
}
261275

@@ -293,4 +307,3 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
293307

294308
};
295309
}]);
296-

0 commit comments

Comments
 (0)