diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index ffe3355..d5e454e 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -171,7 +171,7 @@ def prefilled_display if async_src && associated_object associated_object.to_combobox_display elsif hidden_field_value - options.find { |option| option.value == hidden_field_value }&.content + options.find { |option| option.value == hidden_field_value }&.autocompletable_as end end diff --git a/app/presenters/hotwire_combobox/listbox/option.rb b/app/presenters/hotwire_combobox/listbox/option.rb index d677af7..ce69797 100644 --- a/app/presenters/hotwire_combobox/listbox/option.rb +++ b/app/presenters/hotwire_combobox/listbox/option.rb @@ -11,8 +11,8 @@ def value option.try(:value) || option.id end - def content - option.try(:content) || option.try(:display) + def autocompletable_as + option.try(:autocompletable_as) || option.try(:display) end private @@ -42,11 +42,11 @@ def data } end - def filterable_as - option.try(:filterable_as) || option.try(:display) + def content + option.try(:content) || option.try(:display) end - def autocompletable_as - option.try(:autocompletable_as) || option.try(:display) + def filterable_as + option.try(:filterable_as) || option.try(:display) end end diff --git a/test/dummy/app/controllers/comboboxes_controller.rb b/test/dummy/app/controllers/comboboxes_controller.rb index d3339a8..db6c88e 100644 --- a/test/dummy/app/controllers/comboboxes_controller.rb +++ b/test/dummy/app/controllers/comboboxes_controller.rb @@ -39,6 +39,9 @@ def prefilled_form @user = User.first || raise("No user found, load fixtures first.") end + def prefilled_html + end + def async_html end diff --git a/test/dummy/app/views/comboboxes/prefilled_html.html.erb b/test/dummy/app/views/comboboxes/prefilled_html.html.erb new file mode 100644 index 0000000..9467b60 --- /dev/null +++ b/test/dummy/app/views/comboboxes/prefilled_html.html.erb @@ -0,0 +1,2 @@ +<%= combobox_tag "movie", Movie.limit(99), id: "movie-field", value: Movie.second.id, + render_in: { partial: "movies/movie", formats: [:turbo_stream] } %> diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index be93b25..041e61d 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -3,6 +3,7 @@ get "open", to: "comboboxes#open" get "html_options", to: "comboboxes#html_options" get "prefilled", to: "comboboxes#prefilled" + get "prefilled_html", to: "comboboxes#prefilled_html" get "required", to: "comboboxes#required" get "formbuilder", to: "comboboxes#formbuilder" get "new_options", to: "comboboxes#new_options" diff --git a/test/system/hotwire_combobox_test.rb b/test/system/hotwire_combobox_test.rb index 700180b..bf8d4cc 100644 --- a/test/system/hotwire_combobox_test.rb +++ b/test/system/hotwire_combobox_test.rb @@ -209,6 +209,16 @@ class HotwireComboboxTest < ApplicationSystemTestCase assert_selected_option_with text: "Michigan" end + test "html combobox with prefilled value" do + visit prefilled_html_path + + assert_closed_combobox + assert_combobox_display_and_value "#movie-field", Movie.second.title, Movie.second.id + + open_combobox "#movie-field" + assert_selected_option_with text: Movie.second.title + end + test "async combobox with prefilled value" do visit prefilled_async_path