Skip to content

Add ability to render custom attributes from the controller #384

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ Components can also be prerendered directly from a controller action with the cu
class TodoController < ApplicationController
def index
@todos = Todo.all
render component: 'TodoList', props: { todos: @todos }, tag: 'span'
render component: 'TodoList', props: { todos: @todos }, tag: 'span', class: 'todo'
end
end
```

This custom renderer behaves the same as a normal view renderer and accepts the usual arguments - `content_type`, `layout`, `location` and `status`.
By default, your current layout will be used and the component, rather than a view, will be rendered in place of `yield`.
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
can be passed like `data: {remote: true}`.

### Component generator

Expand Down
2 changes: 1 addition & 1 deletion lib/react/rails/controller_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(options={})

def call(name, options, &block)
props = options.fetch(:props, {})
options = options.slice(:data, :tag).merge(prerender: true)
options = options.slice(:data, :aria, :tag, :class, :id).merge(prerender: true)
react_component(name, props, options, &block)
end
end
7 changes: 6 additions & 1 deletion test/dummy/app/controllers/server_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def console_example_suppressed
end

def inline_component
render component: 'TodoList', props: { todos: ['Render this inline'] }, tag: 'span'
render component: 'TodoList',
props: { todos: ['Render this inline'] },
tag: 'span',
class: 'custom-class',
id: 'custom-id',
data: { remote: true }
end
end
6 changes: 5 additions & 1 deletion test/server_rendered_html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def wait_to_ensure_asset_pipeline_detects_changes
test 'react inline component rendering' do
get '/server/inline_component'
rendered_html = response.body
assert_match(/<span data-react-class=\"TodoList\"/, rendered_html)
assert_match(/<span.*data-react-class=\"TodoList\"/, rendered_html)
# make sure that the items are prerendered
assert_match(/Render this inline/, rendered_html)
assert_match(/<\/ul><\/span>/, rendered_html, "it accepts a tag override")
# make sure that the layout is rendered with the component
assert_match(/<title>Dummy<\/title>/, rendered_html)
# make sure that custom html attributes are rendered
assert_match(/class=\"custom-class\"/, rendered_html)
assert_match(/id=\"custom-id\"/, rendered_html)
assert_match(/data-remote=\"true\"/, rendered_html)
end
end