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

Commit 9d53e5a

Browse files
Marcy Suttonpetebacondarwin
Marcy Sutton
authored andcommitted
fix(ngAria): ensure native controls fire a single click
Closes #10388 Closes #10766
1 parent 030a42e commit 9d53e5a

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

docs/content/guide/accessibility.ngdoc

+9-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Currently, ngAria interfaces with the following directives:
3737
* {@link guide/accessibility#nghide ngHide}
3838
* {@link guide/accessibility#ngclick ngClick}
3939
* {@link guide/accessibility#ngdblclick ngDblClick}
40+
* {@link guide/accessibility#ngmessages ngMessages}
4041

4142
<h2 id="ngmodel">ngModel</h2>
4243

@@ -209,10 +210,14 @@ The default CSS for `ngHide`, the inverse method to `ngShow`, makes ngAria redun
209210
`display: none`. See explanation for {@link guide/accessibility#ngshow ngShow} when overriding the default CSS.
210211

211212
<h2><span id="ngclick">ngClick</span> and <span id="ngdblclick">ngDblclick</span></h2>
212-
If `ng-click` or `ng-dblclick` is encountered, ngAria will add `tabindex` if it isn't there already.
213-
Even with this, you must currently still add `ng-keypress` to non-interactive elements such as `div`
214-
or `taco-button` to enable keyboard access. Conversation is currently ongoing about whether ngAria
215-
should also bind `ng-keypress`.
213+
If `ng-click` or `ng-dblclick` is encountered, ngAria will add `tabindex="0"` if it isn't there
214+
already.
215+
216+
For `ng-click`, keypress will also be bound to `div` and `li` elements. You can turn this
217+
functionality on or off with the `bindKeypress` configuration option.
218+
219+
For `ng-dblclick`, you must manually add `ng-keypress` to non-interactive elements such as `div`
220+
or `taco-button` to enable keyboard access.
216221

217222
<h3>Example</h3>
218223
```html
@@ -223,7 +228,6 @@ Becomes:
223228
```html
224229
<div ng-click="toggleMenu()" tabindex="0"></div>
225230
```
226-
*Note: ngAria still requires `ng-keypress` to be added manually to non-native controls like divs.*
227231

228232
<h2 id="ngmessages">ngMessages</h2>
229233

src/ngAria/aria.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ function $AriaProvider() {
100100
* - **ariaMultiline** – `{boolean}` – Enables/disables aria-multiline tags
101101
* - **ariaValue** – `{boolean}` – Enables/disables aria-valuemin, aria-valuemax and aria-valuenow tags
102102
* - **tabindex** – `{boolean}` – Enables/disables tabindex tags
103-
* - **bindKeypress** – `{boolean}` – Enables/disables keypress event binding on ng-click
103+
* - **bindKeypress** – `{boolean}` – Enables/disables keypress event binding on `&lt;div&gt;` and
104+
* `&lt;li&gt;` elements with ng-click
104105
*
105106
* @description
106107
* Enables/disables various ARIA attributes
@@ -303,11 +304,18 @@ 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+
if (nodeTypeArray.indexOf(elem[0].nodeName) !== -1) {
310+
return true;
311+
}
312+
}
313+
306314
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
307315
elem.attr('tabindex', 0);
308316
}
309317

310-
if ($aria.config('bindKeypress') && !attr.ngKeypress) {
318+
if ($aria.config('bindKeypress') && !attr.ngKeypress && isNodeOneOf(elem, ['DIV', 'LI'])) {
311319
elem.on('keypress', function(event) {
312320
if (event.keyCode === 32 || event.keyCode === 13) {
313321
scope.$apply(callback);

test/ngAria/ariaSpec.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,24 @@ describe('$aria', function() {
488488

489489
it('should a trigger click from the keyboard', function() {
490490
scope.someAction = function() {};
491-
compileInput('<div ng-click="someAction()" tabindex="0"></div>');
491+
492+
var elements = $compile('<section>' +
493+
'<div class="div-click" ng-click="someAction(\'div\')" tabindex="0"></div>' +
494+
'<ul><li ng-click="someAction( \'li\')" tabindex="0"></li></ul>' +
495+
'</section>')(scope);
496+
497+
scope.$digest();
498+
492499
clickFn = spyOn(scope, 'someAction');
493500

494-
element.triggerHandler({type: 'keypress', keyCode: 32});
501+
var divElement = elements.find('div');
502+
var liElement = elements.find('li');
503+
504+
divElement.triggerHandler({type: 'keypress', keyCode: 32});
505+
liElement.triggerHandler({type: 'keypress', keyCode: 32});
495506

496-
expect(clickFn).toHaveBeenCalled();
507+
expect(clickFn).toHaveBeenCalledWith('div');
508+
expect(clickFn).toHaveBeenCalledWith('li');
497509
});
498510

499511
it('should not override existing ng-keypress', function() {
@@ -526,6 +538,13 @@ describe('$aria', function() {
526538
element.triggerHandler({ type: 'keypress', keyCode: 13 });
527539
expect(element.text()).toBe('keypress13');
528540
});
541+
542+
it('should not bind keypress to elements not in the default config', function() {
543+
compileInput('<button ng-click="event = $event">{{event.type}}{{event.keyCode}}</button>');
544+
expect(element.text()).toBe('');
545+
element.triggerHandler({ type: 'keypress', keyCode: 13 });
546+
expect(element.text()).toBe('');
547+
});
529548
});
530549

531550
describe('actions when bindKeypress set to false', function() {
@@ -534,11 +553,11 @@ describe('$aria', function() {
534553
}));
535554
beforeEach(injectScopeAndCompiler);
536555

537-
it('should not a trigger click from the keyboard', function() {
556+
it('should not a trigger click', function() {
538557
scope.someAction = function() {};
539558
var clickFn = spyOn(scope, 'someAction');
540559

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

543562
element.triggerHandler({type: 'keypress', keyCode: 32});
544563

0 commit comments

Comments
 (0)