From 1df8d81ae4908b2eb98bc6240d9b412bf0ef9a21 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Sun, 27 May 2018 17:22:58 +0100 Subject: [PATCH] Refactor _canAddItem --- src/scripts/choices.js | 45 +++++++++++++++++----------------------- src/scripts/lib/utils.js | 10 +++++++++ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/scripts/choices.js b/src/scripts/choices.js index 3192c94..5dc05d3 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -22,6 +22,7 @@ import { findAncestorByAttrName, regexFilter, isIE11, + existsInArray, } from './lib/utils'; /** @@ -758,7 +759,9 @@ class Choices { this.config.addItemText(value) : this.config.addItemText; - if (this._isSelectMultipleElement || this._isTextElement) { + if (!this._isSelectOneElement) { + const valueAlreadyExists = !existsInArray(activeItems, value); + if (this.config.maxItemCount > 0 && this.config.maxItemCount <= activeItems.length) { // If there is a max entry limit and we have reached that limit // don't update @@ -767,34 +770,24 @@ class Choices { this.config.maxItemText(this.config.maxItemCount) : this.config.maxItemText; } - } - if (this.config.regexFilter && this._isTextElement && this.config.addItems && canAddItem) { - // If a user has supplied a regular expression filter - // determine whether we can update based on whether - // our regular expression passes - canAddItem = regexFilter(value, this.config.regexFilter); - } - - // If no duplicates are allowed, and the value already exists - // in the array - const isUnique = !activeItems.some((item) => { - if (isType('String', value)) { - return item.value === value.trim(); + if (this.config.regexFilter && this._isTextElement && this.config.addItems && canAddItem) { + // If a user has supplied a regular expression filter + // determine whether we can update based on whether + // our regular expression passes + canAddItem = regexFilter(value, this.config.regexFilter); } - return item.value === value; - }); - - if (!isUnique && - !this.config.duplicateItems && - !this._isSelectOneElement && - canAddItem - ) { - canAddItem = false; - notice = isType('Function', this.config.uniqueItemText) ? - this.config.uniqueItemText(value) : - this.config.uniqueItemText; + if ( + !this.config.duplicateItems && + !valueAlreadyExists && + canAddItem + ) { + canAddItem = false; + notice = isType('Function', this.config.uniqueItemText) ? + this.config.uniqueItemText(value) : + this.config.uniqueItemText; + } } return { diff --git a/src/scripts/lib/utils.js b/src/scripts/lib/utils.js index 6779106..412ea99 100644 --- a/src/scripts/lib/utils.js +++ b/src/scripts/lib/utils.js @@ -351,4 +351,14 @@ export const reduceToValues = (items, key = 'value') => { export const isIE11 = () => { return !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/)); +}; + +export const existsInArray = (array, value) => { + return array.some((item) => { + if (isType('String', value)) { + return item.value === value.trim(); + } + + return item.value === value; + }) }; \ No newline at end of file