Skip to content

Commit 3f87dbc

Browse files
authored
Merge pull request #1273 from Choices-js/fix-disabled-placeholder-handling
Fix v11 regression for disabled placeholder option handling
2 parents bc285dc + abc3b04 commit 3f87dbc

File tree

12 files changed

+117
-88
lines changed

12 files changed

+117
-88
lines changed

public/assets/scripts/choices.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@
12481248
* Get highlighted items from store
12491249
*/
12501250
get: function () {
1251-
return this.items.filter(function (item) { return !item.disabled && item.active && item.highlighted; });
1251+
return this.items.filter(function (item) { return item.active && item.highlighted; });
12521252
},
12531253
enumerable: false,
12541254
configurable: true
@@ -1275,7 +1275,7 @@
12751275
});
12761276
Object.defineProperty(Store.prototype, "searchableChoices", {
12771277
/**
1278-
* Get choices that can be searched (excluding placeholders)
1278+
* Get choices that can be searched (excluding placeholders or disabled choices)
12791279
*/
12801280
get: function () {
12811281
return this.choices.filter(function (choice) { return !choice.disabled && !choice.placeholder; });
@@ -3838,7 +3838,11 @@
38383838
if (!isDefaultLabel || !isDefaultValue) {
38393839
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
38403840
}
3841-
_this._addChoice(mapInputToChoice(choice, false));
3841+
var choiceFull = mapInputToChoice(choice, false);
3842+
_this._addChoice(choiceFull);
3843+
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
3844+
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
3845+
}
38423846
}
38433847
});
38443848
_this.unhighlightAll();
@@ -3864,7 +3868,7 @@
38643868
var existingItems = {};
38653869
if (!deselectAll) {
38663870
_this._store.items.forEach(function (choice) {
3867-
if (choice.id && choice.active && choice.selected && !choice.disabled) {
3871+
if (choice.id && choice.active && choice.selected) {
38683872
existingItems[choice.value] = true;
38693873
}
38703874
});
@@ -4100,26 +4104,26 @@
41004104
};
41014105
// new items
41024106
items.forEach(addItemToFragment);
4103-
var addItems = !!fragment.childNodes.length;
4104-
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
4107+
var addedItems = !!fragment.childNodes.length;
4108+
if (this._isSelectOneElement) {
41054109
var existingItems = itemList.children.length;
4106-
if (addItems || existingItems > 1) {
4110+
if (addedItems || existingItems > 1) {
41074111
var placeholder = itemList.querySelector(getClassNamesSelector(config.classNames.placeholder));
41084112
if (placeholder) {
41094113
placeholder.remove();
41104114
}
41114115
}
4112-
else if (!existingItems) {
4113-
addItems = true;
4116+
else if (!addedItems && !existingItems && this._placeholderValue) {
4117+
addedItems = true;
41144118
addItemToFragment(mapInputToChoice({
41154119
selected: true,
41164120
value: '',
4117-
label: config.placeholderValue || '',
4121+
label: this._placeholderValue,
41184122
placeholder: true,
41194123
}, false));
41204124
}
41214125
}
4122-
if (addItems) {
4126+
if (addedItems) {
41234127
itemList.append(fragment);
41244128
if (config.shouldSortItems && !this._isSelectOneElement) {
41254129
items.sort(config.sorter);
@@ -4230,7 +4234,7 @@
42304234
_this._removeItem(itemToRemove);
42314235
_this._triggerChange(itemToRemove.value);
42324236
if (_this._isSelectOneElement && !_this._hasNonChoicePlaceholder) {
4233-
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return !choice.disabled && choice.placeholder; });
4237+
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return choice.placeholder; });
42344238
if (placeholderChoice) {
42354239
_this._addItem(placeholderChoice);
42364240
_this.unhighlightAll();

public/assets/scripts/choices.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.

public/assets/scripts/choices.mjs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ var Store = /** @class */ (function () {
12421242
* Get highlighted items from store
12431243
*/
12441244
get: function () {
1245-
return this.items.filter(function (item) { return !item.disabled && item.active && item.highlighted; });
1245+
return this.items.filter(function (item) { return item.active && item.highlighted; });
12461246
},
12471247
enumerable: false,
12481248
configurable: true
@@ -1269,7 +1269,7 @@ var Store = /** @class */ (function () {
12691269
});
12701270
Object.defineProperty(Store.prototype, "searchableChoices", {
12711271
/**
1272-
* Get choices that can be searched (excluding placeholders)
1272+
* Get choices that can be searched (excluding placeholders or disabled choices)
12731273
*/
12741274
get: function () {
12751275
return this.choices.filter(function (choice) { return !choice.disabled && !choice.placeholder; });
@@ -3832,7 +3832,11 @@ var Choices = /** @class */ (function () {
38323832
if (!isDefaultLabel || !isDefaultValue) {
38333833
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
38343834
}
3835-
_this._addChoice(mapInputToChoice(choice, false));
3835+
var choiceFull = mapInputToChoice(choice, false);
3836+
_this._addChoice(choiceFull);
3837+
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
3838+
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
3839+
}
38363840
}
38373841
});
38383842
_this.unhighlightAll();
@@ -3858,7 +3862,7 @@ var Choices = /** @class */ (function () {
38583862
var existingItems = {};
38593863
if (!deselectAll) {
38603864
_this._store.items.forEach(function (choice) {
3861-
if (choice.id && choice.active && choice.selected && !choice.disabled) {
3865+
if (choice.id && choice.active && choice.selected) {
38623866
existingItems[choice.value] = true;
38633867
}
38643868
});
@@ -4094,26 +4098,26 @@ var Choices = /** @class */ (function () {
40944098
};
40954099
// new items
40964100
items.forEach(addItemToFragment);
4097-
var addItems = !!fragment.childNodes.length;
4098-
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
4101+
var addedItems = !!fragment.childNodes.length;
4102+
if (this._isSelectOneElement) {
40994103
var existingItems = itemList.children.length;
4100-
if (addItems || existingItems > 1) {
4104+
if (addedItems || existingItems > 1) {
41014105
var placeholder = itemList.querySelector(getClassNamesSelector(config.classNames.placeholder));
41024106
if (placeholder) {
41034107
placeholder.remove();
41044108
}
41054109
}
4106-
else if (!existingItems) {
4107-
addItems = true;
4110+
else if (!addedItems && !existingItems && this._placeholderValue) {
4111+
addedItems = true;
41084112
addItemToFragment(mapInputToChoice({
41094113
selected: true,
41104114
value: '',
4111-
label: config.placeholderValue || '',
4115+
label: this._placeholderValue,
41124116
placeholder: true,
41134117
}, false));
41144118
}
41154119
}
4116-
if (addItems) {
4120+
if (addedItems) {
41174121
itemList.append(fragment);
41184122
if (config.shouldSortItems && !this._isSelectOneElement) {
41194123
items.sort(config.sorter);
@@ -4224,7 +4228,7 @@ var Choices = /** @class */ (function () {
42244228
_this._removeItem(itemToRemove);
42254229
_this._triggerChange(itemToRemove.value);
42264230
if (_this._isSelectOneElement && !_this._hasNonChoicePlaceholder) {
4227-
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return !choice.disabled && choice.placeholder; });
4231+
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return choice.placeholder; });
42284232
if (placeholderChoice) {
42294233
_this._addItem(placeholderChoice);
42304234
_this.unhighlightAll();

public/assets/scripts/choices.search-basic.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@
12481248
* Get highlighted items from store
12491249
*/
12501250
get: function () {
1251-
return this.items.filter(function (item) { return !item.disabled && item.active && item.highlighted; });
1251+
return this.items.filter(function (item) { return item.active && item.highlighted; });
12521252
},
12531253
enumerable: false,
12541254
configurable: true
@@ -1275,7 +1275,7 @@
12751275
});
12761276
Object.defineProperty(Store.prototype, "searchableChoices", {
12771277
/**
1278-
* Get choices that can be searched (excluding placeholders)
1278+
* Get choices that can be searched (excluding placeholders or disabled choices)
12791279
*/
12801280
get: function () {
12811281
return this.choices.filter(function (choice) { return !choice.disabled && !choice.placeholder; });
@@ -3356,7 +3356,11 @@
33563356
if (!isDefaultLabel || !isDefaultValue) {
33573357
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
33583358
}
3359-
_this._addChoice(mapInputToChoice(choice, false));
3359+
var choiceFull = mapInputToChoice(choice, false);
3360+
_this._addChoice(choiceFull);
3361+
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
3362+
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
3363+
}
33603364
}
33613365
});
33623366
_this.unhighlightAll();
@@ -3382,7 +3386,7 @@
33823386
var existingItems = {};
33833387
if (!deselectAll) {
33843388
_this._store.items.forEach(function (choice) {
3385-
if (choice.id && choice.active && choice.selected && !choice.disabled) {
3389+
if (choice.id && choice.active && choice.selected) {
33863390
existingItems[choice.value] = true;
33873391
}
33883392
});
@@ -3618,26 +3622,26 @@
36183622
};
36193623
// new items
36203624
items.forEach(addItemToFragment);
3621-
var addItems = !!fragment.childNodes.length;
3622-
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
3625+
var addedItems = !!fragment.childNodes.length;
3626+
if (this._isSelectOneElement) {
36233627
var existingItems = itemList.children.length;
3624-
if (addItems || existingItems > 1) {
3628+
if (addedItems || existingItems > 1) {
36253629
var placeholder = itemList.querySelector(getClassNamesSelector(config.classNames.placeholder));
36263630
if (placeholder) {
36273631
placeholder.remove();
36283632
}
36293633
}
3630-
else if (!existingItems) {
3631-
addItems = true;
3634+
else if (!addedItems && !existingItems && this._placeholderValue) {
3635+
addedItems = true;
36323636
addItemToFragment(mapInputToChoice({
36333637
selected: true,
36343638
value: '',
3635-
label: config.placeholderValue || '',
3639+
label: this._placeholderValue,
36363640
placeholder: true,
36373641
}, false));
36383642
}
36393643
}
3640-
if (addItems) {
3644+
if (addedItems) {
36413645
itemList.append(fragment);
36423646
if (config.shouldSortItems && !this._isSelectOneElement) {
36433647
items.sort(config.sorter);
@@ -3748,7 +3752,7 @@
37483752
_this._removeItem(itemToRemove);
37493753
_this._triggerChange(itemToRemove.value);
37503754
if (_this._isSelectOneElement && !_this._hasNonChoicePlaceholder) {
3751-
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return !choice.disabled && choice.placeholder; });
3755+
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return choice.placeholder; });
37523756
if (placeholderChoice) {
37533757
_this._addItem(placeholderChoice);
37543758
_this.unhighlightAll();

public/assets/scripts/choices.search-basic.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.

public/assets/scripts/choices.search-basic.mjs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ var Store = /** @class */ (function () {
12421242
* Get highlighted items from store
12431243
*/
12441244
get: function () {
1245-
return this.items.filter(function (item) { return !item.disabled && item.active && item.highlighted; });
1245+
return this.items.filter(function (item) { return item.active && item.highlighted; });
12461246
},
12471247
enumerable: false,
12481248
configurable: true
@@ -1269,7 +1269,7 @@ var Store = /** @class */ (function () {
12691269
});
12701270
Object.defineProperty(Store.prototype, "searchableChoices", {
12711271
/**
1272-
* Get choices that can be searched (excluding placeholders)
1272+
* Get choices that can be searched (excluding placeholders or disabled choices)
12731273
*/
12741274
get: function () {
12751275
return this.choices.filter(function (choice) { return !choice.disabled && !choice.placeholder; });
@@ -3350,7 +3350,11 @@ var Choices = /** @class */ (function () {
33503350
if (!isDefaultLabel || !isDefaultValue) {
33513351
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
33523352
}
3353-
_this._addChoice(mapInputToChoice(choice, false));
3353+
var choiceFull = mapInputToChoice(choice, false);
3354+
_this._addChoice(choiceFull);
3355+
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
3356+
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
3357+
}
33543358
}
33553359
});
33563360
_this.unhighlightAll();
@@ -3376,7 +3380,7 @@ var Choices = /** @class */ (function () {
33763380
var existingItems = {};
33773381
if (!deselectAll) {
33783382
_this._store.items.forEach(function (choice) {
3379-
if (choice.id && choice.active && choice.selected && !choice.disabled) {
3383+
if (choice.id && choice.active && choice.selected) {
33803384
existingItems[choice.value] = true;
33813385
}
33823386
});
@@ -3612,26 +3616,26 @@ var Choices = /** @class */ (function () {
36123616
};
36133617
// new items
36143618
items.forEach(addItemToFragment);
3615-
var addItems = !!fragment.childNodes.length;
3616-
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
3619+
var addedItems = !!fragment.childNodes.length;
3620+
if (this._isSelectOneElement) {
36173621
var existingItems = itemList.children.length;
3618-
if (addItems || existingItems > 1) {
3622+
if (addedItems || existingItems > 1) {
36193623
var placeholder = itemList.querySelector(getClassNamesSelector(config.classNames.placeholder));
36203624
if (placeholder) {
36213625
placeholder.remove();
36223626
}
36233627
}
3624-
else if (!existingItems) {
3625-
addItems = true;
3628+
else if (!addedItems && !existingItems && this._placeholderValue) {
3629+
addedItems = true;
36263630
addItemToFragment(mapInputToChoice({
36273631
selected: true,
36283632
value: '',
3629-
label: config.placeholderValue || '',
3633+
label: this._placeholderValue,
36303634
placeholder: true,
36313635
}, false));
36323636
}
36333637
}
3634-
if (addItems) {
3638+
if (addedItems) {
36353639
itemList.append(fragment);
36363640
if (config.shouldSortItems && !this._isSelectOneElement) {
36373641
items.sort(config.sorter);
@@ -3742,7 +3746,7 @@ var Choices = /** @class */ (function () {
37423746
_this._removeItem(itemToRemove);
37433747
_this._triggerChange(itemToRemove.value);
37443748
if (_this._isSelectOneElement && !_this._hasNonChoicePlaceholder) {
3745-
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return !choice.disabled && choice.placeholder; });
3749+
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return choice.placeholder; });
37463750
if (placeholderChoice) {
37473751
_this._addItem(placeholderChoice);
37483752
_this.unhighlightAll();

0 commit comments

Comments
 (0)