diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index 2ecd1bf..85e8a9d 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -246,7 +246,7 @@ def prefilled_display elsif async_src && form_object&.respond_to?(name) form_object.public_send name else - options.find_by_value(hidden_field_value)&.autocompletable_as + options.find_by_value(hidden_field_value)&.autocompletable_as || hidden_field_value end end diff --git a/test/dummy/app/controllers/comboboxes_controller.rb b/test/dummy/app/controllers/comboboxes_controller.rb index 00b324d..7a75600 100644 --- a/test/dummy/app/controllers/comboboxes_controller.rb +++ b/test/dummy/app/controllers/comboboxes_controller.rb @@ -43,6 +43,9 @@ def prefilled_form @user = User.first || raise("No user found, load fixtures first.") end + def prefilled_free_text + end + def prefilled_html end diff --git a/test/dummy/app/views/comboboxes/prefilled_free_text.html.erb b/test/dummy/app/views/comboboxes/prefilled_free_text.html.erb new file mode 100644 index 0000000..74bc9c6 --- /dev/null +++ b/test/dummy/app/views/comboboxes/prefilled_free_text.html.erb @@ -0,0 +1 @@ +<%= combobox_tag "state", @state_options, value: "East Oregon", free_text: true, id: "state-field" %> diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index f87b1a0..dfec8e1 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -13,6 +13,7 @@ get "freetext_async", to: "comboboxes#freetext_async" get "prefilled_async", to: "comboboxes#prefilled_async" get "prefilled_form", to: "comboboxes#prefilled_form" + get "prefilled_free_text", to: "comboboxes#prefilled_free_text" get "async_html", to: "comboboxes#async_html" get "render_in", to: "comboboxes#render_in" get "enum", to: "comboboxes#enum" diff --git a/test/system/hotwire_combobox_test.rb b/test/system/hotwire_combobox_test.rb index 2a96f3f..64c9c0a 100644 --- a/test/system/hotwire_combobox_test.rb +++ b/test/system/hotwire_combobox_test.rb @@ -396,6 +396,17 @@ class HotwireComboboxTest < ApplicationSystemTestCase assert_selected_option_with text: "Michigan" end + test "combobox with prefilled free text value" do + visit prefilled_free_text_path + + assert_closed_combobox + assert_combobox_display_and_value "#state-field", "East Oregon", "East Oregon" + + open_combobox "#state-field" + assert_no_visible_selected_option + assert_option_with text: "Oregon" + end + test "html combobox with prefilled value" do visit prefilled_html_path