Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pre-filling of free text inputs #203

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/controllers/comboboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= combobox_tag "state", @state_options, value: "East Oregon", free_text: true, id: "state-field" %>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down