Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(datepicker): don't stop ESC propagation unless dropdown is open
Browse files Browse the repository at this point in the history
In reference to issue #3096
If the escape key should have other functionality in the context of a form (for
example, if datepicker is on a modal) it makes sense to prevent this from
happening if escape is used to close the dropdown.  If the dropdown is closed,
however, the event should be allowed to propagate.

Fixes #3096
Closes #3179
  • Loading branch information
MikeMatusz authored and chrisirhc committed Mar 9, 2015
1 parent 2724b46 commit c2e5b28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
scope.keydown = function(evt) {
if (evt.which === 27) {
evt.preventDefault();
evt.stopPropagation();
if (scope.isOpen) {
evt.stopPropagation();
}
scope.close();
} else if (evt.which === 40 && !scope.isOpen) {
scope.isOpen = true;
Expand Down
21 changes: 21 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,27 @@ describe('datepicker directive', function () {
expect(dropdownEl).toBeHidden();
expect(document.activeElement.tagName).toBe('INPUT');
});

it('stops the ESC key from propagating if the dropdown is open, but not when closed', function() {
expect(dropdownEl).not.toBeHidden();

dropdownEl.find('button').eq(0).focus();
expect(document.activeElement.tagName).toBe('BUTTON');

var documentKey = -1;
var getKey = function(evt) { documentKey = evt.which; };
$document.bind('keydown', getKey);

triggerKeyDown(inputEl, 'esc');
$rootScope.$digest();
expect(dropdownEl).toBeHidden();
expect(documentKey).toBe(-1);

triggerKeyDown(inputEl, 'esc');
expect(documentKey).toBe(27);

$document.unbind('keydown', getKey);
});
});
});

Expand Down

0 comments on commit c2e5b28

Please sign in to comment.