Semi-working prototype

This commit is contained in:
Josh Johnson 2016-11-08 14:23:25 +00:00 committed by Josh Johnson
parent 01dad3ac77
commit 217015caef
2 changed files with 24 additions and 15 deletions

View file

@ -385,8 +385,7 @@ class Choices {
// Choices // Choices
if (this.currentState.choices !== this.prevState.choices || if (this.currentState.choices !== this.prevState.choices ||
this.currentState.groups !== this.prevState.groups) { this.currentState.groups !== this.prevState.groups) {
if (this.passedElement.type === 'select-multiple' || if (!this.isTextElement) {
this.passedElement.type === 'select-one') {
// Get active groups/choices // Get active groups/choices
const activeGroups = this.store.getGroupsFilteredByActive(); const activeGroups = this.store.getGroupsFilteredByActive();
const activeChoices = this.store.getChoicesFilteredByActive(); const activeChoices = this.store.getChoicesFilteredByActive();
@ -414,16 +413,14 @@ class Choices {
this.choiceList.appendChild(choiceListFragment); this.choiceList.appendChild(choiceListFragment);
this._highlightChoice(); this._highlightChoice();
} else { } else {
// Otherwise show a notice const activeItems = this.store.getItemsFilteredByActive();
let dropdownItem; const canAddItem = this._canAddItem(activeItems, this.input.value);
let notice; let dropdownItem = this._getTemplate('notice', this.config.noChoicesText);
if (this.isSearching) { if (canAddItem.notice) {
notice = isType('Function', this.config.noResultsText) ? this.config.noResultsText() : this.config.noResultsText; dropdownItem = this._getTemplate('notice', canAddItem.notice);
dropdownItem = this._getTemplate('notice', notice); } else if (this.isSearching) {
} else { dropdownItem = this._getTemplate('notice', this.config.noResultsText);
notice = isType('Function', this.config.noChoicesText) ? this.config.noChoicesText() : this.config.noChoicesText;
dropdownItem = this._getTemplate('notice', notice);
} }
this.choiceList.appendChild(dropdownItem); 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()); const isUnique = !activeItems.some((item) => item.value === value.trim());
// If a user has supplied a regular expression filter // If a user has supplied a regular expression filter
@ -1207,7 +1204,8 @@ class Choices {
this.currentValue = newValue; this.currentValue = newValue;
this.highlightPosition = 0; this.highlightPosition = 0;
this.isSearching = true; this.isSearching = true;
this.store.dispatch(filterChoices(results));
return results;
} }
} }
@ -1360,7 +1358,7 @@ class Choices {
const onEnterKey = () => { const onEnterKey = () => {
// If enter key is pressed and the input has a value // 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 value = this.input.value;
const canAddItem = this._canAddItem(activeItems, value); const canAddItem = this._canAddItem(activeItems, value);
@ -1369,7 +1367,14 @@ class Choices {
if (hasActiveDropdown) { if (hasActiveDropdown) {
this.hideDropdown(); 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._triggerChange(value);
this.clearInput(this.passedElement); this.clearInput(this.passedElement);
} }

View file

@ -444,6 +444,10 @@
var singleNoSorting = new Choices('#choices-single-no-sorting', { var singleNoSorting = new Choices('#choices-single-no-sorting', {
shouldSort: false, shouldSort: false,
addItems: true,
callbackOnChange: function(a, b) {
console.log(this.currentState);
}
}); });
var states = new Choices(document.getElementById('states')); var states = new Choices(document.getElementById('states'));