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

fix(ngAria): trigger digest on ng-click via keypress, pass $event to expression #10443

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
31 changes: 19 additions & 12 deletions src/ngAria/aria.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,28 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
}
};
})
.directive('ngClick',['$aria', function($aria) {
.directive('ngClick',['$aria', '$parse', function($aria, $parse) {
return {
restrict: 'A',
link: function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
elem.attr('tabindex', 0);
}
compile: function(elem, attr) {
var fn = $parse(attr.ngClick, /* interceptorFn */ null, /* expensiveChecks */ true);
return function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be safe to move this into compile ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be a problem with replace: true directives, which has been reported for other places where we do this, like ngModel. It's a trade-off, perf vs correctness

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);
}
});
}
if ($aria.config('bindKeypress') && !attr.ngKeypress) {
elem.on('keypress', function(event) {
if (event.keyCode === 32 || event.keyCode === 13) {
scope.$apply(callback);
}

function callback() {
fn(scope, { $event: event });
}
});
}
};
}
};
}])
Expand Down
17 changes: 17 additions & 0 deletions test/ngAria/ariaSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,23 @@ describe('$aria', function() {
expect(clickFn).not.toHaveBeenCalled();
expect(keypressFn).toHaveBeenCalled();
});

it('should update bindings when keypress handled', function() {
compileInput('<div ng-click="text = \'clicked!\'">{{text}}</div>');
expect(element.text()).toBe('');
spyOn(scope.$root, '$digest').andCallThrough();
element.triggerHandler({ type: 'keypress', keyCode: 13 });
expect(element.text()).toBe('clicked!');
expect(scope.$root.$digest).toHaveBeenCalledOnce();
});

it('should pass $event to ng-click handler as local', function() {
compileInput('<div ng-click="event = $event">{{event.type}}' +
'{{event.keyCode}}</div>');
expect(element.text()).toBe('');
element.triggerHandler({ type: 'keypress', keyCode: 13 });
expect(element.text()).toBe('keypress13');
});
});

describe('actions when bindKeypress set to false', function() {
Expand Down