From 565d659c0ade7e93343238acbca6f9f92875bf65 Mon Sep 17 00:00:00 2001
From: Josh Johnson
Date: Thu, 3 Aug 2017 10:56:49 +0100
Subject: [PATCH] Fix search + use choice placeholder on remove button
---
assets/scripts/src/choices.js | 55 +++--
index.html | 453 +++++++++++++++++-----------------
2 files changed, 264 insertions(+), 244 deletions(-)
diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js
index 20249eb..5b69f4c 100644
--- a/assets/scripts/src/choices.js
+++ b/assets/scripts/src/choices.js
@@ -174,13 +174,12 @@ class Choices {
this.highlightPosition = 0;
this.canSearch = this.config.searchEnabled;
- this.placeholder = this.config.placeholder ?
- (this.config.placeholderValue || this.passedElement.getAttribute('placeholder')) :
- false;
this.placeholder = false;
if (!this.isSelectOneElement) {
- this.placeholder = (this.config.placeholderValue || this.passedElement.getAttribute('placeholder')) || false;
+ this.placeholder = this.config.placeholder ?
+ (this.config.placeholderValue || this.passedElement.getAttribute('placeholder')) :
+ false;
}
// Assign preset choices from passed object
@@ -406,14 +405,16 @@ class Choices {
const sortedChoices = [...placeholderChoices, ...normalChoices];
if (this.isSearching) {
- choiceLimit = Math.min(searchResultLimit, sortedChoices.length - 1);
+ choiceLimit = searchResultLimit;
} else if (renderChoiceLimit > 0 && !withinGroup) {
- choiceLimit = Math.min(renderChoiceLimit, sortedChoices.length - 1);
+ choiceLimit = renderChoiceLimit;
}
// Add each choice to dropdown within range
for (let i = 0; i < choiceLimit; i++) {
- appendChoice(sortedChoices[i]);
+ if (sortedChoices[i]) {
+ appendChoice(sortedChoices[i]);
+ }
};
return choicesFragment;
@@ -1148,13 +1149,31 @@ class Choices {
this._removeItem(itemToRemove);
this._triggerChange(itemToRemove.value);
- if (this.isSelectOneElement && this.placeholder) {
- const placeholderItem = this._getTemplate('placeholder', this.placeholder);
- this.itemList.appendChild(placeholderItem);
+ if (this.isSelectOneElement) {
+ this._selectPlaceholderChoice();
}
}
}
+ /**
+ * Select placeholder choice
+ */
+ _selectPlaceholderChoice() {
+ const placeholderChoice = this.store.getPlaceholderChoice();
+
+ if (placeholderChoice) {
+ this._addItem(
+ placeholderChoice.value,
+ placeholderChoice.label,
+ placeholderChoice.id,
+ placeholderChoice.groupId,
+ null,
+ placeholderChoice.placeholder
+ );
+ this._triggerChange(placeholderChoice.value);
+ }
+ }
+
/**
* Process click of an item
* @param {Array} activeItems The currently active items
@@ -1402,20 +1421,8 @@ class Choices {
);
}
- if (this.passedElement.type === 'select-one') {
- const placeholderChoice = this.store.getPlaceholderChoice();
-
- if (placeholderChoice) {
- this._addItem(
- placeholderChoice.value,
- placeholderChoice.label,
- placeholderChoice.id,
- placeholderChoice.groupId,
- null,
- placeholderChoice.placeholder
- );
- this._triggerChange(placeholderChoice.value);
- }
+ if (this.isSelectOneElement) {
+ this._selectPlaceholderChoice();
}
});
} else {
diff --git a/index.html b/index.html
index 8e3edd5..1c3be52 100644
--- a/index.html
+++ b/index.html
@@ -53,7 +53,7 @@
- Text inputs
+
+
+