Skip to content

Commit a23ab90

Browse files
authored
Merge pull request #644 from prikha/view-helper-signature-fix
Fix react_component helper usage with a block
2 parents bc2f80a + 21d657b commit a23ab90

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/react/rails/view_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module ViewHelper
1818
# Otherwise, make a new instance.
1919
def react_component(*args, &block)
2020
helper_obj = @__react_component_helper ||= helper_implementation_class.new
21-
helper_obj.react_component(*args, &block)
21+
helper_obj.react_component(*args) { capture &block if block_given? }
2222
end
2323
end
2424
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%= react_component 'GreetingMessage', { :name => 'Name' }, { :id => 'component' } do %>
2+
<div id="unique-nested-id">NestedContent</div>
3+
<% end %>

test/react/rails/view_helper_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
# Provide direct access to the view helper methods
44
class ViewHelperHelper
5+
extend ActionView::Context
6+
extend ActionView::Helpers::CaptureHelper
57
extend React::Rails::ViewHelper
8+
69
end
710

811
class ViewHelperTest < ActionView::TestCase
@@ -12,12 +15,31 @@ class ViewHelperTest < ActionView::TestCase
1215
assert_equal(expected_html, rendered_html)
1316
end
1417

18+
test 'view helper accepts block usage' do
19+
expected_html = %{<div data-react-class="Component" data-react-props="{&quot;a&quot;:&quot;b&quot;}">content</div>}
20+
rendered_html = ViewHelperHelper.react_component("Component", {a: "b"}) do
21+
"content"
22+
end
23+
assert_equal(expected_html, rendered_html)
24+
end
25+
1526
test "view helper can be used in stand-alone views" do
1627
@name = "React-Rails"
1728
render template: "pages/show"
1829
assert_includes(rendered, "React-Rails")
1930
end
2031

32+
test "view helper can accept block and render inner content only once" do
33+
rendered_html = render partial: "pages/component_with_inner_html"
34+
expected_html = <<HTML
35+
<div data-react-class=\"GreetingMessage\" data-react-props=\"{&quot;name&quot;:&quot;Name&quot;}\" id=\"component\">
36+
<div id=\"unique-nested-id\">NestedContent</div>
37+
</div>
38+
HTML
39+
assert_equal expected_html.strip, rendered_html
40+
end
41+
42+
2143
test "view helper uses the implementation class set in the initializer" do
2244
assert_equal(
2345
React::Rails::ViewHelper.helper_implementation_class.to_s,

0 commit comments

Comments
 (0)