diff --git a/lib/ruby_ui/combobox/combobox.rb b/lib/ruby_ui/combobox/combobox.rb index e3156e93..fa440c49 100644 --- a/lib/ruby_ui/combobox/combobox.rb +++ b/lib/ruby_ui/combobox/combobox.rb @@ -2,7 +2,7 @@ module RubyUI class Combobox < Base - def initialize(term: "items", **) + def initialize(term: nil, **) @term = term super(**) end @@ -18,7 +18,8 @@ def default_attrs role: "combobox", data: { controller: "ruby-ui--combobox", - ruby_ui__combobox_term_value: @term.to_s + ruby_ui__combobox_term_value: @term, + action: "turbo:morph@window->ruby-ui--combobox#updateTriggerContent" } } end diff --git a/lib/ruby_ui/combobox/combobox_controller.js b/lib/ruby_ui/combobox/combobox_controller.js index 7e233164..8d1d9d32 100644 --- a/lib/ruby_ui/combobox/combobox_controller.js +++ b/lib/ruby_ui/combobox/combobox_controller.js @@ -53,12 +53,12 @@ export default class extends Controller { updateTriggerContent() { const checkedInputs = this.inputTargets.filter(input => input.checked) - if (checkedInputs.length == 0) { + if (checkedInputs.length === 0) { this.triggerContentTarget.innerText = this.triggerTarget.dataset.placeholder - } else if (checkedInputs.length === 1) { - this.triggerContentTarget.innerText = this.inputContent(checkedInputs[0]) - } else { + } else if (this.termValue && checkedInputs.length > 1) { this.triggerContentTarget.innerText = `${checkedInputs.length} ${this.termValue}` + } else { + this.triggerContentTarget.innerText = checkedInputs.map((input) => this.inputContent(input)).join(", ") } }