Skip to content

Commit e8420c5

Browse files
author
Kai Wood
committed
Add ability to render custom attributes from the controller
Currently, all custom attributes are sliced from options when using the controller renderer, instead of forwarding it to the view helper, making it impossible to set a custom class name or id. This patch changes the behaviour to allow class, id, custom data-* and aria-* attributes.
1 parent 0573ae2 commit e8420c5

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

lib/react/rails/controller_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(options={})
1212

1313
def call(name, options, &block)
1414
props = options.fetch(:props, {})
15-
options = options.slice(:data, :tag).merge(prerender: true)
15+
options = options.slice(:data, :aria, :tag, :class, :id).merge(prerender: true)
1616
react_component(name, props, options, &block)
1717
end
1818
end

test/dummy/app/controllers/server_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def console_example_suppressed
1616
end
1717

1818
def inline_component
19-
render component: 'TodoList', props: { todos: ['Render this inline'] }, tag: 'span'
19+
render component: 'TodoList',
20+
props: { todos: ['Render this inline'] },
21+
tag: 'span',
22+
class: 'custom-class',
23+
id: 'custom-id',
24+
data: { remote: true }
2025
end
2126
end

test/server_rendered_html_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ def wait_to_ensure_asset_pipeline_detects_changes
5555
test 'react inline component rendering' do
5656
get '/server/inline_component'
5757
rendered_html = response.body
58-
assert_match(/<span data-react-class=\"TodoList\"/, rendered_html)
58+
assert_match(/<span.*data-react-class=\"TodoList\"/, rendered_html)
5959
# make sure that the items are prerendered
6060
assert_match(/Render this inline/, rendered_html)
6161
assert_match(/<\/ul><\/span>/, rendered_html, "it accepts a tag override")
6262
# make sure that the layout is rendered with the component
6363
assert_match(/<title>Dummy<\/title>/, rendered_html)
64+
# make sure that custom html attributes are rendered
65+
assert_match(/class=\"custom-class\"/, rendered_html)
66+
assert_match(/id=\"custom-id\"/, rendered_html)
67+
assert_match(/data-remote=\"true\"/, rendered_html)
6468
end
6569
end

0 commit comments

Comments
 (0)