From 158f180cd0574fe16f5c843bbef3ab21f0a3052e Mon Sep 17 00:00:00 2001 From: Aaron Contreras Date: Thu, 28 Sep 2023 17:40:42 -0500 Subject: [PATCH 1/2] Fix: Correct `:remove` action for turbo stream response Given I attempt to remove a component via a turbo stream, this resulted in a ``` "You need to wrap your component in a `component_wrapper` block in order to use the turbo-stream methods" ``` error. This is due to the component template not being rendered and the `@component_wrapped_used` instance variable not being set (because the `component_wrapper` invocation in the component template is never called. The proposed fix for this is to render JUST the `component_wrapper` (without its block content) in order to get the dom id that needs removal and turbo streams is able to do its magic. This is achieved by providing a `@wrapper_only` flag so when the `component_wrapper` method is called within the component's template, only the wrapper tag is rendered, making it enough to perform a successful turbo stream removal of the component by handling this case with the short circuit. We don't really care in this case about rendering the entire component given all we care about is that outer tag. --- app/components/concerns/op_turbo/streamable.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/components/concerns/op_turbo/streamable.rb b/app/components/concerns/op_turbo/streamable.rb index 4802b7e83e90..92216f134c75 100644 --- a/app/components/concerns/op_turbo/streamable.rb +++ b/app/components/concerns/op_turbo/streamable.rb @@ -45,6 +45,8 @@ def render_as_turbo_stream(view_context:, action: :update) when :replace template = render_in(view_context) when :remove + @wrapper_only = true + render_in(view_context) template = nil else raise "Unsupported action #{action}" @@ -79,6 +81,8 @@ def component_wrapper(tag: "div", class: nil, data: nil, style: nil, &block) @component_wrapper_used = true if inner_html_only? capture(&block) + elsif wrapper_only? + content_tag(tag, id: wrapper_key, class:, data:, style:) else content_tag(tag, id: wrapper_key, class:, data:, style:, &block) end @@ -88,6 +92,10 @@ def inner_html_only? @inner_html_only == true end + def wrapper_only? + !!@wrapper_only + end + def wrapper_key if wrapper_uniq_by.nil? self.class.wrapper_key From 48e7f277b858393aee418481d388470e7421439e Mon Sep 17 00:00:00 2001 From: Aaron Contreras Date: Thu, 28 Sep 2023 17:46:16 -0500 Subject: [PATCH 2/2] Lint: Use !! instead of comparing to `true` --- app/components/concerns/op_turbo/streamable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/concerns/op_turbo/streamable.rb b/app/components/concerns/op_turbo/streamable.rb index 92216f134c75..48b45ecf11aa 100644 --- a/app/components/concerns/op_turbo/streamable.rb +++ b/app/components/concerns/op_turbo/streamable.rb @@ -89,7 +89,7 @@ def component_wrapper(tag: "div", class: nil, data: nil, style: nil, &block) end def inner_html_only? - @inner_html_only == true + !!@inner_html_only end def wrapper_only?