Skip to content

Commit 8e70077

Browse files
mariocasciarofernando-sendMail
authored andcommitted
feat(dropdown): Make Auto-Close Dropdowns optional.
Fixes angular-ui#2218 Closes angular-ui#3045
1 parent 0183d2b commit 8e70077

File tree

3 files changed

+430
-5
lines changed

3 files changed

+430
-5
lines changed

src/dropdown/docs/readme.md

-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
Dropdown is a simple directive which will toggle a dropdown menu on click or programmatically.
33
You can either use `is-open` to toggle or add inside a `<a dropdown-toggle>` element to toggle it when is clicked.
44
There is also the `on-toggle(open)` optional expression fired when dropdown changes state.
5-
6-
Add `dropdown-append-to-body` to the `dropdown` element to append to the inner `dropdown-menu` to the body.
7-
This is useful when the dropdown button is inside a div with `overflow: hidden`, and the menu would otherwise be hidden.
8-
95
By default the dropdown will automatically close if any of its elements is clicked, you can change this behavior by setting the `auto-close` option as follows:
106

117
* `always` - (Default) automatically closes the dropdown when any of its elements is clicked.
128
* `outsideClick` - closes the dropdown automatically only when the user clicks any element outside the dropdown.
139
* `disabled` - disables the auto close. You can then control the open/close status of the dropdown manually, by using `is-open`. Please notice that the dropdown will still close if the toggle is clicked, the `esc` key is pressed or another dropdown is open.
14-

src/dropdown/dropdown.js

+15
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@ angular.module('ui.bootstrap.dropdown', [])
3333
// unbound this event handler. So check openScope before proceeding.
3434
if (!openScope) { return; }
3535

36+
if( evt && openScope.getAutoClose() === 'disabled' ) { return ; }
37+
3638
var toggleElement = openScope.getToggleElement();
3739
if ( evt && toggleElement && toggleElement[0].contains(evt.target) ) {
3840
return;
3941
}
4042

43+
var $element = openScope.getElement();
44+
if( evt && openScope.getAutoClose() === 'outsideClick' && $element && $element[0].contains(evt.target) ) {
45+
return;
46+
}
47+
4148
openScope.isOpen = false;
4249

4350
if (!$rootScope.$$phase) {
@@ -87,6 +94,14 @@ angular.module('ui.bootstrap.dropdown', [])
8794
return self.toggleElement;
8895
};
8996

97+
scope.getAutoClose = function() {
98+
return $attrs.autoClose || 'always'; //or 'outsideClick' or 'disabled'
99+
};
100+
101+
scope.getElement = function() {
102+
return self.$element;
103+
};
104+
90105
scope.focusToggleElement = function() {
91106
if ( self.toggleElement ) {
92107
self.toggleElement[0].focus();

0 commit comments

Comments
 (0)