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

Commit 5481e2c

Browse files
Marcy Suttoncaitp
Marcy Sutton
authored andcommitted
feat(ngAria): bind keypress on ng-click w/ option
Closes #10288
1 parent c6b57f1 commit 5481e2c

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

src/ngAria/aria.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* | {@link ng.directive:ngDisabled ngDisabled} | aria-disabled |
2727
* | {@link ng.directive:ngShow ngShow} | aria-hidden |
2828
* | {@link ng.directive:ngHide ngHide} | aria-hidden |
29-
* | {@link ng.directive:ngClick ngClick} | tabindex |
29+
* | {@link ng.directive:ngClick ngClick} | tabindex, keypress event |
3030
* | {@link ng.directive:ngDblclick ngDblclick} | tabindex |
3131
* | {@link module:ngMessages ngMessages} | aria-live |
3232
*
@@ -82,7 +82,8 @@ function $AriaProvider() {
8282
ariaInvalid: true,
8383
ariaMultiline: true,
8484
ariaValue: true,
85-
tabindex: true
85+
tabindex: true,
86+
bindKeypress: true
8687
};
8788

8889
/**
@@ -99,6 +100,7 @@ function $AriaProvider() {
99100
* - **ariaMultiline** – `{boolean}` – Enables/disables aria-multiline tags
100101
* - **ariaValue** – `{boolean}` – Enables/disables aria-valuemin, aria-valuemax and aria-valuenow tags
101102
* - **tabindex** – `{boolean}` – Enables/disables tabindex tags
103+
* - **bindKeypress** – `{boolean}` – Enables/disables keypress event binding on ng-click
102104
*
103105
* @description
104106
* Enables/disables various ARIA attributes
@@ -183,13 +185,6 @@ function $AriaProvider() {
183185
};
184186
}
185187

186-
var ngAriaTabindex = ['$aria', function($aria) {
187-
return function(scope, elem, attr) {
188-
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
189-
elem.attr('tabindex', 0);
190-
}
191-
};
192-
}];
193188

194189
ngAriaModule.directive('ngShow', ['$aria', function($aria) {
195190
return $aria.$$watchExpr('ngShow', 'aria-hidden', true);
@@ -309,5 +304,28 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
309304
}
310305
};
311306
})
312-
.directive('ngClick', ngAriaTabindex)
313-
.directive('ngDblclick', ngAriaTabindex);
307+
.directive('ngClick',['$aria', function($aria) {
308+
return {
309+
restrict: 'A',
310+
link: function(scope, elem, attr) {
311+
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
312+
elem.attr('tabindex', 0);
313+
}
314+
315+
if ($aria.config('bindKeypress') && !elem.attr('ng-keypress')) {
316+
elem.on('keypress', function(event) {
317+
if (event.keyCode === 32 || event.keyCode === 13) {
318+
scope.$eval(attr.ngClick);
319+
}
320+
});
321+
}
322+
}
323+
};
324+
}])
325+
.directive('ngDblclick', ['$aria', function($aria) {
326+
return function(scope, elem, attr) {
327+
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
328+
elem.attr('tabindex', 0);
329+
}
330+
};
331+
}]);

test/ngAria/ariaSpec.js

+48
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,54 @@ describe('$aria', function() {
481481
});
482482
});
483483

484+
describe('accessible actions', function() {
485+
beforeEach(injectScopeAndCompiler);
486+
487+
var clickFn;
488+
489+
it('should a trigger click from the keyboard', function() {
490+
scope.someAction = function() {};
491+
compileInput('<div ng-click="someAction()" tabindex="0"></div>');
492+
clickFn = spyOn(scope, 'someAction');
493+
494+
element.triggerHandler({type: 'keypress', keyCode: 32});
495+
496+
expect(clickFn).toHaveBeenCalled();
497+
});
498+
499+
it('should not override existing ng-keypress', function() {
500+
scope.someOtherAction = function() {};
501+
var keypressFn = spyOn(scope, 'someOtherAction');
502+
503+
scope.someAction = function() {};
504+
clickFn = spyOn(scope, 'someAction');
505+
compileInput('<div ng-click="someAction()" ng-keypress="someOtherAction()" tabindex="0"></div>');
506+
507+
element.triggerHandler({type: 'keypress', keyCode: 32});
508+
509+
expect(clickFn).not.toHaveBeenCalled();
510+
expect(keypressFn).toHaveBeenCalled();
511+
});
512+
});
513+
514+
describe('actions when bindKeypress set to false', function() {
515+
beforeEach(configAriaProvider({
516+
bindKeypress: false
517+
}));
518+
beforeEach(injectScopeAndCompiler);
519+
520+
it('should not a trigger click from the keyboard', function() {
521+
scope.someAction = function() {};
522+
var clickFn = spyOn(scope, 'someAction');
523+
524+
element = $compile('<div ng-click="someAction()" tabindex="0">></div>')(scope);
525+
526+
element.triggerHandler({type: 'keypress', keyCode: 32});
527+
528+
expect(clickFn).not.toHaveBeenCalled();
529+
});
530+
});
531+
484532
describe('tabindex when disabled', function() {
485533
beforeEach(configAriaProvider({
486534
tabindex: false

0 commit comments

Comments
 (0)