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

Ensure html format when rendering combobox partial #239

Merged
merged 3 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ PLATFORMS
arm64-darwin-22
arm64-darwin-23
x86_64-darwin-23
x86_64-darwin-24
x86_64-linux

DEPENDENCIES
Expand Down
12 changes: 11 additions & 1 deletion lib/hotwire_combobox/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def hw_combobox_style_tag(*args, **kwargs)
def hw_combobox_tag(name, options_or_src = [], render_in: {}, include_blank: nil, **kwargs, &block)
options, src = hw_extract_options_and_src options_or_src, render_in, include_blank
component = HotwireCombobox::Component.new self, name, options: options, async_src: src, request: request, **kwargs
render component, &block
# hw_with_html_format { render component, &block }
render component, &block
end

def hw_combobox_options(options, render_in: {}, include_blank: nil, display: :to_combobox_display, **custom_methods)
Expand Down Expand Up @@ -131,6 +132,15 @@ def hw_call_method_or_proc(object, method_or_proc)
end

private

def hw_with_html_format
original_formats = lookup_context.formats.dup
lookup_context.formats = [:html]
yield
ensure
lookup_context.formats = original_formats
end

def hw_fullpath_for_pagination
transient_params = %w[ input_type ]
hw_uri_with_params request.path, **request.query_parameters.except(*transient_params)
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/comboboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def external_clear
def dialog
end

def turbo_stream_rendering
@state = State.first || raise("No state found, load fixtures first.")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused, removed in main

end

private
delegate :combobox_options, :html_combobox_options, to: "ApplicationController.helpers", private: true

Expand Down
10 changes: 10 additions & 0 deletions test/dummy/app/controllers/turbo_stream_rendering_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class TurboStreamRenderingController < ApplicationController
helper TurboStreamHelper
def edit
@state = State.find(params[:id])
end

def edit2
@state = State.find(params[:id])
end
end
19 changes: 19 additions & 0 deletions test/dummy/app/helpers/turbo_stream_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module TurboStreamHelper
def turbo_stream_action_tag_with_block(
action,
target: nil,
targets: nil,
**options,
&block
)
template_content = block_given? ? capture(&block) : nil

turbo_stream_action_tag(
action,
target: target,
targets: targets,
template: template_content,
**options
)
end
end
14 changes: 14 additions & 0 deletions test/dummy/app/views/comboboxes/turbo_stream_rendering.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>My fav state is:</h1>
(Error replicated with turbo_stream_rendering#edit)
<div id=<%= dom_id(@state) %>>
<p><%= @state.name %></p>
<p><%= @state.abbreviation %></p>
<%= button_to "Edit", edit_turbo_stream_rendering_path(@state, format: :turbo_stream ), method: :get%>
</div>
<h1>My fav state is:</h1>
(turbo_stream_rendering#edit2)
<div id=<%= dom_id(@state, "2") %>>
<p><%= @state.name %></p>
<p><%= @state.abbreviation %></p>
<%= button_to "Edit", edit2_turbo_stream_rendering_path(@state, format: :turbo_stream ), method: :get%>
</div>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= turbo_stream_action_tag_with_block "replace",
target: dom_id(@state),
data: {
transition_enter: "",
transition_enter_start: "",
transition_enter_end: "",
} do %>
<%= combobox_tag "State", states_path, id: :fav_state %>
<% end %>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.replace dom_id(@state, "2") do %>
<%= combobox_tag "State", states_path, id: :fav_state %>
<% end %>
5 changes: 5 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
get "turbo_stream_rendering", to: "comboboxes#turbo_stream_rendering"
get "async", to: "comboboxes#async"
get "async_html", to: "comboboxes#async_html"
get "async_preload", to: "comboboxes#async_preload"
Expand Down Expand Up @@ -36,6 +37,10 @@
get "render_in_locals", to: "comboboxes#render_in_locals"
get "required", to: "comboboxes#required"

resources :turbo_stream_rendering, only: %i[edit] do
get :edit2, on: :member
end

resources :movies, only: %i[ index update ]
get "movies_html", to: "movies#index_html"
get "movies_with_blank", to: "movies#index_with_blank"
Expand Down