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

Commit fc37df3

Browse files
committed
Squashed commit of the following:
commit 25c5f1e Author: Mitch VanDuyn <mitch@catprint.com> Date: Sat May 28 02:06:34 2016 -0400 tests passing using rake, react.js sources available, nice error displayed if react.js not available commit 2b544fc Author: Mitch VanDuyn <mitch@catprint.com> Date: Sat May 28 00:10:56 2016 -0400 all tests passing for react v13-v15 commit bda67e8 Author: Mitch VanDuyn <mitch@catprint.com> Date: Thu May 26 07:44:17 2016 -0400 wip commit 30e14c7 Author: Mitch VanDuyn <mitch@catprint.com> Date: Wed May 25 21:04:20 2016 -0400 removed require js commit 52e52b5 Author: Mitch VanDuyn <mitch@catprint.com> Date: Thu May 19 12:49:04 2016 -0400 bump to next version commit de5d685 Author: Mitch VanDuyn <mitch@catprint.com> Date: Thu May 19 12:47:11 2016 -0400 closes #142 closes #143
1 parent 1be83c7 commit fc37df3

File tree

18 files changed

+63318
-19902
lines changed

18 files changed

+63318
-19902
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/component/api.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module React
22
module Component
33
module API
44
def dom_node
5-
if `typeof React.findDOMNode === 'undefined'`
6-
`#{self}.native.getDOMNode` # v0.12.0
7-
else
5+
if !(`typeof ReactDOM === 'undefined' || typeof ReactDOM.findDOMNode === 'undefined'`)
6+
`ReactDOM.findDOMNode(#{self}.native)` # v0.14.0
7+
elsif !(`typeof React.findDOMNode === 'undefined'`)
88
`React.findDOMNode(#{self}.native)` # v0.13.0
9+
else
10+
`#{self}.native.getDOMNode` # v0.12.0
911
end
1012
end
1113

lib/react/top_level.rb

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

6464
def self.render(element, container)
6565
container = `container.$$class ? container[0] : container`
66-
component = Native(`React.render(#{element.to_n}, container, function(){#{yield if block_given?}})`)
66+
if !(`typeof ReactDOM === 'undefined'`)
67+
component = Native(`ReactDOM.render(#{element.to_n}, container, function(){#{yield if block_given?}})`) # v0.15+
68+
elsif !(`typeof React.renderToString === 'undefined'`)
69+
component = Native(`React.render(#{element.to_n}, container, function(){#{yield if block_given?}})`)
70+
else
71+
raise "render is not defined. In React >= v15 you must import it with ReactDOM"
72+
end
73+
6774
component.class.include(React::Component::API)
6875
component
6976
end
@@ -73,22 +80,51 @@ def self.is_valid_element(element)
7380
end
7481

7582
def self.render_to_string(element)
76-
React::RenderingContext.build { `React.renderToString(#{element.to_n})` }
83+
if !(`typeof ReactDOMServer === 'undefined'`)
84+
React::RenderingContext.build { `ReactDOMServer.renderToString(#{element.to_n})` } # v0.15+
85+
elsif !(`typeof React.renderToString === 'undefined'`)
86+
React::RenderingContext.build { `React.renderToString(#{element.to_n})` }
87+
else
88+
raise "renderToString is not defined. In React >= v15 you must import it with ReactDOMServer"
89+
end
7790
end
7891

7992
def self.render_to_static_markup(element)
80-
React::RenderingContext.build { `React.renderToStaticMarkup(#{element.to_n})` }
93+
if !(`typeof ReactDOMServer === 'undefined'`)
94+
React::RenderingContext.build { `ReactDOMServer.renderToStaticMarkup(#{element.to_n})` } # v0.15+
95+
elsif !(`typeof React.renderToString === 'undefined'`)
96+
React::RenderingContext.build { `React.renderToStaticMarkup(#{element.to_n})` }
97+
else
98+
raise "renderToStaticMarkup is not defined. In React >= v15 you must import it with ReactDOMServer"
99+
end
81100
end
82101

83102
def self.unmount_component_at_node(node)
84-
`React.unmountComponentAtNode(node.$$class ? node[0] : node)`
103+
if !(`typeof ReactDOM === 'undefined'`)
104+
`ReactDOM.unmountComponentAtNode(node.$$class ? node[0] : node)` # v0.15+
105+
elsif !(`typeof React.renderToString === 'undefined'`)
106+
`React.unmountComponentAtNode(node.$$class ? node[0] : node)`
107+
else
108+
raise "unmountComponentAtNode is not defined. In React >= v15 you must import it with ReactDOM"
109+
end
85110
end
86111

87112
end
88113

89114
Element.instance_eval do
115+
90116
class ::Element::DummyContext < React::Component::Base
91117
end
118+
119+
def self.find(selector)
120+
selector = selector.dom_node if selector.respond_to? :dom_node rescue selector
121+
`$(#{selector})`
122+
end
123+
124+
def self.[](selector)
125+
find(selector)
126+
end
127+
92128
def render(&block)
93129
React.render(React::RenderingContext.render(nil) {::Element::DummyContext.new.instance_eval &block}, self)
94130
end

lib/reactive-ruby.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
2+
13
if RUBY_ENGINE == 'opal'
2-
require 'sources/react.js'
4+
if `window.React === undefined || window.React.version === undefined`
5+
raise [
6+
"No React.js Available",
7+
"",
8+
"React.js must be defined before requiring 'reactive-ruby'",
9+
"'reactive-ruby' has been tested with react v13, v14, and v15.",
10+
"",
11+
"IF USING 'react-rails':",
12+
" add 'require \"react\"' immediately before the 'require \"reactive-ruby\" directive in 'views/components.rb'.",
13+
"IF USING WEBPACK:",
14+
" add 'react' to your webpack manifest.",
15+
"OTHERWISE TO GET THE LATEST TESTED VERSION",
16+
" add 'require \"react-latest\"' immediately before the require of 'reactive-ruby',",
17+
"OR TO USE A SPECIFIC VERSION",
18+
" add 'require \"react-v1x\"' immediately before the require of 'reactive-ruby'."
19+
].join("\n")
20+
end
321
require 'react/top_level'
422
require 'react/observable'
523
require 'react/component'
@@ -13,14 +31,20 @@
1331
require 'reactive-ruby/isomorphic_helpers'
1432
require 'rails-helpers/top_level_rails_component'
1533
require 'reactive-ruby/version'
34+
1635
else
1736
require 'opal'
1837
require 'opal-browser'
38+
begin
39+
require 'opal-jquery'
40+
rescue LoadError
41+
end
1942
require 'opal-activesupport'
2043
require 'reactive-ruby/version'
2144
require 'reactive-ruby/rails' if defined?(Rails)
2245
require 'reactive-ruby/isomorphic_helpers'
2346
require 'reactive-ruby/serializers'
2447

2548
Opal.append_path File.expand_path('../', __FILE__).untaint
49+
Opal.append_path File.expand_path('../sources/', __FILE__).untaint
2650
end

0 commit comments

Comments
 (0)