Skip to content

Commit 481c438

Browse files
committed
selectOnClose now properly works with closeOnSelect
Previously we hacked around the infinite loop between closeOnSelect and selectOnClose by attempting to detect what event was being triggered without knowing what event triggered it. Now we properly relay the Select2 event and the jQuery event that triggered the select or unselect This closes select2#4012
1 parent ad8447c commit 481c438

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/js/select2/dropdown/closeOnSelect.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ define([
2525
return;
2626
}
2727

28-
this.trigger('close', {});
28+
this.trigger('close', {
29+
originalEvent: originalEvent,
30+
originalSelect2Event: evt
31+
});
2932
};
3033

3134
return CloseOnSelect;

src/js/select2/dropdown/selectOnClose.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@ define([
88

99
decorated.call(this, container, $container);
1010

11-
container.on('close', function () {
12-
self._handleSelectOnClose();
11+
container.on('close', function (params) {
12+
self._handleSelectOnClose(params);
1313
});
1414
};
1515

16-
SelectOnClose.prototype._handleSelectOnClose = function () {
16+
SelectOnClose.prototype._handleSelectOnClose = function (_, params) {
17+
if (params && params.originalSelect2Event != null) {
18+
var event = params.originalSelect2Event;
19+
20+
// Don't select an item if the close event was triggered from a select or
21+
// unselect event
22+
if (event._type === 'select' || event._type === 'unselect') {
23+
return;
24+
}
25+
}
26+
1727
var $highlightedResults = this.getHighlightedResults();
1828

1929
// Only select highlighted results

0 commit comments

Comments
 (0)