mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-02 22:03:11 +02:00
Refactor _canAddItem
This commit is contained in:
parent
ba67ff4a4d
commit
1df8d81ae4
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
})
|
||||
};
|
Loading…
Reference in a new issue