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

fix(dropdown): close after selecting an item #2140

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
7 changes: 6 additions & 1 deletion src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ angular.module('ui.bootstrap.dropdown', [])
};

var closeDropdown = function( evt ) {
if (evt && evt.isDefaultPrevented()) {
var toggleElement = openScope.getToggleElement();
if ( evt && toggleElement && toggleElement[0].contains(evt.target) ) {
return;
}

Expand Down Expand Up @@ -76,6 +77,10 @@ angular.module('ui.bootstrap.dropdown', [])
return scope.isOpen;
};

scope.getToggleElement = function() {
return self.toggleElement;
};

scope.focusToggleElement = function() {
if ( self.toggleElement ) {
self.toggleElement[0].focus();
Expand Down
16 changes: 14 additions & 2 deletions src/dropdown/test/dropdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('dropdownToggle', function() {

var clickDropdownToggle = function(elm) {
elm = elm || element;
elm.find('a').click();
elm.find('a[dropdown-toggle]').click();
};

var triggerKeyDown = function (element, keyCode) {
Expand All @@ -26,7 +26,7 @@ describe('dropdownToggle', function() {

describe('basic', function() {
function dropdown() {
return $compile('<li class="dropdown"><a href dropdown-toggle></a><ul><li>Hello</li></ul></li>')($rootScope);
return $compile('<li class="dropdown"><a href dropdown-toggle></a><ul><li><a href>Hello</a></li></ul></li>')($rootScope);
}

beforeEach(function() {
Expand All @@ -41,6 +41,18 @@ describe('dropdownToggle', function() {
expect(element.hasClass('open')).toBe(false);
});

it('should toggle when an option is clicked', function() {
$document.find('body').append(element);
expect(element.hasClass('open')).toBe(false);
clickDropdownToggle();
expect(element.hasClass('open')).toBe(true);

var optionEl = element.find('ul > li').eq(0).find('a').eq(0);
optionEl.click();
expect(element.hasClass('open')).toBe(false);
element.remove();
});

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