Skip to content

Commit

Permalink
Fix prefilled html (#43)
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
josefarias authored Feb 29, 2024
1 parent 8223999 commit 5fae4b0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions app/presenters/hotwire_combobox/listbox/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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 @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/views/comboboxes/prefilled_html.html.erb
Original file line number Diff line number Diff line change
@@ -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] } %>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5fae4b0

Please sign in to comment.