mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-07 00:12:40 +02:00
Minor refactoring
This commit is contained in:
parent
d48fdf4e02
commit
82cb17ec2b
|
@ -825,22 +825,16 @@ class Choices {
|
||||||
// Check 'value' property exists and the choice isn't already selected
|
// Check 'value' property exists and the choice isn't already selected
|
||||||
const foundChoice = choices.find(choice => choice.value === val);
|
const foundChoice = choices.find(choice => choice.value === val);
|
||||||
|
|
||||||
if (foundChoice) {
|
if (foundChoice && !foundChoice.selected) {
|
||||||
if (!foundChoice.selected) {
|
this._addItem(
|
||||||
this._addItem(
|
foundChoice.value,
|
||||||
foundChoice.value,
|
foundChoice.label,
|
||||||
foundChoice.label,
|
foundChoice.id,
|
||||||
foundChoice.id,
|
foundChoice.groupId,
|
||||||
foundChoice.groupId,
|
foundChoice.customProperties,
|
||||||
foundChoice.customProperties,
|
foundChoice.placeholder,
|
||||||
foundChoice.placeholder,
|
foundChoice.keyCode,
|
||||||
foundChoice.keyCode,
|
);
|
||||||
);
|
|
||||||
} else if (!this.config.silent) {
|
|
||||||
console.warn('Attempting to select choice already selected');
|
|
||||||
}
|
|
||||||
} else if (!this.config.silent) {
|
|
||||||
console.warn('Attempting to select choice that does not exist');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1445,8 +1439,6 @@ class Choices {
|
||||||
const hasActiveDropdown = this.dropdown.isActive;
|
const hasActiveDropdown = this.dropdown.isActive;
|
||||||
const hasItems = this.itemList.hasChildren;
|
const hasItems = this.itemList.hasChildren;
|
||||||
const keyString = String.fromCharCode(e.keyCode);
|
const keyString = String.fromCharCode(e.keyCode);
|
||||||
|
|
||||||
// TO DO: Move into constants file
|
|
||||||
const backKey = KEY_CODES.BACK_KEY;
|
const backKey = KEY_CODES.BACK_KEY;
|
||||||
const deleteKey = KEY_CODES.DELETE_KEY;
|
const deleteKey = KEY_CODES.DELETE_KEY;
|
||||||
const enterKey = KEY_CODES.ENTER_KEY;
|
const enterKey = KEY_CODES.ENTER_KEY;
|
||||||
|
@ -2366,6 +2358,7 @@ class Choices {
|
||||||
/* placeholder */ true,
|
/* placeholder */ true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
passedGroups.forEach((group) => {
|
passedGroups.forEach((group) => {
|
||||||
this._addGroup(group, (group.id || null));
|
this._addGroup(group, (group.id || null));
|
||||||
});
|
});
|
||||||
|
@ -2392,9 +2385,7 @@ class Choices {
|
||||||
|
|
||||||
// Determine whether there is a selected choice
|
// Determine whether there is a selected choice
|
||||||
const hasSelectedChoice = allChoices.some(choice => choice.selected);
|
const hasSelectedChoice = allChoices.some(choice => choice.selected);
|
||||||
|
const handleChoice = (choice, index) => {
|
||||||
// Add each choice
|
|
||||||
allChoices.forEach((choice, index) => {
|
|
||||||
if (this.isSelectElement) {
|
if (this.isSelectElement) {
|
||||||
// If the choice is actually a group
|
// If the choice is actually a group
|
||||||
if (choice.choices) {
|
if (choice.choices) {
|
||||||
|
@ -2428,11 +2419,13 @@ class Choices {
|
||||||
choice.placeholder,
|
choice.placeholder,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Add each choice
|
||||||
|
allChoices.forEach((choice, index) => handleChoice(choice, index));
|
||||||
}
|
}
|
||||||
} else if (this.isTextElement) {
|
} else if (this.isTextElement) {
|
||||||
// Add any preset values seperated by delimiter
|
const handlePresetItem = (item) => {
|
||||||
this.presetItems.forEach((item) => {
|
|
||||||
const itemType = getType(item);
|
const itemType = getType(item);
|
||||||
if (itemType === 'Object') {
|
if (itemType === 'Object') {
|
||||||
if (!item.value) {
|
if (!item.value) {
|
||||||
|
@ -2449,7 +2442,9 @@ class Choices {
|
||||||
} else if (itemType === 'String') {
|
} else if (itemType === 'String') {
|
||||||
this._addItem(item);
|
this._addItem(item);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
this.presetItems.forEach(item => handlePresetItem(item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue