Skip to content

Commit 43f6188

Browse files
committed
Revert add items functionality - buggy
1 parent 13288ed commit 43f6188

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

assets/scripts/src/choices.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -408,21 +408,36 @@ class Choices {
408408
choiceListFragment = this.renderChoices(activeChoices, choiceListFragment);
409409
}
410410

411+
const activeItems = this.store.getItemsFilteredByActive();
412+
const canAddItem = this._canAddItem(activeItems, this.input.value);
413+
414+
// If we have choices to show
411415
if (choiceListFragment.childNodes && choiceListFragment.childNodes.length > 0) {
412-
// If we actually have anything to add to our dropdown
413-
// append it and highlight the first choice
414-
this.choiceList.appendChild(choiceListFragment);
415-
this._highlightChoice();
416+
// ...and we can select them
417+
if (canAddItem.response) {
418+
// ...append them and highlight the first choice
419+
this.choiceList.appendChild(choiceListFragment);
420+
this._highlightChoice();
421+
} else {
422+
// ...otherwise show a notice
423+
this.choiceList.appendChild(this._getTemplate('notice', canAddItem.notice));
424+
}
416425
} else {
417426
// Otherwise show a notice
418427
let dropdownItem;
419428
let notice;
420429

421430
if (this.isSearching) {
422-
notice = isType('Function', this.config.noResultsText) ? this.config.noResultsText() : this.config.noResultsText;
431+
notice = isType('Function', this.config.noResultsText) ?
432+
this.config.noResultsText() :
433+
this.config.noResultsText;
434+
423435
dropdownItem = this._getTemplate('notice', notice);
424436
} else {
425-
notice = isType('Function', this.config.noChoicesText) ? this.config.noChoicesText() : this.config.noChoicesText;
437+
notice = isType('Function', this.config.noChoicesText) ?
438+
this.config.noChoicesText() :
439+
this.config.noChoicesText;
440+
426441
dropdownItem = this._getTemplate('notice', notice);
427442
}
428443

@@ -1085,15 +1100,17 @@ class Choices {
10851100
let notice = isType('Function', this.config.addItemText) ? this.config.addItemText(value) : this.config.addItemText;
10861101

10871102
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
1088-
if (this.config.maxItemCount > 0 && this.config.maxItemCount <= this.itemList.children.length) {
1103+
if (this.config.maxItemCount > 0 && this.config.maxItemCount <= activeItems.length) {
10891104
// If there is a max entry limit and we have reached that limit
10901105
// don't update
10911106
canAddItem = false;
1092-
notice = isType('Function', this.config.maxItemText) ? this.config.maxItemText(this.config.maxItemCount) : this.config.maxItemText;
1107+
notice = isType('Function', this.config.maxItemText) ?
1108+
this.config.maxItemText(this.config.maxItemCount) :
1109+
this.config.maxItemText;
10931110
}
10941111
}
10951112

1096-
if (this.passedElement.type === 'text' && this.config.addItems) {
1113+
if (this.passedElement.type === 'text' && this.config.addItems && canAddItem) {
10971114
const isUnique = !activeItems.some((item) => item.value === value.trim());
10981115

10991116
// If a user has supplied a regular expression filter
@@ -1482,15 +1499,15 @@ class Choices {
14821499
_onKeyUp(e) {
14831500
if (e.target !== this.input) return;
14841501

1502+
const value = this.input.value;
1503+
const activeItems = this.store.getItemsFilteredByActive();
1504+
const canAddItem = this._canAddItem(activeItems, value);
1505+
14851506
// We are typing into a text input and have a value, we want to show a dropdown
14861507
// notice. Otherwise hide the dropdown
14871508
if (this.isTextElement) {
14881509
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
1489-
const value = this.input.value;
1490-
14911510
if (value) {
1492-
const activeItems = this.store.getItemsFilteredByActive();
1493-
const canAddItem = this._canAddItem(activeItems, value);
14941511

14951512
if (canAddItem.notice) {
14961513
const dropdownItem = this._getTemplate('notice', canAddItem.notice);
@@ -1518,7 +1535,7 @@ class Choices {
15181535
this.isSearching = false;
15191536
this.store.dispatch(activateChoices(true));
15201537
}
1521-
} else if (this.canSearch) {
1538+
} else if (this.canSearch && canAddItem.response) {
15221539
this._handleSearch(this.input.value);
15231540
}
15241541
}

0 commit comments

Comments
 (0)