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

Commit 3f251ba

Browse files
author
Marcy Sutton
committed
bug(ngAria): native controls fire a single click event
1 parent e24d968 commit 3f251ba

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/ngAria/aria.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ function $AriaProvider() {
8383
ariaMultiline: true,
8484
ariaValue: true,
8585
tabindex: true,
86-
bindKeypress: true
86+
bindKeypress: true,
87+
keypressEls: ['DIV', 'LI']
8788
};
8889

8990
/**
@@ -303,11 +304,20 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
303304
compile: function(elem, attr) {
304305
var fn = $parse(attr.ngClick, /* interceptorFn */ null, /* expensiveChecks */ true);
305306
return function(scope, elem, attr) {
307+
308+
function isNodeOneOf(elem, nodeTypeArray) {
309+
for (var i=0; i < nodeTypeArray.length; i++) {
310+
if (elem[0].nodeName === nodeTypeArray[i]) {
311+
return true;
312+
}
313+
}
314+
}
315+
306316
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
307317
elem.attr('tabindex', 0);
308318
}
309319

310-
if ($aria.config('bindKeypress') && !attr.ngKeypress) {
320+
if ($aria.config('bindKeypress') && !attr.ngKeypress && isNodeOneOf(elem, $aria.config('keypressEls'))) {
311321
elem.on('keypress', function(event) {
312322
if (event.keyCode === 32 || event.keyCode === 13) {
313323
scope.$apply(callback);

test/ngAria/ariaSpec.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,16 @@ describe('$aria', function() {
496496
expect(clickFn).toHaveBeenCalled();
497497
});
498498

499+
it('should a trigger click from the keyboard', function() {
500+
scope.someAction = function() {};
501+
compileInput('<li ng-click="someAction()" tabindex="0"></li>');
502+
clickFn = spyOn(scope, 'someAction');
503+
504+
element.triggerHandler({type: 'keypress', keyCode: 32});
505+
506+
expect(clickFn).toHaveBeenCalled();
507+
});
508+
499509
it('should not override existing ng-keypress', function() {
500510
scope.someOtherAction = function() {};
501511
var keypressFn = spyOn(scope, 'someOtherAction');
@@ -528,6 +538,24 @@ describe('$aria', function() {
528538
});
529539
});
530540

541+
describe('actions for different configured elements', function() {
542+
beforeEach(configAriaProvider({
543+
keypressEls: ['DIV']
544+
}));
545+
beforeEach(injectScopeAndCompiler);
546+
547+
it('should not a trigger click from the keyboard', function() {
548+
scope.someAction = function() {};
549+
var clickFn = spyOn(scope, 'someAction');
550+
551+
element = $compile('<li ng-click="someAction()" tabindex="0"></li>')(scope);
552+
553+
element.triggerHandler({type: 'keypress', keyCode: 32});
554+
555+
expect(clickFn).not.toHaveBeenCalled();
556+
});
557+
});
558+
531559
describe('actions when bindKeypress set to false', function() {
532560
beforeEach(configAriaProvider({
533561
bindKeypress: false
@@ -538,7 +566,7 @@ describe('$aria', function() {
538566
scope.someAction = function() {};
539567
var clickFn = spyOn(scope, 'someAction');
540568

541-
element = $compile('<div ng-click="someAction()" tabindex="0">></div>')(scope);
569+
element = $compile('<div ng-click="someAction()" tabindex="0"></div>')(scope);
542570

543571
element.triggerHandler({type: 'keypress', keyCode: 32});
544572

0 commit comments

Comments
 (0)