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

Commit

Permalink
feat(dropdownToggle): add support for escape key
Browse files Browse the repository at this point in the history
Closes #1558
  • Loading branch information
bekos committed Jan 21, 2014
1 parent ae31079 commit 1417c54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/dropdownToggle/dropdownToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ angular.module('ui.bootstrap.dropdownToggle', [])
this.open = function( dropdownScope ) {
if ( !openScope ) {
$document.bind('click', closeDropdown);
$document.bind('keydown', escapeKeyBind);
}

if ( openScope && openScope !== dropdownScope ) {
Expand All @@ -23,6 +24,7 @@ angular.module('ui.bootstrap.dropdownToggle', [])
if ( openScope === dropdownScope ) {
openScope = null;
$document.unbind('click', closeDropdown);
$document.unbind('keydown', escapeKeyBind);
}
};

Expand All @@ -31,6 +33,12 @@ angular.module('ui.bootstrap.dropdownToggle', [])
openScope.isOpen = false;
});
};

var escapeKeyBind = function( evt ) {
if ( evt.which === 27 ) {
closeDropdown();
}
};
}])

.controller('DropdownController', ['$scope', '$attrs', 'dropdownConfig', 'dropdownService', function($scope, $attrs, dropdownConfig, dropdownService) {
Expand Down
18 changes: 18 additions & 0 deletions src/dropdownToggle/test/dropdownToggle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('dropdownToggle', function() {
elm.find('a').click();
};

var triggerKeyDown = function (element, keyCode) {
var e = $.Event('keydown');
e.which = keyCode;
element.trigger(e);
};

describe('basic', function() {
function dropdown() {
return $compile('<li class="dropdown"><a dropdown-toggle></a><ul><li>Hello</li></ul></li>')($rootScope);
Expand All @@ -38,6 +44,18 @@ describe('dropdownToggle', function() {
expect(element.hasClass('open')).toBe(false);
});

it('should close on escape key', function() {
clickDropdownToggle();
triggerKeyDown($document, 27);
expect(element.hasClass('open')).toBe(false);
});

it('should not close on backspace key', function() {
clickDropdownToggle();
triggerKeyDown($document, 8);
expect(element.hasClass('open')).toBe(true);
});

it('should close on $location change', function() {
clickDropdownToggle();
expect(element.hasClass('open')).toBe(true);
Expand Down

0 comments on commit 1417c54

Please sign in to comment.