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

Commit

Permalink
fix(dropdown): close after selecting an item
Browse files Browse the repository at this point in the history
Fixes #2130
Closes #2140
  • Loading branch information
bekos authored and pkozlowski-opensource committed May 4, 2014
1 parent 5b41e47 commit 3ac3b48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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();

This comment has been minimized.

Copy link
@grgrzybek

grgrzybek Jul 22, 2014

Hi @bekos - this line causes NPE, as this.close() is called a moment before, setting openScope to null.

This comment has been minimized.

Copy link
@KhodeN

KhodeN Oct 22, 2014

My issue #2868

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

0 comments on commit 3ac3b48

Please sign in to comment.