Skip to content

Commit 78b6faf

Browse files
committed
Check that allowClear is used with placeholder
The `allowClear` option may not work as intended if a placeholder is not used. This is becaise the placeholder holds the value that the `<select>` should be reset to when the `x` is hit. An error is now properly raised in the console when Select2 detects that the `allowClear` option is used without the `placeholder` option. This closes select2#3016.
1 parent 7a8500f commit 78b6faf

8 files changed

Lines changed: 79 additions & 34 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,15 @@ define('select2/selection/allowClear',[
11421142

11431143
decorated.call(this, container, $container);
11441144

1145+
if (self.placeholder == null) {
1146+
if (console && console.error) {
1147+
console.error(
1148+
'Select2: The `allowClear` option should be used in combination ' +
1149+
'with the `placeholder` option.'
1150+
);
1151+
}
1152+
}
1153+
11451154
this.$selection.on('mousedown', '.select2-selection__clear',
11461155
function (evt) {
11471156
// Ignore the event if it is disabled
@@ -3796,13 +3805,13 @@ define('select2/defaults',[
37963805
options.selectionAdapter,
37973806
Placeholder
37983807
);
3808+
}
37993809

3800-
if (options.allowClear) {
3801-
options.selectionAdapter = Utils.Decorate(
3802-
options.selectionAdapter,
3803-
AllowClear
3804-
);
3805-
}
3810+
if (options.allowClear) {
3811+
options.selectionAdapter = Utils.Decorate(
3812+
options.selectionAdapter,
3813+
AllowClear
3814+
);
38063815
}
38073816

38083817
if (options.multiple) {

dist/js/select2.amd.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,15 @@ define('select2/selection/allowClear',[
11421142

11431143
decorated.call(this, container, $container);
11441144

1145+
if (self.placeholder == null) {
1146+
if (console && console.error) {
1147+
console.error(
1148+
'Select2: The `allowClear` option should be used in combination ' +
1149+
'with the `placeholder` option.'
1150+
);
1151+
}
1152+
}
1153+
11451154
this.$selection.on('mousedown', '.select2-selection__clear',
11461155
function (evt) {
11471156
// Ignore the event if it is disabled
@@ -3796,13 +3805,13 @@ define('select2/defaults',[
37963805
options.selectionAdapter,
37973806
Placeholder
37983807
);
3808+
}
37993809

3800-
if (options.allowClear) {
3801-
options.selectionAdapter = Utils.Decorate(
3802-
options.selectionAdapter,
3803-
AllowClear
3804-
);
3805-
}
3810+
if (options.allowClear) {
3811+
options.selectionAdapter = Utils.Decorate(
3812+
options.selectionAdapter,
3813+
AllowClear
3814+
);
38063815
}
38073816

38083817
if (options.multiple) {

dist/js/select2.full.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,15 @@ define('select2/selection/allowClear',[
15801580

15811581
decorated.call(this, container, $container);
15821582

1583+
if (self.placeholder == null) {
1584+
if (console && console.error) {
1585+
console.error(
1586+
'Select2: The `allowClear` option should be used in combination ' +
1587+
'with the `placeholder` option.'
1588+
);
1589+
}
1590+
}
1591+
15831592
this.$selection.on('mousedown', '.select2-selection__clear',
15841593
function (evt) {
15851594
// Ignore the event if it is disabled
@@ -4234,13 +4243,13 @@ define('select2/defaults',[
42344243
options.selectionAdapter,
42354244
Placeholder
42364245
);
4246+
}
42374247

4238-
if (options.allowClear) {
4239-
options.selectionAdapter = Utils.Decorate(
4240-
options.selectionAdapter,
4241-
AllowClear
4242-
);
4243-
}
4248+
if (options.allowClear) {
4249+
options.selectionAdapter = Utils.Decorate(
4250+
options.selectionAdapter,
4251+
AllowClear
4252+
);
42444253
}
42454254

42464255
if (options.multiple) {

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,15 @@ define('select2/selection/allowClear',[
15801580

15811581
decorated.call(this, container, $container);
15821582

1583+
if (self.placeholder == null) {
1584+
if (console && console.error) {
1585+
console.error(
1586+
'Select2: The `allowClear` option should be used in combination ' +
1587+
'with the `placeholder` option.'
1588+
);
1589+
}
1590+
}
1591+
15831592
this.$selection.on('mousedown', '.select2-selection__clear',
15841593
function (evt) {
15851594
// Ignore the event if it is disabled
@@ -4234,13 +4243,13 @@ define('select2/defaults',[
42344243
options.selectionAdapter,
42354244
Placeholder
42364245
);
4246+
}
42374247

4238-
if (options.allowClear) {
4239-
options.selectionAdapter = Utils.Decorate(
4240-
options.selectionAdapter,
4241-
AllowClear
4242-
);
4243-
}
4248+
if (options.allowClear) {
4249+
options.selectionAdapter = Utils.Decorate(
4250+
options.selectionAdapter,
4251+
AllowClear
4252+
);
42444253
}
42454254

42464255
if (options.multiple) {

dist/js/select2.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/defaults.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ define([
172172
options.selectionAdapter,
173173
Placeholder
174174
);
175+
}
175176

176-
if (options.allowClear) {
177-
options.selectionAdapter = Utils.Decorate(
178-
options.selectionAdapter,
179-
AllowClear
180-
);
181-
}
177+
if (options.allowClear) {
178+
options.selectionAdapter = Utils.Decorate(
179+
options.selectionAdapter,
180+
AllowClear
181+
);
182182
}
183183

184184
if (options.multiple) {

src/js/select2/selection/allowClear.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ define([
88

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

11+
if (self.placeholder == null) {
12+
if (console && console.error) {
13+
console.error(
14+
'Select2: The `allowClear` option should be used in combination ' +
15+
'with the `placeholder` option.'
16+
);
17+
}
18+
}
19+
1120
this.$selection.on('mousedown', '.select2-selection__clear',
1221
function (evt) {
1322
// Ignore the event if it is disabled

0 commit comments

Comments
 (0)