Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 2b0b17b

Browse files
authored
Merge pull request #2003 from nzzdev/changeEventPropagationMode-onDocumentclick
fix: close drop-down regardless of `stopPropagation()` calls
2 parents 50c6273 + 6f19d5f commit 2b0b17b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/uiSelectDirective.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
uis.directive('uiSelect',
2-
['$document', 'uiSelectConfig', 'uiSelectMinErr', 'uisOffset', '$compile', '$parse', '$timeout',
3-
function($document, uiSelectConfig, uiSelectMinErr, uisOffset, $compile, $parse, $timeout) {
2+
['$document', 'uiSelectConfig', 'uiSelectMinErr', 'uisOffset', '$compile', '$parse', '$timeout', '$window',
3+
function($document, uiSelectConfig, uiSelectMinErr, uisOffset, $compile, $parse, $timeout, $window) {
44

55
return {
66
restrict: 'EA',
@@ -206,11 +206,15 @@ uis.directive('uiSelect',
206206
$select.clickTriggeredSelect = false;
207207
}
208208

209-
// See Click everywhere but here event http://stackoverflow.com/questions/12931369
210-
$document.on('click', onDocumentClick);
209+
// See Click everywhere but here. Similar approach to http://stackoverflow.com/questions/12931369
210+
// but using the capture phase instead of bubble phase of the event propagation.
211+
//
212+
// Using the capture phase avoids problems that araise when event.stopPropatagion()
213+
// is called before the event reaches the `document`.
214+
$window.document.addEventListener('click', onDocumentClick, true);
211215

212216
scope.$on('$destroy', function() {
213-
$document.off('click', onDocumentClick);
217+
$window.document.removeEventListener('click', onDocumentClick, true);
214218
});
215219

216220
// Move transcluded elements to their correct position in main template

test/select.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,28 @@ describe('ui-select tests', function () {
746746
el2.remove();
747747
});
748748

749+
it('should close an opened select clicking outside with stopPropagation()', function () {
750+
var el1 = createUiSelect();
751+
var el2 = $('<div></div>');
752+
el1.appendTo(document.body);
753+
el2.appendTo(document.body);
754+
755+
el2.on('click', function (e) {
756+
e.stopPropagation()
757+
});
758+
759+
expect(isDropdownOpened(el1)).toEqual(false);
760+
clickMatch(el1);
761+
expect(isDropdownOpened(el1)).toEqual(true);
762+
763+
// Using a native dom click() to make sure the test fails when it should.
764+
el2[0].click();
765+
766+
expect(isDropdownOpened(el1)).toEqual(false);
767+
el1.remove();
768+
el2.remove();
769+
});
770+
749771
it('should bind model correctly (with object as source)', function () {
750772
var el = compileTemplate(
751773
'<ui-select ng-model="selection.selected"> \

0 commit comments

Comments
 (0)