From bf8eab14e3263ac8ad1f1719cc969475d56b5383 Mon Sep 17 00:00:00 2001 From: Tasos Bekos Date: Sat, 3 May 2014 22:00:03 +0200 Subject: [PATCH] fix(dropdown): close after selecting an item Fixes #2130 --- src/dropdown/dropdown.js | 7 ++++++- src/dropdown/test/dropdown.spec.js | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/dropdown/dropdown.js b/src/dropdown/dropdown.js index d67a960677..ed869da24c 100644 --- a/src/dropdown/dropdown.js +++ b/src/dropdown/dropdown.js @@ -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; } @@ -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(); diff --git a/src/dropdown/test/dropdown.spec.js b/src/dropdown/test/dropdown.spec.js index c65f8305e6..c34d1e5bb4 100644 --- a/src/dropdown/test/dropdown.spec.js +++ b/src/dropdown/test/dropdown.spec.js @@ -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) { @@ -26,7 +26,7 @@ describe('dropdownToggle', function() { describe('basic', function() { function dropdown() { - return $compile('')($rootScope); + return $compile('')($rootScope); } beforeEach(function() { @@ -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);