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

Commit ef892d2

Browse files
committed
Address #187
1 parent c66a7ee commit ef892d2

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

lib/react/top_level.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def self.create_element(type, properties = {}, &block)
5555
end
5656

5757
def self.render(element, container)
58+
%x{
59+
console.error(
60+
"Warning: Using deprecated behavior of `React.render`,",
61+
"require \"react/top_level_render\" to get the correct behavior."
62+
);
63+
}
5864
container = `container.$$class ? container[0] : container`
5965
if !(`typeof ReactDOM === 'undefined'`)
6066
component = Native(`ReactDOM.render(#{element.to_n}, container, function(){#{yield if block_given?}})`) # v0.15+

lib/react/top_level_render.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module React
2+
def self.render(element, container)
3+
container = `container.$$class ? container[0] : container`
4+
if !(`typeof ReactDOM === 'undefined'`)
5+
native = `ReactDOM.render(#{element.to_n}, container, function(){#{yield if block_given?}})` # v0.15+
6+
elsif !(`typeof React.renderToString === 'undefined'`)
7+
native = `React.render(#{element.to_n}, container, function(){#{yield if block_given?}})`
8+
else
9+
raise "render is not defined. In React >= v15 you must import it with ReactDOM"
10+
end
11+
12+
if `#{native}._getOpalInstance !== undefined`
13+
`#{native}._getOpalInstance()`
14+
elsif `React.findDOMNode !== undefined && #{native}.nodeType == undefined`
15+
`React.findDOMNode(#{native})`
16+
else
17+
native
18+
end
19+
end
20+
end

spec/react/react_spec.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,23 @@ def render
181181
React.render(React.create_element('span') { "lorem" }, div)
182182
end
183183

184-
it "should return a React::Component::API compatible object" do
185-
div = `document.createElement("div")`
186-
component = React.render(React.create_element('span') { "lorem" }, div)
187-
React::Component::API.public_instance_methods(true).each do |method_name|
188-
expect(component).to respond_to(method_name)
184+
it "returns the actual ruby instance" do
185+
stub_const 'Foo', Class.new
186+
Foo.class_eval do
187+
def render
188+
React.create_element("div") { "lorem" }
189+
end
189190
end
191+
192+
div = `document.createElement("div")`
193+
instance = React.render(React.create_element(Foo), div)
194+
expect(instance).to be_a(Foo)
190195
end
191196

192-
pending "should return nil to prevent abstraction leakage" do
197+
it "returns the actual DOM node" do
193198
div = `document.createElement("div")`
194-
expect {
195-
React.render(React.create_element('span') { "lorem" }, div)
196-
}.to be_nil
199+
node = React.render(React.create_element('span') { "lorem" }, div)
200+
expect(`#{node}.nodeType`).to eq(1)
197201
end
198202
end
199203

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def ruby?
1919
require 'hyper-react'
2020
require 'react/test/rspec'
2121
require 'react/test/utils'
22+
require 'react/top_level_render'
2223

2324
require File.expand_path('../support/react/spec_helpers', __FILE__)
2425

0 commit comments

Comments
 (0)