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

fix(dropdown): fix up arrow nav support #4330

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,19 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
break;
}
case (38): { // Up
dropdownCtrl.selectedOption = dropdownCtrl.selectedOption === 0 ?
0 : dropdownCtrl.selectedOption - 1;
if (!angular.isNumber(dropdownCtrl.selectedOption)) {
dropdownCtrl.selectedOption = elems.length - 1;
} else {
dropdownCtrl.selectedOption = dropdownCtrl.selectedOption === 0 ?
0 : dropdownCtrl.selectedOption - 1;
}
break;
}
}
elems[dropdownCtrl.selectedOption].focus();
}
});
}

};
})

Expand Down
11 changes: 11 additions & 0 deletions src/dropdown/test/dropdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ describe('dropdownToggle', function() {
expect(isFocused(focusEl)).toBe(false);
});

it('should focus last list element when up arrow pressed after dropdown toggled', function() {
$document.find('body').append(element);
clickDropdownToggle();
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);

triggerKeyDown($document, 38);
var elems = element.find('ul').eq(0).find('a');
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be var elems = element.find('ul li').eq(0).find('a');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the selector isn't the problem, rather the up trigger is not causing the anchor tag to become the document.activeElement. so the focus check will fail. why this is the case, i don't know. but essentially, the up arrow key is doing nothing here.

var focusEl = elems.eq(elems.length - 1);
expect(isFocused(focusEl)).toBe(true);
});

it('should not focus any list element when down arrow pressed if closed', function() {
$document.find('body').append(element);
triggerKeyDown($document, 40);
Expand Down