Skip to content

Commit ba59b5a

Browse files
committed
Update single select search behaviour
This change brings a behaviour similar to the one that common HTML selects have, on top of some of TomSelect niceties. By default, TomSelect would include a search input in single select mode, even when there is an option selected, which is uncommon. So instead, we keep search box visible only when there is nothing selected. As soon as user selects an option, select box gains "opacity-0" and therefore hides the confusing search box effects (caret and ability to type ahead). However, since a usual HTML select responds to user's typing by highlighting matched option (if any), we can replicate this logic because technically the input is not gone, and it still responds to user input. If user types while the select is focused, the options will be filtered and highlight will be applied (TomSelect defaults), however if filter search returns 0 options - we reset the search term to empty string and options displayed to show full list again.
1 parent 1513722 commit ba59b5a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

admin/app/components/solidus_admin/ui/forms/select/component.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def initialize(label:, name:, choices:, size: :m, hint: nil, tip: nil, error: ni
6868
input_classes = ["[&_input]:has-[.item]:placeholder:invisible [&_input:disabled]:cursor-not-allowed [&_input:disabled]:bg-gray-50
6969
[&_input]:peer-invalid:placeholder:text-red-400"]
7070

71+
unless @attributes[:multiple]
72+
input_classes << "[&_input]:has-[.item]:opacity-0 [&_input]:has-[.item]:cursor-default"
73+
end
74+
7175
dropdown_classes = ["[&_.dropdown]:w-full [&_.dropdown]:absolute [&_.dropdown]:border [&_.dropdown]:border-gray-100
7276
[&_.dropdown]:mt-0.5 [&_.dropdown]:min-w-[10rem] [&_.dropdown]:p-2 [&_.dropdown]:rounded-sm [&_.dropdown]:z-10
7377
[&_.dropdown]:shadow-lg [&_.dropdown]:bg-white"]

admin/app/javascript/solidus_admin/web_components/solidus_select.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class SolidusSelect extends HTMLSelectElement {
2626
this.setTextboxValue("");
2727
if (originalSelect.multiple) this.refreshOptions();
2828
},
29+
onType: function() {
30+
if (!originalSelect.multiple && !this.currentResults.items.length) {
31+
this.setTextboxValue("");
32+
this.refreshOptions();
33+
}
34+
},
2935
});
3036

3137
originalSelect.setAttribute("synced", "true");

0 commit comments

Comments
 (0)