Fix being able to add an existing label

This commit is contained in:
Stéphane Trébel 2016-12-14 14:28:01 +01:00 committed by Josh Johnson
parent 10af1f4104
commit 304d0b4d5d
4 changed files with 41 additions and 15 deletions

View file

@ -1300,7 +1300,7 @@ return /******/ (function(modules) { // webpackBootstrap
if (this.config.addItems) {
var isUnique = !activeItems.some(function (item) {
return item.value === value.trim();
return item.value === value.trim() || item.label === value.trim();
});
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
@ -1681,17 +1681,24 @@ return /******/ (function(modules) { // webpackBootstrap
if (_this17.isTextElement) {
_this17._addItem(value);
} else {
var existingChoice = void 0;
var matchingChoices = [];
var isUnique = void 0;
var duplicateItems = _this17.config.duplicateItems;
if (!duplicateItems) {
existingChoice = !_this17.store.getItems().filter(function (item) {
return item.value === value.trim();
matchingChoices = _this17.store.getChoices().filter(function (choice) {
return choice.label === value.trim();
});
isUnique = !_this17.store.getItemsFilteredByActive().some(function (item) {
return item.label === value.trim();
});
}
if (duplicateItems) {
if (duplicateItems || matchingChoices.length === 0 && isUnique) {
_this17._addChoice(true, false, value, value);
} else {
_this17._addItem(existingChoice.value, existingChoice.label, existingChoice.id);
}
if (duplicateItems || isUnique) {
if (matchingChoices[0]) {
_this17._addItem(matchingChoices[0].value, matchingChoices[0].label, matchingChoices[0].id);
}
}
_this17.containerOuter.focus();
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1086,7 +1086,7 @@ class Choices {
let notice = isType('Function', this.config.addItemText) ? this.config.addItemText(value) : this.config.addItemText;
if (this.config.addItems) {
const isUnique = !activeItems.some((item) => item.value === value.trim());
const isUnique = !activeItems.some((item) => (item.value === value.trim()) || (item.label === value.trim()));
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
if (this.config.maxItemCount > 0 && this.config.maxItemCount <= this.itemList.children.length) {
@ -1390,17 +1390,28 @@ class Choices {
if (this.isTextElement) {
this._addItem(value);
} else {
let existingChoice;
let matchingChoices = [];
let isUnique;
const duplicateItems = this.config.duplicateItems;
if (!duplicateItems) {
existingChoice = !this.store
.getItems()
.filter((item) => item.value === value.trim());
matchingChoices = this.store
.getChoices()
.filter((choice) => choice.label === value.trim());
isUnique = !this.store
.getItemsFilteredByActive()
.some((item) => item.label === value.trim());
}
if (duplicateItems) {
if (duplicateItems || (matchingChoices.length === 0 && isUnique)) {
this._addChoice(true, false, value, value);
} else {
this._addItem(existingChoice.value, existingChoice.label, existingChoice.id);
}
if (duplicateItems || isUnique) {
if (matchingChoices[0]) {
this._addItem(
matchingChoices[0].value,
matchingChoices[0].label,
matchingChoices[0].id
);
}
}
this.containerOuter.focus();
}