Skip to content

Commit

Permalink
Show empty input for easy search on tom-select dropdowns
Browse files Browse the repository at this point in the history
One nice feature of tom-select is it provides a search
feature for dropdowns so you can start typing to find the
item you are looking for. However, by default when a dropdown
has an existing value, tom-select shows the value in the input
box and puts the cursor to the right. You can start typing to
find a new value, but it's confusing to see the old value
still displayed as you type to search for a new value.

Clear the value on focus so you can start typing immediately.
If no value is selected, restore the original value. This makes
dropdowns behave the same as RT 5.
  • Loading branch information
cbrandtbuffalo authored and sunnavy committed Feb 27, 2025
1 parent 18cada8 commit afdb84b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions share/static/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,27 @@ function initializeSelectElement(elt) {
// just a regular dropdown.
settings.controlInput = null;
}
else {
settings.onFocus = function() {
// When the user clicks on the menu, show an empty input to make it
// less confusing to start typing immediately to find a new value
// with autocomplete.
if (this.settings.maxItems === 1) { // single select
this.currentValue = this.getValue();
this.setValue(null, true);
}
};
settings.onChange = function(value) {
delete this.currentValue;
};
settings.onBlur = function() {
if (this.settings.maxItems === 1 && this.hasOwnProperty('currentValue')) {
// If no new value was selected, restore the original value
this.setValue(this.currentValue, true);
delete this.currentValue;
}
};
}

if (elt.classList.contains('rt-autocomplete')) {
settings.placeholder = elt.getAttribute('placeholder');
Expand Down

0 comments on commit afdb84b

Please sign in to comment.