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,
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 {

View file

@ -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;
})
};