Skip to content

Commit ffed370

Browse files
committed
Added back the allowClear option
1 parent dafa99b commit ffed370

13 files changed

Lines changed: 284 additions & 35 deletions

File tree

dist/css/select2.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
.select2-container--default .select2-selection--single .select2-selection__rendered {
8585
color: #444;
8686
line-height: 28px; }
87+
.select2-container--default .select2-selection--single .select2-selection__clear {
88+
cursor: pointer;
89+
float: right;
90+
font-weight: bold;
91+
margin-right: 10px; }
8792
.select2-container--default .select2-selection--single .select2-selection__placeholder {
8893
color: #999; }
8994
.select2-container--default .select2-selection--multiple {
@@ -156,6 +161,11 @@
156161
.select2-container--classic .select2-selection--single .select2-selection__rendered {
157162
color: #444;
158163
line-height: 28px; }
164+
.select2-container--classic .select2-selection--single .select2-selection__clear {
165+
cursor: pointer;
166+
float: right;
167+
font-weight: bold;
168+
margin-right: 10px; }
159169
.select2-container--classic .select2-selection--single .select2-selection__placeholder {
160170
color: #999; }
161171
.select2-container--classic.select2-container--open .select2-selection--single {

dist/css/select2.min.css

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.amd.full.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,45 @@ define('select2/selection/placeholder',[
967967
return Placeholder;
968968
});
969969

970+
define('select2/selection/allowClear',[
971+
972+
], function () {
973+
function AllowClear () { }
974+
975+
AllowClear.prototype.bind = function (decorated, container, $container) {
976+
var self = this;
977+
978+
decorated.call(this, container, $container);
979+
980+
this.$selection.on('mousedown', '.select2-selection__clear',
981+
function (evt) {
982+
evt.stopPropagation();
983+
984+
self.$element.val(self.placeholder.id).trigger('change');
985+
986+
self.trigger('toggle');
987+
});
988+
};
989+
990+
AllowClear.prototype.update = function (decorated, data) {
991+
decorated.call(this, data);
992+
993+
if (this.$selection.find('.select2-selection__placeholder').length > 0) {
994+
return;
995+
}
996+
997+
var $remove = $(
998+
'<span class="select2-selection__clear">' +
999+
'&times;' +
1000+
'</span>'
1001+
);
1002+
1003+
this.$selection.find('.select2-selection__rendered').append($remove);
1004+
};
1005+
1006+
return AllowClear;
1007+
});
1008+
9701009
define('select2/selection/search',[
9711010
'../utils'
9721011
], function (Utils) {
@@ -2800,6 +2839,7 @@ define('select2/defaults',[
28002839
'./selection/single',
28012840
'./selection/multiple',
28022841
'./selection/placeholder',
2842+
'./selection/allowClear',
28032843
'./selection/search',
28042844

28052845
'./utils',
@@ -2819,7 +2859,8 @@ define('select2/defaults',[
28192859

28202860
'./i18n/en'
28212861
], function ($, ResultsList,
2822-
SingleSelection, MultipleSelection, Placeholder, SelectionSearch,
2862+
SingleSelection, MultipleSelection, Placeholder, AllowClear,
2863+
SelectionSearch,
28232864
Utils, Translation, DIACRITICS,
28242865
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
28252866
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
@@ -2894,6 +2935,13 @@ define('select2/defaults',[
28942935
options.selectionAdapter,
28952936
Placeholder
28962937
);
2938+
2939+
if (options.allowClear) {
2940+
options.selectionAdapter = Utils.Decorate(
2941+
options.selectionAdapter,
2942+
AllowClear
2943+
);
2944+
}
28972945
}
28982946

28992947
if (options.multiple) {

dist/js/select2.amd.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,45 @@ define('select2/selection/placeholder',[
967967
return Placeholder;
968968
});
969969

970+
define('select2/selection/allowClear',[
971+
972+
], function () {
973+
function AllowClear () { }
974+
975+
AllowClear.prototype.bind = function (decorated, container, $container) {
976+
var self = this;
977+
978+
decorated.call(this, container, $container);
979+
980+
this.$selection.on('mousedown', '.select2-selection__clear',
981+
function (evt) {
982+
evt.stopPropagation();
983+
984+
self.$element.val(self.placeholder.id).trigger('change');
985+
986+
self.trigger('toggle');
987+
});
988+
};
989+
990+
AllowClear.prototype.update = function (decorated, data) {
991+
decorated.call(this, data);
992+
993+
if (this.$selection.find('.select2-selection__placeholder').length > 0) {
994+
return;
995+
}
996+
997+
var $remove = $(
998+
'<span class="select2-selection__clear">' +
999+
'&times;' +
1000+
'</span>'
1001+
);
1002+
1003+
this.$selection.find('.select2-selection__rendered').append($remove);
1004+
};
1005+
1006+
return AllowClear;
1007+
});
1008+
9701009
define('select2/selection/search',[
9711010
'../utils'
9721011
], function (Utils) {
@@ -2800,6 +2839,7 @@ define('select2/defaults',[
28002839
'./selection/single',
28012840
'./selection/multiple',
28022841
'./selection/placeholder',
2842+
'./selection/allowClear',
28032843
'./selection/search',
28042844

28052845
'./utils',
@@ -2819,7 +2859,8 @@ define('select2/defaults',[
28192859

28202860
'./i18n/en'
28212861
], function ($, ResultsList,
2822-
SingleSelection, MultipleSelection, Placeholder, SelectionSearch,
2862+
SingleSelection, MultipleSelection, Placeholder, AllowClear,
2863+
SelectionSearch,
28232864
Utils, Translation, DIACRITICS,
28242865
SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
28252866
Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
@@ -2894,6 +2935,13 @@ define('select2/defaults',[
28942935
options.selectionAdapter,
28952936
Placeholder
28962937
);
2938+
2939+
if (options.allowClear) {
2940+
options.selectionAdapter = Utils.Decorate(
2941+
options.selectionAdapter,
2942+
AllowClear
2943+
);
2944+
}
28972945
}
28982946

28992947
if (options.multiple) {

0 commit comments

Comments
 (0)