Skip to content

Commit 88c6b64

Browse files
author
Robert Mosolgo
committed
Merge pull request #384 from kaiwood/custom_attributes_in_component_renderer
Add ability to render custom attributes from the controller
2 parents 0573ae2 + a793672 commit 88c6b64

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ Components can also be prerendered directly from a controller action with the cu
212212
class TodoController < ApplicationController
213213
def index
214214
@todos = Todo.all
215-
render component: 'TodoList', props: { todos: @todos }, tag: 'span'
215+
render component: 'TodoList', props: { todos: @todos }, tag: 'span', class: 'todo'
216216
end
217217
end
218218
```
219219

220220
This custom renderer behaves the same as a normal view renderer and accepts the usual arguments - `content_type`, `layout`, `location` and `status`.
221-
By default, your current layout will be used and the component, rather than a view, will be rendered in place of `yield`.
221+
By default, your current layout will be used and the component, rather than a view, will be rendered in place of `yield`. Custom data-* attributes
222+
can be passed like `data: {remote: true}`.
222223

223224
### Component generator
224225

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)