Skip to content

Commit

Permalink
Simplest implementation of clearable dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jlukic committed Sep 10, 2018
1 parent 35c583f commit 33d08ec
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,12 @@ $.fn.dropdown = function(parameters) {
},
icon: {
click: function(event) {
module.toggle();
if($icon.hasClass(className.clear)) {
module.clear();
}
else {
module.toggle();
}
}
},
text: {
Expand Down Expand Up @@ -2481,6 +2486,15 @@ $.fn.dropdown = function(parameters) {
$module.data(metadata.value, stringValue);
}
}
if(module.is.single() && settings.clearable) {
// treat undefined or '' as empty
if(!escapedValue) {
module.remove.clearable();
}
else {
module.set.clearable();
}
}
if(settings.fireOnInit === false && module.is.initialLoad()) {
module.verbose('No callback on initial load', settings.onChange);
}
Expand Down Expand Up @@ -2576,7 +2590,10 @@ $.fn.dropdown = function(parameters) {
}
})
;
}
},
clearable: function() {
$icon.addClass(className.clear);
},
},

add: {
Expand Down Expand Up @@ -2774,7 +2791,7 @@ $.fn.dropdown = function(parameters) {
}
module.set.value(newValue, addedValue, addedText, $selectedItem);
module.check.maxSelections();
}
},
},

remove: {
Expand Down Expand Up @@ -2999,6 +3016,9 @@ $.fn.dropdown = function(parameters) {
.removeAttr('tabindex')
;
}
},
clearable: function() {
$icon.removeClass(className.clear);
}
},

Expand Down Expand Up @@ -3686,6 +3706,8 @@ $.fn.dropdown.settings = {

values : false, // specify values to use for dropdown

clearable : true, // whether the value of the dropdown can be cleared

apiSettings : false,
selectOnKeydown : true, // Whether selection should occur automatically when keyboard shortcuts used
minCharacters : 0, // Minimum characters required to trigger API call
Expand Down Expand Up @@ -3838,6 +3860,7 @@ $.fn.dropdown.settings = {
active : 'active',
addition : 'addition',
animating : 'animating',
clear : 'clear',
disabled : 'disabled',
empty : 'empty',
dropdown : 'ui dropdown',
Expand Down

0 comments on commit 33d08ec

Please sign in to comment.