Skip to content

Commit

Permalink
aded "options" for "dropdown.filterListItems" method, to allow exact …
Browse files Browse the repository at this point in the history
…complete match

refactored "normalizeTags" (for users list scenario with different types of initial values)
  • Loading branch information
yairEO committed Nov 5, 2020
1 parent 3bc9b05 commit 1e358c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
27 changes: 17 additions & 10 deletions dist/tagify.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,16 @@

/**
* returns an HTML string of the suggestions' list items
* @param {string} value string to filter the whitelist by
* @param {String} value string to filter the whitelist by
* @param {Object} options "exact" - for exact complete match
* @return {Array} list of filtered whitelist items according to the settings provided and current value
*/
filterListItems: function filterListItems(value) {
filterListItems: function filterListItems(value, options) {
var _this6 = this;

var _s = this.settings,
_sd = _s.dropdown,
options = options || {},
list = [],
whitelist = _s.whitelist,
suggestionsCount = _sd.maxItems || Infinity,
Expand Down Expand Up @@ -743,7 +745,7 @@
value: whitelist[i]
}; //normalize value as an Object

if (_sd.fuzzySearch) {
if (_sd.fuzzySearch && !options.exact) {
searchBy = searchKeys.reduce(function (values, k) {
return values + " " + (whitelistItem[k] || "");
}, "").toLowerCase();
Expand All @@ -764,7 +766,7 @@
}

if (!_sd.caseSensitive) v = v.toLowerCase();
return v.indexOf(niddle) == 0;
return options.exact ? v == niddle : v.indexOf(niddle) == 0;
});
}

Expand Down Expand Up @@ -2434,6 +2436,7 @@
delimiters = _this$settings.delimiters,
mode = _this$settings.mode,
tagTextProp = _this$settings.tagTextProp,
enforceWhitelist = _this$settings.enforceWhitelist,
whitelistMatches = [],
whitelistWithProps = whitelist ? whitelist[0] instanceof Object : false,
isArray = tagsItems instanceof Array,
Expand Down Expand Up @@ -2471,22 +2474,26 @@
}); // if suggestions are shown, they are already filtered, so it's easier to use them,
// because the whitelist might also include items which have already been added

var filteredList = _this6.dropdown.filterListItems.call(_this6, item[tagTextProp]) // also filter out items which have already been matches in previous iterations
var filteredList = _this6.dropdown.filterListItems.call(_this6, item[tagTextProp], {
exact: true
}) // also filter out items which have already been matches in previous iterations
.filter(function (filteredItem) {
return !whitelistMatchesValues.includes(filteredItem.value);
}); // get the best match out of list of possible matches
}); // get the best match out of list of possible matches.
// if there was a single item in the filtered list, use that one


var matchObj = _this6.getWhitelistItem(item[tagTextProp], null, filteredList);
var matchObj = filteredList.length > 1 ? _this6.getWhitelistItem(item[tagTextProp], tagTextProp, filteredList) : filteredList[0];

if (matchObj && matchObj instanceof Object) {
whitelistMatches.push(matchObj); // set the Array (with the found Object) as the new value
} else if (mode != 'mix') {
} else if (mode != 'mix' && !enforceWhitelist) {
if (item.value == undefined) item.value = item[tagTextProp];
whitelistMatches.push(item);
}
});
if (whitelistMatches.length) tagsItems = whitelistMatches;
}); // if( whitelistMatches.length )

tagsItems = whitelistMatches;
}

return tagsItems;
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ <h3>JAVASCRIPT</h3>

// initialize Tagify on the above input node reference
var tagify = new Tagify(inputElm, {
tagTextProp: 'name', // this allows typing a "value" or a "name" to match input with whitelist
tagTextProp: 'name', // very important since a custom template is used with this property as text
enforceWhitelist: true,
skipInvalid: true, // do not remporarily add invalid tags
dropdown: {
Expand Down
12 changes: 8 additions & 4 deletions src/parts/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,14 @@ export default {

/**
* returns an HTML string of the suggestions' list items
* @param {string} value string to filter the whitelist by
* @param {String} value string to filter the whitelist by
* @param {Object} options "exact" - for exact complete match
* @return {Array} list of filtered whitelist items according to the settings provided and current value
*/
filterListItems( value ){
filterListItems( value, options ){
var _s = this.settings,
_sd = _s.dropdown,
options = options || {},
list = [],
whitelist = _s.whitelist,
suggestionsCount = _sd.maxItems || Infinity,
Expand Down Expand Up @@ -544,7 +546,7 @@ export default {
for( ; i < whitelist.length; i++ ){
whitelistItem = whitelist[i] instanceof Object ? whitelist[i] : { value:whitelist[i] } //normalize value as an Object

if( _sd.fuzzySearch ){
if( _sd.fuzzySearch && !options.exact ){
searchBy = searchKeys.reduce((values, k) => values + " " + (whitelistItem[k]||""), "").toLowerCase()

if( _sd.accentedSearch ){
Expand All @@ -567,7 +569,9 @@ export default {
if( !_sd.caseSensitive )
v = v.toLowerCase()

return v.indexOf(niddle) == 0
return options.exact
? v == niddle
: v.indexOf(niddle) == 0
})
}

Expand Down
17 changes: 10 additions & 7 deletions src/tagify.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ Tagify.prototype = {
* @return {Array} [Array of Objects]
*/
normalizeTags( tagsItems ){
var {whitelist, delimiters, mode, tagTextProp} = this.settings,
var {whitelist, delimiters, mode, tagTextProp, enforceWhitelist} = this.settings,
whitelistMatches = [],
whitelistWithProps = whitelist ? whitelist[0] instanceof Object : false,
// checks if this is a "collection", meanning an Array of Objects
Expand Down Expand Up @@ -909,25 +909,28 @@ Tagify.prototype = {

// if suggestions are shown, they are already filtered, so it's easier to use them,
// because the whitelist might also include items which have already been added
var filteredList = this.dropdown.filterListItems.call(this, item[tagTextProp])
var filteredList = this.dropdown.filterListItems.call(this, item[tagTextProp], { exact:true })
// also filter out items which have already been matches in previous iterations
.filter(filteredItem => !whitelistMatchesValues.includes(filteredItem.value))

// get the best match out of list of possible matches
var matchObj = this.getWhitelistItem(item[tagTextProp], null, filteredList)
// get the best match out of list of possible matches.
// if there was a single item in the filtered list, use that one
var matchObj = filteredList.length > 1
? this.getWhitelistItem(item[tagTextProp], tagTextProp, filteredList)
: filteredList[0]

if( matchObj && matchObj instanceof Object ){
whitelistMatches.push( matchObj ) // set the Array (with the found Object) as the new value
}
else if( mode != 'mix' ){
else if( mode != 'mix' && !enforceWhitelist ){
if( item.value == undefined )
item.value = item[tagTextProp]
whitelistMatches.push(item)
}
})

if( whitelistMatches.length )
tagsItems = whitelistMatches
// if( whitelistMatches.length )
tagsItems = whitelistMatches
}

return tagsItems;
Expand Down

0 comments on commit 1e358c0

Please sign in to comment.