Skip to content

Commit c9a8508

Browse files
committed
Add back closeOnSelect option
The `closeOnSelect` option was previously used to control whether or not the dropdown was closed when an option was selected. This could be simulated by triggering the `open` event after the `close` event was received, but it makes sense to abstract it out into a decorator. This also adds support for not closing the dropdown when the control key is being held. This is useful when multiple options need to be selected in quick succession, so the dropdown does not have to be reopened. This also adds documentation that covers both changes. This closes select2#2735. This closes select2#3017.
1 parent caeb0ec commit c9a8508

10 files changed

Lines changed: 220 additions & 107 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,26 +3104,6 @@ define('select2/dropdown',[
31043104
this.$dropdown.remove();
31053105
};
31063106

3107-
Dropdown.prototype.bind = function (container, $container) {
3108-
var self = this;
3109-
3110-
container.on('select', function (params) {
3111-
self._onSelect(params);
3112-
});
3113-
3114-
container.on('unselect', function (params) {
3115-
self._onUnSelect(params);
3116-
});
3117-
};
3118-
3119-
Dropdown.prototype._onSelect = function () {
3120-
this.trigger('close');
3121-
};
3122-
3123-
Dropdown.prototype._onUnSelect = function () {
3124-
this.trigger('close');
3125-
};
3126-
31273107
return Dropdown;
31283108
});
31293109

@@ -3610,6 +3590,31 @@ define('select2/dropdown/selectOnClose',[
36103590
return SelectOnClose;
36113591
});
36123592

3593+
define('select2/dropdown/closeOnSelect',[
3594+
3595+
], function () {
3596+
function CloseOnSelect () { }
3597+
3598+
CloseOnSelect.prototype.bind = function (decorated, container, $container) {
3599+
var self = this;
3600+
3601+
decorated.call(this, container, $container);
3602+
3603+
container.on('select', function (evt) {
3604+
var originalEvent = evt.originalEvent;
3605+
3606+
// Don't close if the control key is being held
3607+
if (originalEvent && originalEvent.ctrlKey) {
3608+
return;
3609+
}
3610+
3611+
self.trigger('close');
3612+
});
3613+
};
3614+
3615+
return CloseOnSelect;
3616+
});
3617+
36133618
define('select2/i18n/en',[],function () {
36143619
// English
36153620
return {
@@ -3686,6 +3691,7 @@ define('select2/defaults',[
36863691
'./dropdown/attachBody',
36873692
'./dropdown/minimumResultsForSearch',
36883693
'./dropdown/selectOnClose',
3694+
'./dropdown/closeOnSelect',
36893695

36903696
'./i18n/en'
36913697
], function ($, ResultsList,
@@ -3699,7 +3705,7 @@ define('select2/defaults',[
36993705
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
37003706

37013707
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
3702-
AttachBody, MinimumResultsForSearch, SelectOnClose,
3708+
AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
37033709

37043710
EnglishTranslation) {
37053711
function Defaults () {
@@ -3810,6 +3816,13 @@ define('select2/defaults',[
38103816
);
38113817
}
38123818

3819+
if (options.closeOnSelect) {
3820+
options.dropdownAdapter = Utils.Decorate(
3821+
options.dropdownAdapter,
3822+
CloseOnSelect
3823+
);
3824+
}
3825+
38133826
options.dropdownAdapter = Utils.Decorate(
38143827
options.dropdownAdapter,
38153828
AttachBody
@@ -3966,6 +3979,7 @@ define('select2/defaults',[
39663979
this.defaults = {
39673980
amdBase: 'select2/',
39683981
amdLanguageBase: 'select2/i18n/',
3982+
closeOnSelect: true,
39693983
escapeMarkup: Utils.escapeMarkup,
39703984
language: EnglishTranslation,
39713985
matcher: matcher,

dist/js/select2.amd.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,26 +3104,6 @@ define('select2/dropdown',[
31043104
this.$dropdown.remove();
31053105
};
31063106

3107-
Dropdown.prototype.bind = function (container, $container) {
3108-
var self = this;
3109-
3110-
container.on('select', function (params) {
3111-
self._onSelect(params);
3112-
});
3113-
3114-
container.on('unselect', function (params) {
3115-
self._onUnSelect(params);
3116-
});
3117-
};
3118-
3119-
Dropdown.prototype._onSelect = function () {
3120-
this.trigger('close');
3121-
};
3122-
3123-
Dropdown.prototype._onUnSelect = function () {
3124-
this.trigger('close');
3125-
};
3126-
31273107
return Dropdown;
31283108
});
31293109

@@ -3610,6 +3590,31 @@ define('select2/dropdown/selectOnClose',[
36103590
return SelectOnClose;
36113591
});
36123592

3593+
define('select2/dropdown/closeOnSelect',[
3594+
3595+
], function () {
3596+
function CloseOnSelect () { }
3597+
3598+
CloseOnSelect.prototype.bind = function (decorated, container, $container) {
3599+
var self = this;
3600+
3601+
decorated.call(this, container, $container);
3602+
3603+
container.on('select', function (evt) {
3604+
var originalEvent = evt.originalEvent;
3605+
3606+
// Don't close if the control key is being held
3607+
if (originalEvent && originalEvent.ctrlKey) {
3608+
return;
3609+
}
3610+
3611+
self.trigger('close');
3612+
});
3613+
};
3614+
3615+
return CloseOnSelect;
3616+
});
3617+
36133618
define('select2/i18n/en',[],function () {
36143619
// English
36153620
return {
@@ -3686,6 +3691,7 @@ define('select2/defaults',[
36863691
'./dropdown/attachBody',
36873692
'./dropdown/minimumResultsForSearch',
36883693
'./dropdown/selectOnClose',
3694+
'./dropdown/closeOnSelect',
36893695

36903696
'./i18n/en'
36913697
], function ($, ResultsList,
@@ -3699,7 +3705,7 @@ define('select2/defaults',[
36993705
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
37003706

37013707
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
3702-
AttachBody, MinimumResultsForSearch, SelectOnClose,
3708+
AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
37033709

37043710
EnglishTranslation) {
37053711
function Defaults () {
@@ -3810,6 +3816,13 @@ define('select2/defaults',[
38103816
);
38113817
}
38123818

3819+
if (options.closeOnSelect) {
3820+
options.dropdownAdapter = Utils.Decorate(
3821+
options.dropdownAdapter,
3822+
CloseOnSelect
3823+
);
3824+
}
3825+
38133826
options.dropdownAdapter = Utils.Decorate(
38143827
options.dropdownAdapter,
38153828
AttachBody
@@ -3966,6 +3979,7 @@ define('select2/defaults',[
39663979
this.defaults = {
39673980
amdBase: 'select2/',
39683981
amdLanguageBase: 'select2/i18n/',
3982+
closeOnSelect: true,
39693983
escapeMarkup: Utils.escapeMarkup,
39703984
language: EnglishTranslation,
39713985
matcher: matcher,

dist/js/select2.full.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,26 +3542,6 @@ define('select2/dropdown',[
35423542
this.$dropdown.remove();
35433543
};
35443544

3545-
Dropdown.prototype.bind = function (container, $container) {
3546-
var self = this;
3547-
3548-
container.on('select', function (params) {
3549-
self._onSelect(params);
3550-
});
3551-
3552-
container.on('unselect', function (params) {
3553-
self._onUnSelect(params);
3554-
});
3555-
};
3556-
3557-
Dropdown.prototype._onSelect = function () {
3558-
this.trigger('close');
3559-
};
3560-
3561-
Dropdown.prototype._onUnSelect = function () {
3562-
this.trigger('close');
3563-
};
3564-
35653545
return Dropdown;
35663546
});
35673547

@@ -4048,6 +4028,31 @@ define('select2/dropdown/selectOnClose',[
40484028
return SelectOnClose;
40494029
});
40504030

4031+
define('select2/dropdown/closeOnSelect',[
4032+
4033+
], function () {
4034+
function CloseOnSelect () { }
4035+
4036+
CloseOnSelect.prototype.bind = function (decorated, container, $container) {
4037+
var self = this;
4038+
4039+
decorated.call(this, container, $container);
4040+
4041+
container.on('select', function (evt) {
4042+
var originalEvent = evt.originalEvent;
4043+
4044+
// Don't close if the control key is being held
4045+
if (originalEvent && originalEvent.ctrlKey) {
4046+
return;
4047+
}
4048+
4049+
self.trigger('close');
4050+
});
4051+
};
4052+
4053+
return CloseOnSelect;
4054+
});
4055+
40514056
define('select2/i18n/en',[],function () {
40524057
// English
40534058
return {
@@ -4124,6 +4129,7 @@ define('select2/defaults',[
41244129
'./dropdown/attachBody',
41254130
'./dropdown/minimumResultsForSearch',
41264131
'./dropdown/selectOnClose',
4132+
'./dropdown/closeOnSelect',
41274133

41284134
'./i18n/en'
41294135
], function ($, ResultsList,
@@ -4137,7 +4143,7 @@ define('select2/defaults',[
41374143
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
41384144

41394145
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
4140-
AttachBody, MinimumResultsForSearch, SelectOnClose,
4146+
AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
41414147

41424148
EnglishTranslation) {
41434149
function Defaults () {
@@ -4248,6 +4254,13 @@ define('select2/defaults',[
42484254
);
42494255
}
42504256

4257+
if (options.closeOnSelect) {
4258+
options.dropdownAdapter = Utils.Decorate(
4259+
options.dropdownAdapter,
4260+
CloseOnSelect
4261+
);
4262+
}
4263+
42514264
options.dropdownAdapter = Utils.Decorate(
42524265
options.dropdownAdapter,
42534266
AttachBody
@@ -4404,6 +4417,7 @@ define('select2/defaults',[
44044417
this.defaults = {
44054418
amdBase: 'select2/',
44064419
amdLanguageBase: 'select2/i18n/',
4420+
closeOnSelect: true,
44074421
escapeMarkup: Utils.escapeMarkup,
44084422
language: EnglishTranslation,
44094423
matcher: matcher,

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,26 +3542,6 @@ define('select2/dropdown',[
35423542
this.$dropdown.remove();
35433543
};
35443544

3545-
Dropdown.prototype.bind = function (container, $container) {
3546-
var self = this;
3547-
3548-
container.on('select', function (params) {
3549-
self._onSelect(params);
3550-
});
3551-
3552-
container.on('unselect', function (params) {
3553-
self._onUnSelect(params);
3554-
});
3555-
};
3556-
3557-
Dropdown.prototype._onSelect = function () {
3558-
this.trigger('close');
3559-
};
3560-
3561-
Dropdown.prototype._onUnSelect = function () {
3562-
this.trigger('close');
3563-
};
3564-
35653545
return Dropdown;
35663546
});
35673547

@@ -4048,6 +4028,31 @@ define('select2/dropdown/selectOnClose',[
40484028
return SelectOnClose;
40494029
});
40504030

4031+
define('select2/dropdown/closeOnSelect',[
4032+
4033+
], function () {
4034+
function CloseOnSelect () { }
4035+
4036+
CloseOnSelect.prototype.bind = function (decorated, container, $container) {
4037+
var self = this;
4038+
4039+
decorated.call(this, container, $container);
4040+
4041+
container.on('select', function (evt) {
4042+
var originalEvent = evt.originalEvent;
4043+
4044+
// Don't close if the control key is being held
4045+
if (originalEvent && originalEvent.ctrlKey) {
4046+
return;
4047+
}
4048+
4049+
self.trigger('close');
4050+
});
4051+
};
4052+
4053+
return CloseOnSelect;
4054+
});
4055+
40514056
define('select2/i18n/en',[],function () {
40524057
// English
40534058
return {
@@ -4124,6 +4129,7 @@ define('select2/defaults',[
41244129
'./dropdown/attachBody',
41254130
'./dropdown/minimumResultsForSearch',
41264131
'./dropdown/selectOnClose',
4132+
'./dropdown/closeOnSelect',
41274133

41284134
'./i18n/en'
41294135
], function ($, ResultsList,
@@ -4137,7 +4143,7 @@ define('select2/defaults',[
41374143
MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
41384144

41394145
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
4140-
AttachBody, MinimumResultsForSearch, SelectOnClose,
4146+
AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
41414147

41424148
EnglishTranslation) {
41434149
function Defaults () {
@@ -4248,6 +4254,13 @@ define('select2/defaults',[
42484254
);
42494255
}
42504256

4257+
if (options.closeOnSelect) {
4258+
options.dropdownAdapter = Utils.Decorate(
4259+
options.dropdownAdapter,
4260+
CloseOnSelect
4261+
);
4262+
}
4263+
42514264
options.dropdownAdapter = Utils.Decorate(
42524265
options.dropdownAdapter,
42534266
AttachBody
@@ -4404,6 +4417,7 @@ define('select2/defaults',[
44044417
this.defaults = {
44054418
amdBase: 'select2/',
44064419
amdLanguageBase: 'select2/i18n/',
4420+
closeOnSelect: true,
44074421
escapeMarkup: Utils.escapeMarkup,
44084422
language: EnglishTranslation,
44094423
matcher: matcher,

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)