Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 2b544fc

Browse files
committed
all tests passing for react v13-v15
1 parent bda67e8 commit 2b544fc

File tree

15 files changed

+176
-19873
lines changed

15 files changed

+176
-19873
lines changed

config.ru

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ require 'bundler'
22
Bundler.require
33

44
require "opal-rspec"
5-
require "react/source"
5+
require "opal-jquery"
6+
#require "react/source"
67

78
Opal.append_path File.expand_path('../spec', __FILE__)
89

910
run Opal::Server.new { |s|
1011
s.main = 'opal/rspec/sprockets_runner'
1112
s.append_path 'spec'
12-
s.append_path File.dirname(::React::Source.bundled_path_for("react-with-addons.js"))
13+
#s.append_path File.dirname(::React::Source.bundled_path_for("react-with-addons.js"))
1314
s.debug = true
14-
s.index_path = 'spec/reactjs/index.html.erb'
15+
s.index_path = 'spec/index.html.erb'
1516
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'opal'
2+
require 'react'
23
require 'reactive-ruby'
34
require_tree './components'

lib/react/top_level.rb

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ def self.create_element(type, properties = {}, &block)
2929

3030
def self.render(element, container)
3131
container = `container.$$class ? container[0] : container`
32-
component = Native(`React.render(#{element.to_n}, container, function(){#{yield if block_given?}})`)
32+
if !(`typeof ReactDOM === 'undefined'`)
33+
component = Native(`ReactDOM.render(#{element.to_n}, container, function(){#{yield if block_given?}})`) # v0.15+
34+
elsif !(`typeof React.renderToString === 'undefined'`)
35+
component = Native(`React.render(#{element.to_n}, container, function(){#{yield if block_given?}})`)
36+
else
37+
raise "render is not defined. In React >= v15 you must import it with ReactDOM"
38+
end
39+
3340
component.class.include(React::Component::API)
3441
component
3542
end
@@ -39,22 +46,51 @@ def self.is_valid_element(element)
3946
end
4047

4148
def self.render_to_string(element)
42-
React::RenderingContext.build { `React.renderToString(#{element.to_n})` }
49+
if !(`typeof ReactDOMServer === 'undefined'`)
50+
React::RenderingContext.build { `ReactDOMServer.renderToString(#{element.to_n})` } # v0.15+
51+
elsif !(`typeof React.renderToString === 'undefined'`)
52+
React::RenderingContext.build { `React.renderToString(#{element.to_n})` }
53+
else
54+
raise "renderToString is not defined. In React >= v15 you must import it with ReactDOMServer"
55+
end
4356
end
4457

4558
def self.render_to_static_markup(element)
46-
React::RenderingContext.build { `React.renderToStaticMarkup(#{element.to_n})` }
59+
if !(`typeof ReactDOMServer === 'undefined'`)
60+
React::RenderingContext.build { `ReactDOMServer.renderToStaticMarkup(#{element.to_n})` } # v0.15+
61+
elsif !(`typeof React.renderToString === 'undefined'`)
62+
React::RenderingContext.build { `React.renderToStaticMarkup(#{element.to_n})` }
63+
else
64+
raise "renderToStaticMarkup is not defined. In React >= v15 you must import it with ReactDOMServer"
65+
end
4766
end
4867

4968
def self.unmount_component_at_node(node)
50-
`React.unmountComponentAtNode(node.$$class ? node[0] : node)`
69+
if !(`typeof ReactDOM === 'undefined'`)
70+
`ReactDOM.unmountComponentAtNode(node.$$class ? node[0] : node)` # v0.15+
71+
elsif !(`typeof React.renderToString === 'undefined'`)
72+
`React.unmountComponentAtNode(node.$$class ? node[0] : node)`
73+
else
74+
raise "unmountComponentAtNode is not defined. In React >= v15 you must import it with ReactDOM"
75+
end
5176
end
5277

5378
end
5479

5580
Element.instance_eval do
81+
5682
class ::Element::DummyContext < React::Component::Base
5783
end
84+
85+
def self.find(selector)
86+
selector = selector.dom_node if selector.respond_to? :dom_node rescue selector
87+
`$(#{selector})`
88+
end
89+
90+
def self.[](selector)
91+
find(selector)
92+
end
93+
5894
def render(&block)
5995
React.render(React::RenderingContext.render(nil) {::Element::DummyContext.new.instance_eval &block}, self)
6096
end

lib/reactive-ruby.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
2+
13
if RUBY_ENGINE == 'opal'
2-
#require 'sources/react_v15.js' comment this out for release, leave in for testing with which ever version you like
4+
#require 'sources/react_v13.js' #comment this out for release, leave in for testing with which ever version you like
5+
#
36
require 'react/top_level'
47
require 'react/observable'
58
require 'react/component'
@@ -16,6 +19,10 @@
1619
else
1720
require 'opal'
1821
require 'opal-browser'
22+
begin
23+
require 'opal-jquery'
24+
rescue LoadError
25+
end
1926
require 'opal-activesupport'
2027
require 'reactive-ruby/version'
2128
require 'reactive-ruby/rails' if defined?(Rails)

0 commit comments

Comments
 (0)