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

Commit

Permalink
fix(dropdown): do not autoclose with outsideClick and append to body
Browse files Browse the repository at this point in the history
- Do not auto-close the dropdown menu when it is set to `outsideClick` and dropdown-append-to-body is used

Closes #3792
Fixes #3645
  • Loading branch information
wesleycho committed Jul 22, 2015
1 parent 6173664 commit cc66a06
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
return;
}

var $element = openScope.getElement();
if( evt && openScope.getAutoClose() === 'outsideClick' && $element && $element[0].contains(evt.target) ) {
var dropdownElement = openScope.getDropdownElement();
if (evt && openScope.getAutoClose() === 'outsideClick' &&
dropdownElement && dropdownElement[0].contains(evt.target)) {
return;
}

Expand Down Expand Up @@ -155,6 +156,10 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
elems[self.selectedOption].focus();
};

scope.getDropdownElement = function() {
return self.dropdownMenu;
};

scope.focusToggleElement = function() {
if ( self.toggleElement ) {
self.toggleElement[0].focus();
Expand Down
28 changes: 20 additions & 8 deletions src/dropdown/test/dropdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,26 @@ describe('dropdownToggle', function() {
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
});

it('auto-close="outsideClick"', function() {
element = dropdown('outsideClick');
clickDropdownToggle();
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
element.find('ul li a').click();
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
$document.click();
expect(element.hasClass(dropdownConfig.openClass)).toBe(false);
describe('outsideClick', function() {
it('should close only on a click outside of the dropdown menu', function() {
element = dropdown('outsideClick');
clickDropdownToggle();
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
element.find('ul li a').click();
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
$document.click();
expect(element.hasClass(dropdownConfig.openClass)).toBe(false);
});

it('should work with dropdown-append-to-body', function() {
element = $compile('<li dropdown dropdown-append-to-body auto-close="outsideClick"><a href dropdown-toggle></a><ul class="dropdown-menu" id="dropdown-menu"><li><a href>Hello On Body</a></li></ul></li>')($rootScope);
clickDropdownToggle();
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
$document.find('#dropdown-menu').find('li').eq(0).trigger('click');
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
$document.click();
expect(element.hasClass(dropdownConfig.openClass)).toBe(false);
});
});

it('control with is-open', function() {
Expand Down

0 comments on commit cc66a06

Please sign in to comment.