Skip to content

Commit

Permalink
feat(dropdown): highlightMatches option
Browse files Browse the repository at this point in the history
  • Loading branch information
lubber-de committed Dec 11, 2023
1 parent 7259033 commit e8a29e6
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@
regExpFlags = (settings.ignoreSearchCase ? 'i' : '') + 'gm',
beginsWithRegExp = new RegExp('^' + escapedTerm, regExpFlags)
;
module.remove.filteredItem();
// avoid loop if we're matching nothing
if (module.has.query()) {
results = [];
Expand Down Expand Up @@ -938,12 +939,33 @@
;
}
module.debug('Showing only matched items', searchTerm);
module.remove.filteredItem();
if (results) {
$item
.not(results)
.addClass(className.filtered)
;
if (settings.highlightMatches && (settings.match === 'both' || settings.match === 'text')) {
var querySplit = query.split(''),
diacriticReg = settings.ignoreDiacritics ? '[\u0300-\u036F]?' : '',
htmlReg = '(?![^<]*>)',
markedRegExp = new RegExp(htmlReg + '(' + querySplit.join(diacriticReg + ')(.*)' + htmlReg +'(') + diacriticReg + ')', regExpFlags),

Check failure on line 951 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Operator '+' must be spaced
markedReplacer = function(){

Check failure on line 952 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses

Check failure on line 952 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace
var args = [].slice.call(arguments,1, querySplit.length * 2).map(function(x, i){

Check failure on line 953 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

A space is required after ','

Check failure on line 953 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses

Check failure on line 953 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace
return i & 1 ? x : '<mark>' + x + '</mark>'})

Check failure on line 954 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected use of '&'

Check failure on line 954 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Requires a space before '}'

Check failure on line 954 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Closing curly brace should be on the same line as opening curly brace or on the line after the previous block

Check failure on line 954 in src/definitions/modules/dropdown.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
;
return args.join('');
}
;
$.each(results, function(index, result) {
var $result = $(result),
markedHTML = module.get.choiceText($result)
;
if (settings.ignoreDiacritics) {
markedHTML = markedHTML.normalize('NFD');
}
$result.html(markedHTML.replace(markedRegExp, markedReplacer));
});
}
}

if (!module.has.query()) {
Expand Down Expand Up @@ -3080,6 +3102,12 @@
$item.removeClass(className.active);
},
filteredItem: function () {
if (settings.highlightMatches) {
$.each($item, function (index, item) {
var $markItem = $(item);
$markItem.html($markItem.html().replace(/<\/?mark>/g, ''));
});
}
if (settings.useLabels && module.has.maxSelections()) {
return;
}
Expand Down Expand Up @@ -4005,6 +4033,7 @@

match: 'both', // what to match against with search selection (both, text, or label)
fullTextSearch: 'exact', // search anywhere in value (set to 'exact' to require exact matches)
highlightMatches: false, // Whether search result should highlight matching strings
ignoreDiacritics: false, // match results also if they contain diacritics of the same base character (for example searching for "a" will also match "á" or "â" or "à", etc...)
hideDividers: false, // Whether to hide any divider elements (specified in selector.divider) that are sibling to any items when searched (set to true will hide all dividers, set to 'empty' will hide them when they are not followed by a visible item)

Expand Down

0 comments on commit e8a29e6

Please sign in to comment.