Skip to content

Commit cd8443d

Browse files
committed
Make all select boxes searchable by typing
1 parent 6134b73 commit cd8443d

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

app/assets/stylesheets/src/dark-theme-overrides.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@mixin tom-select-dark-mode() {
22

33
#model_tag_list-ts-dropdown,
4+
.single .ts-control div[data-ts-item],
45
.ts-wrapper .ts-control input,
56
.ts-wrapper .ts-dropdown .ts-dropdown-content .option {
67
color: #fff !important;

app/helpers/application_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def select_input_row(form, name, select_options, options = {})
172172
safe_join [
173173
content_tag(:div, class: "input-group") do
174174
safe_join [
175-
form.select(name, select_options, options.compact, {class: "form-control col-auto form-select #{"is-invalid" if form.object&.errors&.include?(name) && !form.object.errors[name].empty?}"}),
175+
form.select(name, select_options, options.compact, {data: {controller: "searchable-select"}, class: "form-control col-auto form-select #{"is-invalid" if form.object&.errors&.include?(name) && !form.object.errors[name].empty?}"}),
176176
(link_to(options[:button][:label], options[:button][:path], class: "btn btn-outline-secondary col-auto") if options[:button])
177177
]
178178
end,
@@ -192,7 +192,7 @@ def collection_select_input_row(form, name, collection, value_method, text_metho
192192
safe_join [
193193
content_tag(:div, class: "input-group") do
194194
safe_join [
195-
form.collection_select(:"#{name}_id", collection, value_method, text_method, options.compact, {class: "form-control col-auto form-select #{"is-invalid" if form.object&.errors&.include?(name) && !form.object.errors[name].empty?}"}),
195+
form.collection_select(:"#{name}_id", collection, value_method, text_method, options.compact, {data: {controller: "searchable-select"}, class: "form-control col-auto form-select #{"is-invalid" if form.object&.errors&.include?(name) && !form.object.errors[name].empty?}"}),
196196
(link_to(options[:button][:label], options[:button][:path], class: "btn btn-outline-secondary col-auto") if options[:button])
197197
]
198198
end,

app/javascript/controllers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CopyTextController from './copy_text_controller'
1111
import EditableController from './editable_controller'
1212
import I18nController from './i18n_controller'
1313
import RendererController from './renderer_controller'
14+
import SearchableSelectController from './searchable_select_controller'
1415
import StorageServiceController from './storage_service_controller'
1516
import TagInputController from './tag_input_controller'
1617
import TagSectionController from './tag_section_controller'
@@ -24,6 +25,7 @@ application.register('bulk-edit', BulkEditController)
2425
application.register('editable', EditableController)
2526
application.register('i18n', I18nController)
2627
application.register('renderer', RendererController)
28+
application.register('searchable-select', SearchableSelectController)
2729
application.register('storage-service', StorageServiceController)
2830
application.register('tag-input', TagInputController)
2931
application.register('tag-section', TagSectionController)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Controller } from '@hotwired/stimulus'
2+
3+
import TomSelect from 'tom-select'
4+
import type { TomInput } from 'tom-select/dist/cjs/types'
5+
6+
// Connects to data-controller="searchable-select"
7+
export default class extends Controller {
8+
tomSelect: TomSelect | null
9+
10+
connect (): void {
11+
this.tomSelect = new TomSelect((this.element as TomInput), { // eslint-disable-line no-new
12+
selectOnTab: true
13+
})
14+
}
15+
16+
disconnect (): void {
17+
this.tomSelect?.destroy()
18+
}
19+
20+
reconnect (): void {
21+
this.disconnect()
22+
this.connect()
23+
}
24+
}

0 commit comments

Comments
 (0)