forked from reactjs/react-rails
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathview_helper.rb
29 lines (24 loc) · 871 Bytes
/
view_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module React
module Rails
module ViewHelper
# Render a UJS-type HTML tag annotated with data attributes, which
# are used by react_ujs to actually instantiate the React component
# on the client.
#
def react_component(name, args = {}, options = {}, &block)
options = {:tag => options} if options.is_a?(Symbol)
block = Proc.new{React::Renderer.render(name, args)} if options[:prerender] == true
html_options = options.reverse_merge(:data => {})
html_options[:data].tap do |data|
data[:react_class] = name
data[:react_props] = args.to_json unless args.empty?
end
html_tag = html_options.delete(:tag) || :div
content_tag(html_tag, '', html_options, &block)
end
end
end
end
ActionView::Base.class_eval do
include ::React::Rails::ViewHelper
end