Skip to content

Commit

Permalink
Add keynav support to dropdown (angular-ui#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
bleggett committed Jun 11, 2015
1 parent ed4db50 commit 3956f90
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,42 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
};
})

.directive('keyboardNav', function() {
return {
restrict: 'A',
require: '?^dropdown',
link: function (scope, element, attrs, dropdownCtrl) {

element.bind('keydown', function(e) {

e.preventDefault();
e.stopPropagation();

var elems = angular.element(element).find('a');
switch (e.keyCode) {
case (40): // Down
{

if ( !angular.isNumber(dropdownCtrl.selectedOption) ) {
dropdownCtrl.selectedOption = 0;
} else {
dropdownCtrl.selectedOption = (dropdownCtrl.selectedOption === elems.length -1 ? dropdownCtrl.selectedOption : dropdownCtrl.selectedOption+1);
}

}
break;
case (38): // Up
{
dropdownCtrl.selectedOption = (dropdownCtrl.selectedOption === 0 ? 0 : dropdownCtrl.selectedOption-1);
}
break;
}
elems[dropdownCtrl.selectedOption].focus();
});
}
};
})

.directive('dropdownToggle', function() {
return {
require: '?^dropdown',
Expand Down

0 comments on commit 3956f90

Please sign in to comment.