diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index c998404..693d6bf 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -385,8 +385,7 @@ class Choices { // Choices if (this.currentState.choices !== this.prevState.choices || this.currentState.groups !== this.prevState.groups) { - if (this.passedElement.type === 'select-multiple' || - this.passedElement.type === 'select-one') { + if (!this.isTextElement) { // Get active groups/choices const activeGroups = this.store.getGroupsFilteredByActive(); const activeChoices = this.store.getChoicesFilteredByActive(); @@ -414,16 +413,14 @@ class Choices { this.choiceList.appendChild(choiceListFragment); this._highlightChoice(); } else { - // Otherwise show a notice - let dropdownItem; - let notice; + const activeItems = this.store.getItemsFilteredByActive(); + const canAddItem = this._canAddItem(activeItems, this.input.value); + let dropdownItem = this._getTemplate('notice', this.config.noChoicesText); - if (this.isSearching) { - notice = isType('Function', this.config.noResultsText) ? this.config.noResultsText() : this.config.noResultsText; - dropdownItem = this._getTemplate('notice', notice); - } else { - notice = isType('Function', this.config.noChoicesText) ? this.config.noChoicesText() : this.config.noChoicesText; - dropdownItem = this._getTemplate('notice', notice); + if (canAddItem.notice) { + dropdownItem = this._getTemplate('notice', canAddItem.notice); + } else if (this.isSearching) { + dropdownItem = this._getTemplate('notice', this.config.noResultsText); } this.choiceList.appendChild(dropdownItem); @@ -1095,7 +1092,7 @@ class Choices { } } - if (this.passedElement.type === 'text' && this.config.addItems) { + if (this.config.addItems) { const isUnique = !activeItems.some((item) => item.value === value.trim()); // If a user has supplied a regular expression filter @@ -1207,7 +1204,8 @@ class Choices { this.currentValue = newValue; this.highlightPosition = 0; this.isSearching = true; - this.store.dispatch(filterChoices(results)); + + return results; } } @@ -1360,7 +1358,7 @@ class Choices { const onEnterKey = () => { // If enter key is pressed and the input has a value - if (passedElementType === 'text' && target.value) { + if (target.value) { const value = this.input.value; const canAddItem = this._canAddItem(activeItems, value); @@ -1369,7 +1367,14 @@ class Choices { if (hasActiveDropdown) { this.hideDropdown(); } - this._addItem(value); + + if (this.isTextElement) { + this._addItem(value); + } else { + this._addChoice(true, false, value, value); + console.log(this.store.getState()); + } + this._triggerChange(value); this.clearInput(this.passedElement); } diff --git a/index.html b/index.html index 7c1d7c4..cab5e59 100644 --- a/index.html +++ b/index.html @@ -444,6 +444,10 @@ var singleNoSorting = new Choices('#choices-single-no-sorting', { shouldSort: false, + addItems: true, + callbackOnChange: function(a, b) { + console.log(this.currentState); + } }); var states = new Choices(document.getElementById('states'));