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

Commit 32f891e

Browse files
committed
Opal Array#to_n is deep transform
1 parent 101f6ef commit 32f891e

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

lib/react/api.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ def self.create_element(type, properties = {}, &block)
6767
props[React::ATTRIBUTES.include?(lower_camelize(key)) ? lower_camelize(key) : key] = value
6868
end
6969
end
70-
params << props
70+
params << props.shallow_to_n
7171

7272
# Children Nodes
7373
if block_given?
7474
children = [yield].flatten.each do |ele|
75-
params << ele
75+
params << ele.to_n
7676
end
7777
end
7878

79-
return React::Element.new(`React.createElement.apply(null, #{params.to_n})`)
79+
return React::Element.new(`React.createElement.apply(null, #{params})`)
8080
end
8181

8282
def self.clear_component_class_cache

lib/react/component.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "./ext/string"
22
require 'active_support/core_ext/class/attribute'
33
require 'react/callbacks'
4+
require "react/ext/hash"
45

56
module React
67
module Component
@@ -159,7 +160,7 @@ module API
159160
def set_props(prop, &block)
160161
raise "No native ReactComponent associated" unless @native
161162
%x{
162-
#{@native}.setProps(#{prop.to_n}, function(){
163+
#{@native}.setProps(#{prop.shallow_to_n}, function(){
163164
#{block.call if block}
164165
});
165166
}
@@ -168,7 +169,7 @@ def set_props(prop, &block)
168169
def set_props!(prop, &block)
169170
raise "No native ReactComponent associated" unless @native
170171
%x{
171-
#{@native}.replaceProps(#{prop.to_n}, function(){
172+
#{@native}.replaceProps(#{prop.shallow_to_n}, function(){
172173
#{block.call if block}
173174
});
174175
}
@@ -177,7 +178,7 @@ def set_props!(prop, &block)
177178
def set_state(state, &block)
178179
raise "No native ReactComponent associated" unless @native
179180
%x{
180-
#{@native}.setState(#{state.to_n}, function(){
181+
#{@native}.setState(#{state.shallow_to_n}, function(){
181182
#{block.call if block}
182183
});
183184
}
@@ -186,7 +187,7 @@ def set_state(state, &block)
186187
def set_state!(state, &block)
187188
raise "No native ReactComponent associated" unless @native
188189
%x{
189-
#{@native}.replaceState(#{state.to_n}, function(){
190+
#{@native}.replaceState(#{state.shallow_to_n}, function(){
190191
#{block.call if block}
191192
});
192193
}

lib/react/ext/hash.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Hash
2+
def shallow_to_n
3+
hash = `{}`
4+
self.map do |key, value|
5+
`hash[#{key}] = #{value}`
6+
end
7+
hash
8+
end
9+
end

spec/component_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ def render
236236
define_state :foo
237237

238238
before_mount do
239-
self.foo = {a: 10}
239+
self.foo = [{a: 10}]
240240
end
241241

242242
def render
243-
div { self.foo[:a] }
243+
div { self.foo[0][:a] }
244244
end
245245
end
246246

@@ -271,11 +271,11 @@ def render
271271
it "should access nested params as orignal Ruby object" do
272272
Foo.class_eval do
273273
def render
274-
React.create_element("div") { params[:prop][:foo] }
274+
React.create_element("div") { params[:prop][0][:foo] }
275275
end
276276
end
277277

278-
element = renderToDocument(Foo, prop: {foo: 10})
278+
element = renderToDocument(Foo, prop: [{foo: 10}])
279279
expect(element.getDOMNode.textContent).to eq("10")
280280
end
281281
end

0 commit comments

Comments
 (0)