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

Commit e9e520e

Browse files
committed
Inject React::Component::API
1 parent 86f6ef6 commit e9e520e

File tree

4 files changed

+46
-40
lines changed

4 files changed

+46
-40
lines changed

lib/react/component.rb

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module React
66
module Component
77
def self.included(base)
8+
base.include(API)
89
base.include(React::Callbacks)
910
base.class_eval do
1011
class_attribute :init_state, :validator
@@ -30,35 +31,13 @@ def refs
3031
Native(`#{@native}.refs`)
3132
end
3233

33-
def emit(event_name, *args)
34-
self.params["_on#{event_name.to_s.event_camelize}"].call(*args)
35-
end
36-
37-
def mounted?
38-
`#{@native}.isMounted()`
39-
end
40-
4134
def state
4235
raise "No native ReactComponent associated" unless @native
4336
Native(`#{@native}.state`)
4437
end
4538

46-
def set_state(state, &block)
47-
raise "No native ReactComponent associated" unless @native
48-
%x{
49-
#{@native}.setState(#{state.to_n}, function(){
50-
#{block.call if block}
51-
});
52-
}
53-
end
54-
55-
def set_state!(state, &block)
56-
raise "No native ReactComponent associated" unless @native
57-
%x{
58-
#{@native}.replaceState(#{state.to_n}, function(){
59-
#{block.call if block}
60-
});
61-
}
39+
def emit(event_name, *args)
40+
self.params["_on#{event_name.to_s.event_camelize}"].call(*args)
6241
end
6342

6443
def component_will_mount
@@ -169,5 +148,29 @@ def define_state(*states)
169148
end
170149
end
171150
end
151+
152+
module API
153+
def mounted?
154+
`#{@native}.isMounted()`
155+
end
156+
157+
def set_state(state, &block)
158+
raise "No native ReactComponent associated" unless @native
159+
%x{
160+
#{@native}.setState(#{state.to_n}, function(){
161+
#{block.call if block}
162+
});
163+
}
164+
end
165+
166+
def set_state!(state, &block)
167+
raise "No native ReactComponent associated" unless @native
168+
%x{
169+
#{@native}.replaceState(#{state.to_n}, function(){
170+
#{block.call if block}
171+
});
172+
}
173+
end
174+
end
172175
end
173176
end

lib/react/top_level.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require "native"
22
require 'active_support'
3-
require "promise"
43

54
module React
65
HTML_TAGS = %w(a abbr address area article aside audio b base bdi bdo big blockquote body br
@@ -28,14 +27,9 @@ def self.create_element(type, properties = {}, &block)
2827
end
2928

3029
def self.render(element, container)
31-
if block_given?
32-
%x{
33-
React.render(#{element.to_n}, container, function(){#{ yield }})
34-
}
35-
else
36-
`React.render(#{element.to_n}, container, function(){})`
37-
end
38-
return nil
30+
component = Native(`React.render(#{element.to_n}, container, function(){#{yield if block_given?}})`)
31+
component.class.include(React::Component::API)
32+
component
3933
end
4034

4135
def self.is_valid_element(element)

spec/react_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ def render
131131
React.render(React.create_element('span') { "lorem" }, div)
132132
end
133133

134+
it "should return a React::Component::API compatible object" do
135+
div = `document.createElement("div")`
136+
component = React.render(React.create_element('span') { "lorem" }, div)
137+
React::Component::API.public_instance_methods(true).each do |method_name|
138+
expect(component).to respond_to(method_name)
139+
end
140+
end
141+
134142
pending "should return nil to prevent abstraction leakage" do
135143
div = `document.createElement("div")`
136144
expect {

spec/spec_helper.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
module ReactTestHelpers
44
`var ReactTestUtils = React.addons.TestUtils`
5-
5+
66
def renderToDocument(type, options = {})
77
element = React.create_element(type, options)
88
return renderElementToDocument(element)
99
end
10-
10+
1111
def renderElementToDocument(element)
12-
instance = `ReactTestUtils.renderIntoDocument(#{element.to_n})`
13-
return Native(instance)
12+
instance = Native(`ReactTestUtils.renderIntoDocument(#{element.to_n})`)
13+
instance.class.include(React::Component::API)
14+
return instance
1415
end
15-
16+
1617
def simulateEvent(event, element, params = {})
1718
simulator = Native(`ReactTestUtils.Simulate`)
1819
simulator[event.to_s].call(`#{element.to_n}.getDOMNode()`, params)
1920
end
20-
21+
2122
def isElementOfType(element, type)
2223
`React.addons.TestUtils.isElementOfType(#{element.to_n}, #{type.cached_component_class})`
2324
end
2425
end
2526

2627
RSpec.configure do |config|
2728
config.include ReactTestHelpers
28-
end
29+
end

0 commit comments

Comments
 (0)