Refactor _canAddItem

This commit is contained in:
Josh Johnson 2018-05-27 17:22:58 +01:00
parent ba67ff4a4d
commit 1df8d81ae4
2 changed files with 29 additions and 26 deletions

View file

@ -22,6 +22,7 @@ import {
findAncestorByAttrName, findAncestorByAttrName,
regexFilter, regexFilter,
isIE11, isIE11,
existsInArray,
} from './lib/utils'; } from './lib/utils';
/** /**
@ -758,7 +759,9 @@ class Choices {
this.config.addItemText(value) : this.config.addItemText(value) :
this.config.addItemText; 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 (this.config.maxItemCount > 0 && this.config.maxItemCount <= activeItems.length) {
// If there is a max entry limit and we have reached that limit // If there is a max entry limit and we have reached that limit
// don't update // don't update
@ -767,34 +770,24 @@ class Choices {
this.config.maxItemText(this.config.maxItemCount) : this.config.maxItemText(this.config.maxItemCount) :
this.config.maxItemText; this.config.maxItemText;
} }
}
if (this.config.regexFilter && this._isTextElement && this.config.addItems && canAddItem) { if (this.config.regexFilter && this._isTextElement && this.config.addItems && canAddItem) {
// If a user has supplied a regular expression filter // If a user has supplied a regular expression filter
// determine whether we can update based on whether // determine whether we can update based on whether
// our regular expression passes // our regular expression passes
canAddItem = regexFilter(value, this.config.regexFilter); 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();
} }
return item.value === value; if (
}); !this.config.duplicateItems &&
!valueAlreadyExists &&
if (!isUnique && canAddItem
!this.config.duplicateItems && ) {
!this._isSelectOneElement && canAddItem = false;
canAddItem notice = isType('Function', this.config.uniqueItemText) ?
) { this.config.uniqueItemText(value) :
canAddItem = false; this.config.uniqueItemText;
notice = isType('Function', this.config.uniqueItemText) ? }
this.config.uniqueItemText(value) :
this.config.uniqueItemText;
} }
return { return {

View file

@ -351,4 +351,14 @@ export const reduceToValues = (items, key = 'value') => {
export const isIE11 = () => { export const isIE11 = () => {
return !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/)); 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;
})
}; };