Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngTouch's ngClick): add 'ng-touch' attribute to prevent bluring #7231

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/ngTouch/directive/ngClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,15 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
event.stopPropagation();
event.preventDefault();

// Blur focused form elements
event.target && event.target.blur();
// Blur focused form elements only if the user did not specify to keep it.
// Specified by adding the attribute 'ng-touch' with the value 'keep-focus'.
if (!event.target.attributes['ng-touch']) {
event.target && event.target.blur();
} else {
if (event.target.attributes['ng-touch'].nodeValue !== 'keep-focus') {
event.target && event.target.blur();
}
}
}


Expand Down
86 changes: 86 additions & 0 deletions test/ngTouch/directive/ngClickSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,5 +600,91 @@ describe('ngClick (touch)', function() {

expect(called).toEqual(true);
}));

it('should remove focus by default', inject(function($rootScope, $compile, $rootElement, $document) {
$document.find('body').append($rootElement);
$rootElement.append(
'<input ng-click="count = count + 1" type="text" >'
);
$compile($rootElement)($rootScope);

element = $rootElement.find('input').eq(0);

$rootScope.count = 0;

$rootScope.$digest();

expect($rootScope.count).toBe(0);

time = 10;
browserTrigger(element, 'touchstart',{
keys: [],
x: 10,
y: 10
});

time = 50;
browserTrigger(element, 'touchend',{
keys: [],
x: 10,
y: 10
});

expect($rootScope.count).toBe(1);

time = 90;
// Verify that it is blured so we don't get soft-keyboard
element[0].blur = jasmine.createSpy('blur');
browserTrigger(element, 'click',{
keys: [],
x: 10,
y: 10
});
expect(element[0].blur).toHaveBeenCalled();
$document.find('body').empty();
}));

it('should keep focus if it is specified', inject(function($rootScope, $compile, $rootElement, $document) {
$document.find('body').append($rootElement);
$rootElement.append(
'<input ng-click="count = count + 1" type="text" ng-touch="keep-focus" >'
);
$compile($rootElement)($rootScope);

element = $rootElement.find('input').eq(0);

$rootScope.count = 0;

$rootScope.$digest();

expect($rootScope.count).toBe(0);

time = 10;
browserTrigger(element, 'touchstart',{
keys: [],
x: 10,
y: 10
});

time = 50;
browserTrigger(element, 'touchend',{
keys: [],
x: 10,
y: 10
});

expect($rootScope.count).toBe(1);

time = 90;
// Verify that it is not blured so we do get soft-keyboard
element[0].blur = jasmine.createSpy('blur');
browserTrigger(element, 'click',{
keys: [],
x: 10,
y: 10
});
expect(element[0].blur).not.toHaveBeenCalled();
$document.find('body').empty();
}));
});
});