Skip to content

Commit

Permalink
fix(dropdown): split delimiter values on paste
Browse files Browse the repository at this point in the history
When pasting a string inside a search selection dropdown existing comma values (the default delimiter) is ignored. Even worse, when pressing enter afterwards the whole pasted string, containing delimiter values is used as a value itself.
This MR now supports the paste event and splits the pasted string into separate values and adds them when either allowAdditions:true or if the value exists in the dropdown menu.
Therefore the delimiter key is now also supported on multiple dropdowns which do not allow additions for consistency
  • Loading branch information
lubber-de committed Dec 1, 2021
1 parent 398ed43 commit 56226ae
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/definitions/modules/dropdown.js
Expand Up @@ -629,6 +629,11 @@ $.fn.dropdown = function(parameters) {
$module
.on('change' + eventNamespace, selector.input, module.event.change)
;
if(module.is.multiple() && module.is.searchSelection()) {
$module
.on('paste' + eventNamespace, selector.search, module.event.paste)
;
}
},
mouseEvents: function() {
module.verbose('Binding mouse events');
Expand Down Expand Up @@ -1077,6 +1082,15 @@ $.fn.dropdown = function(parameters) {
},

event: {
paste: function(event) {
var pasteValue = (event.originalEvent.clipboardData || window.clipboardData).getData('text'),
tokens = pasteValue.split(settings.delimiter)
;
tokens.forEach(function(value){
module.set.selected(module.escape.htmlEntities(value.trim()), null, true, true);
});
event.preventDefault();
},
change: function() {
if(!internalChange) {
module.debug('Input changed, updating selection');
Expand Down Expand Up @@ -1535,7 +1549,7 @@ $.fn.dropdown = function(parameters) {
hasSubMenu = ($subMenu.length> 0),
hasSelectedItem = ($selectedItem.length > 0),
selectedIsSelectable = ($selectedItem.not(selector.unselectable).length > 0),
delimiterPressed = (pressedKey == keys.delimiter && settings.allowAdditions && module.is.multiple()),
delimiterPressed = (pressedKey == keys.delimiter && module.is.multiple()),
isAdditionWithoutMenu = (settings.allowAdditions && settings.hideAdditions && (pressedKey == keys.enter || delimiterPressed) && selectedIsSelectable),
$nextItem,
isSubMenuItem,
Expand Down

0 comments on commit 56226ae

Please sign in to comment.