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

Commit 6cdee7a

Browse files
catmandozetachang
authored andcommitted
added _as_node support to NativeLibrary
# Conflicts: # lib/react/component/class_methods.rb
1 parent e3df4bc commit 6cdee7a

File tree

7 files changed

+73
-20
lines changed

7 files changed

+73
-20
lines changed

lib/react/component.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ def self.included(base)
2929
base.extend(ClassMethods)
3030
end
3131

32+
def self.deprecation_warning(message)
33+
@deprecation_messages ||= []
34+
message = "Warning: Deprecated feature used in #{name}. #{message}"
35+
unless @deprecation_messages.include? message
36+
@deprecation_messages << message
37+
IsomorphicHelpers.log message, :warning
38+
end
39+
end
40+
3241
def initialize(native_element)
3342
@native = native_element
3443
end

lib/react/component/class_methods.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ def append_backtrace(message_array, backtrace)
2323
backtrace[1..-1].each { |line| message_array << line }
2424
end
2525

26-
def deprecation_warning(message)
27-
@deprecation_messages ||= []
28-
message = "Warning: Deprecated feature used in #{name}. #{message}"
29-
unless @deprecation_messages.include? message
30-
@deprecation_messages << message
31-
IsomorphicHelpers.log message, :warning
32-
end
33-
end
34-
3526
def render(container = nil, params = {}, &block)
3627
define_method :render do
3728
if container
@@ -128,16 +119,16 @@ def export_state(*states, &block)
128119

129120
def define_state_methods(this, name, from = nil, &block)
130121
this.define_method("#{name}") do
131-
self.class.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? || from == this
122+
React::Component.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? || from == this
132123
State.get_state(from || self, name)
133124
end
134125
this.define_method("#{name}=") do |new_state|
135-
self.class.deprecation_warning "Direct assignment to state `#{name}`. Use `#{(from && from != this) ? from : 'state'}.#{name}!` instead."
126+
React::Component.deprecation_warning "Direct assignment to state `#{name}`. Use `#{(from && from != this) ? from : 'state'}.#{name}!` instead."
136127
yield name, State.get_state(from || self, name), new_state if block && block.arity > 0
137128
State.set_state(from || self, name, new_state)
138129
end
139130
this.define_method("#{name}!") do |*args|
140-
self.class.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? or from == this
131+
React::Component.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? or from == this
141132
if args.count > 0
142133
yield name, State.get_state(from || self, name), args[0] if block && block.arity > 0
143134
current_value = State.get_state(from || self, name)

lib/react/component/props_wrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module React
22
module Component
33

44
def deprecated_params_method(name, *args, &block)
5-
self.class.deprecation_warning "Direct access to param `#{name}`. Use `params.#{name}` instead."
5+
React::Component.deprecation_warning"Direct access to param `#{name}`. Use `params.#{name}` instead."
66
params.send(name, *args, &block)
77
end
88

lib/react/native_library.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ def const_missing(const_name)
4545
import_const_from_native(self, const_name, true) || super
4646
end
4747

48-
def method_missing(method, *args, &block)
48+
def method_missing(method_name, *args, &block)
49+
method = method_name.gsub(/_as_node$/, '') # remove once _as_node is deprecated.
4950
component_class = get_const(method) if const_defined?(method)
5051
component_class ||= import_const_from_native(self, method, false)
51-
return React::RenderingContext.render(component_class, *args, &block) if component_class
52-
raise "could not import a react component named: #{scope_native_name method}"
52+
raise 'could not import a react component named: '\
53+
"#{scope_native_name method}" unless component_class
54+
if method == method_name
55+
React::RenderingContext.render(component_class, *args, &block)
56+
else # remove once _as_node is deprecated.
57+
React::RenderingContext.build_only(component_class, *args, &block)
58+
end
5359
end
5460

5561
private

lib/react/rendering_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class << self
44
attr_accessor :waiting_on_resources
55
end
66

7-
def build_only(name, *args, &block)
7+
def self.build_only(name, *args, &block)
88
React::Component.deprecation_warning(
99
'..._as_node is deprecated. Render component and then use the .node method instead'
1010
)

lib/reactrb/auto-import.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ def const_missing(const_name)
1818
end
1919

2020
def method_missing(method_name, *args, &block)
21-
component_class = React::NativeLibrary.import_const_from_native(self, method_name, false)
22-
return React::RenderingContext.render(component_class, *args, &block) if component_class
23-
_reactrb_original_method_missing(method_name, *args, &block)
21+
method = method_name.gsub(/_as_node/, '') # remove once _as_node is deprecated.
22+
component_class = React::NativeLibrary.import_const_from_native(self, method, false)
23+
_reactrb_original_method_missing(method, *args, &block) unless component_class
24+
if method == method_name
25+
React::RenderingContext.render(component_class, *args, &block)
26+
else # remove once _as_node is deprecated.
27+
React::RenderingContext.build_only(component_class, *args, &block)
28+
end
2429
end
2530
end
2631
end

spec/react/native_library_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,48 @@ class NestedComponent < React::Component::Base
192192
React.create_element(Foo))).to eq('<div>Hello There</div>')
193193
end
194194

195+
it 'will automatically import a React.js component when referenced in another component with the _as_node suffix' do
196+
stub_const 'Foo', Class.new(React::Component::Base)
197+
Foo.class_eval do
198+
render(:div) do
199+
e = React::Element.new(NativeComponent_as_node(name: 'There'))
200+
2.times { e.render }
201+
end
202+
end
203+
%x{
204+
window.NativeComponent = React.createClass({
205+
displayName: "HelloMessage",
206+
render: function render() {
207+
return React.createElement("div", null, "Hello ", this.props.name);
208+
}
209+
})
210+
}
211+
expect(React.render_to_static_markup(
212+
React.create_element(Foo))).to eq('<div><div>Hello There</div><div>Hello There</div></div>')
213+
end
214+
215+
it "will automatically import a React.js component in a library when referenced in another component with the _as_node suffix" do
216+
stub_const 'Foo', Class.new(React::Component::Base)
217+
Foo.class_eval do
218+
render(:div) do
219+
e = React::Element.new(NativeLibrary::NativeComponent_as_node(name: 'There'))
220+
2.times { e.render }
221+
end
222+
end
223+
%x{
224+
window.NativeLibrary = {
225+
NativeComponent: React.createClass({
226+
displayName: "HelloMessage",
227+
render: function render() {
228+
return React.createElement("div", null, "Hello ", this.props.name);
229+
}
230+
})
231+
}
232+
}
233+
expect(React.render_to_static_markup(
234+
React.create_element(Foo))).to eq('<div><div>Hello There</div><div>Hello There</div></div>')
235+
end
236+
195237
it "will automatically import a React.js component when referenced as a constant" do
196238
%x{
197239
window.NativeComponent = React.createClass({

0 commit comments

Comments
 (0)