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

feat(ngAria): Bind keypress on ng-click w/ optional flag #10288

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
40 changes: 29 additions & 11 deletions src/ngAria/aria.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* | {@link ng.directive:ngDisabled ngDisabled} | aria-disabled |
* | {@link ng.directive:ngShow ngShow} | aria-hidden |
* | {@link ng.directive:ngHide ngHide} | aria-hidden |
* | {@link ng.directive:ngClick ngClick} | tabindex |
* | {@link ng.directive:ngClick ngClick} | tabindex, keypress event |
* | {@link ng.directive:ngDblclick ngDblclick} | tabindex |
* | {@link module:ngMessages ngMessages} | aria-live |
*
Expand Down Expand Up @@ -82,7 +82,8 @@ function $AriaProvider() {
ariaInvalid: true,
ariaMultiline: true,
ariaValue: true,
tabindex: true
tabindex: true,
bindKeypress: true
};

/**
Expand All @@ -99,6 +100,7 @@ function $AriaProvider() {
* - **ariaMultiline** – `{boolean}` – Enables/disables aria-multiline tags
* - **ariaValue** – `{boolean}` – Enables/disables aria-valuemin, aria-valuemax and aria-valuenow tags
* - **tabindex** – `{boolean}` – Enables/disables tabindex tags
* - **bindKeypress** – `{boolean}` – Enables/disables keypress event binding on ng-click
*
* @description
* Enables/disables various ARIA attributes
Expand Down Expand Up @@ -183,13 +185,6 @@ function $AriaProvider() {
};
}

var ngAriaTabindex = ['$aria', function($aria) {
return function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
elem.attr('tabindex', 0);
}
};
}];

ngAriaModule.directive('ngShow', ['$aria', function($aria) {
return $aria.$$watchExpr('ngShow', 'aria-hidden', true);
Expand Down Expand Up @@ -309,5 +304,28 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
}
};
})
.directive('ngClick', ngAriaTabindex)
.directive('ngDblclick', ngAriaTabindex);
.directive('ngClick',['$aria', function($aria) {
return {
restrict: 'A',
link: function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
elem.attr('tabindex', 0);
}

if ($aria.config('bindKeypress') && !elem.attr('ng-keypress')) {
elem.on('keypress', function(event) {
if (event.keyCode === 32 || event.keyCode === 13) {
scope.$eval(attr.ngClick);
}
});
}
}
};
}])
.directive('ngDblclick', ['$aria', function($aria) {
return function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
elem.attr('tabindex', 0);
}
};
}]);
48 changes: 48 additions & 0 deletions test/ngAria/ariaSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,54 @@ describe('$aria', function() {
});
});

describe('accessible actions', function() {
beforeEach(injectScopeAndCompiler);

var clickFn;

it('should a trigger click from the keyboard', function() {
scope.someAction = function() {};
compileInput('<div ng-click="someAction()" tabindex="0"></div>');
clickFn = spyOn(scope, 'someAction');

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

expect(clickFn).toHaveBeenCalled();
});

it('should not override existing ng-keypress', function() {
scope.someOtherAction = function() {};
var keypressFn = spyOn(scope, 'someOtherAction');

scope.someAction = function() {};
clickFn = spyOn(scope, 'someAction');
compileInput('<div ng-click="someAction()" ng-keypress="someOtherAction()" tabindex="0"></div>');

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

expect(clickFn).not.toHaveBeenCalled();
expect(keypressFn).toHaveBeenCalled();
});
});

describe('actions when bindKeypress set to false', function() {
beforeEach(configAriaProvider({
bindKeypress: false
}));
beforeEach(injectScopeAndCompiler);

it('should not a trigger click from the keyboard', function() {
scope.someAction = function() {};
var clickFn = spyOn(scope, 'someAction');

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

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

expect(clickFn).not.toHaveBeenCalled();
});
});

describe('tabindex when disabled', function() {
beforeEach(configAriaProvider({
tabindex: false
Expand Down