Skip to content

Commit 7093adc

Browse files
committed
Port back other React changes in here, all specs pass, but temporarily removed context and props from state
1 parent d1a6bac commit 7093adc

19 files changed

+513
-717
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ gem 'opal-rspec', '>= 0.5.0.beta3'
99

1010
source 'https://rails-assets.org' do
1111
gem 'rails-assets-es5-shim'
12+
gem 'rails-assets-jquery'
1213
end

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require 'react/source'
77
Opal::RSpec::RakeTask.new(:default) do |server, task|
88
# Need a Phantom polyfill, there is a docs and js path, order is not guaranteed, so just append both
99
RailsAssetsEs5Shim.load_paths.each {|p| server.append_path p}
10+
RailsAssetsJquery.load_paths.each { |p| server.append_path p }
1011
server.append_path File.dirname(React::Source.bundled_path_for('react-with-addons.js'))
1112
task.pattern = 'spec/opal/**/*_spec.rb'
1213
end

config.ru

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
Bundler.require
2-
32
require 'opal-rspec'
43
require 'react/source'
54

6-
files = [React::Source.bundled_path_for('react-with-addons.js')] + FileList['spec/opal/**/*_spec.rb']
7-
sprockets_env = Opal::RSpec::SprocketsEnvironment.new(spec_pattern=nil, spec_exclude_pattern=nil, spec_files=files)
5+
sprockets_env = Opal::RSpec::SprocketsEnvironment.new(spec_pattern='spec/opal/**/*_spec.rb')
86
run Opal::Server.new(sprockets: sprockets_env) { |s|
97
s.main = 'opal/rspec/sprockets_runner'
108
sprockets_env.add_spec_paths_to_sprockets
119
s.debug = false
10+
# Need a Phantom polyfill, there is a docs and js path, order is not guaranteed, so just append both
11+
RailsAssetsEs5Shim.load_paths.each { |p| s.append_path p }
12+
RailsAssetsJquery.load_paths.each { |p| s.append_path p }
13+
s.append_path File.dirname(React::Source.bundled_path_for('react-with-addons.js'))
1214
}

gemfiles/opal_master.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ gem 'opal-activesupport'
1313

1414
source 'https://rails-assets.org' do
1515
gem 'rails-assets-es5-shim'
16+
gem 'rails-assets-jquery'
1617
end

lib/react-opal.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
require 'react/opal/component'
44
require 'react/opal/element'
55
require 'react/opal/event'
6-
require 'react/opal/version'
7-
require 'react/opal/api'
6+
require 'react/opal/component_factory'
87
require 'react/opal/validator'
98
else
109
require 'opal'

lib/react/opal/api.rb

-115
This file was deleted.

lib/react/opal/component.rb

+12-113
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
require 'active_support/core_ext/class/attribute'
22
require 'react/opal/callbacks'
33
require 'react/opal/ext/hash'
4+
require 'react/opal/component/api'
45

56
module React
67
module Component
78
def self.included(base)
89
base.include(API)
910
base.include(React::Callbacks)
1011
base.class_eval do
11-
class_attribute :init_state, :validator, :context_types, :child_context_types, :child_context_get
12+
class_attribute :init_state, :validator
1213
define_callback :before_mount
1314
define_callback :after_mount
1415
define_callback :before_receive_props
@@ -19,29 +20,19 @@ def self.included(base)
1920
base.extend(ClassMethods)
2021
end
2122

22-
def initialize(native_element)
23-
@native = native_element
24-
end
25-
2623
def params
27-
Hash.new(`#{@native}.props`)
24+
Hash.new(`#{self}.props`).inject({}) do |memo, (k,v)|
25+
memo[k.underscore] = v
26+
memo
27+
end
2828
end
2929

3030
def refs
31-
Hash.new(`#{@native}.refs`)
32-
end
33-
34-
def context
35-
Hash.new(`#{@native}.context`)
36-
end
37-
38-
def state
39-
raise "No native ReactComponent associated" unless @native
40-
Hash.new(`#{@native}.state`)
31+
Hash.new(`#{self}.refs`)
4132
end
4233

4334
def emit(event_name, *args)
44-
self.params["_on#{event_name.to_s.camelize}"].call(*args)
35+
self.params["on_#{event_name.to_s}"].call(*args)
4536
end
4637

4738
def component_will_mount
@@ -108,12 +99,15 @@ def method_missing(name, *args, &block)
10899
element
109100
end
110101

102+
def to_n
103+
self
104+
end
111105

112106
module ClassMethods
113107
def prop_types
114108
if self.validator
115109
{
116-
_componentValidator: %x{
110+
_componentValidator: %x{
117111
function(props, propName, componentName) {
118112
var errors = #{validator.validate(Hash.new(`props`))};
119113
var error = new Error(#{"In component `" + self.name + "`\n" + `errors`.join("\n")});
@@ -142,55 +136,6 @@ def params(&block)
142136
end
143137
end
144138

145-
def define_state_prop(prop, &block)
146-
define_state prop
147-
update_value = lambda do |new_value|
148-
new_value = instance_exec(new_value, &block) if block
149-
self.send("#{prop}=", new_value)
150-
end
151-
before_mount do
152-
# need to execute in context of each object
153-
instance_exec params[prop], &update_value
154-
end
155-
before_receive_props do |new_props|
156-
# need to execute in context of each object
157-
instance_exec new_props[prop], &update_value
158-
end
159-
end
160-
161-
def get_prop_type(klass)
162-
if klass.is_a?(Proc)
163-
`React.PropTypes.object`
164-
elsif klass.ancestors.include?(Numeric)
165-
`React.PropTypes.number`
166-
elsif klass == String
167-
`React.PropTypes.string`
168-
elsif klass == Array
169-
`React.PropTypes.array`
170-
else
171-
`React.PropTypes.object`
172-
end
173-
end
174-
175-
def consume_context(item, klass)
176-
self.context_types ||= {}
177-
self.context_types[item] = get_prop_type(klass)
178-
end
179-
180-
def provide_context(item, klass, &block)
181-
self.child_context_types ||= {}
182-
self.child_context_types[item] = get_prop_type(klass)
183-
self.child_context_get ||= {}
184-
self.child_context_get[item] = block
185-
unless method_defined?(:get_child_context)
186-
define_method(:get_child_context) do
187-
Hash[self.child_context_get.map do |item, blk|
188-
[item, instance_eval(&blk)]
189-
end]
190-
end
191-
end
192-
end
193-
194139
def define_state(*states)
195140
raise "Block could be only given when define exactly one state" if block_given? && states.count > 1
196141

@@ -202,12 +147,10 @@ def define_state(*states)
202147
states.each do |name|
203148
# getter
204149
define_method("#{name}") do
205-
return unless @native
206150
self.state[name]
207151
end
208152
# setter
209153
define_method("#{name}=") do |new_state|
210-
return unless @native
211154
hash = {}
212155
hash[name] = new_state
213156
self.set_state(hash)
@@ -217,49 +160,5 @@ def define_state(*states)
217160
end
218161
end
219162
end
220-
221-
module API
222-
include Native
223-
224-
alias_native :dom_node, :getDOMNode
225-
alias_native :mounted?, :isMounted
226-
alias_native :force_update!, :forceUpdate
227-
228-
def set_props(prop, &block)
229-
raise "No native ReactComponent associated" unless @native
230-
%x{
231-
#{@native}.setProps(#{prop.shallow_to_n}, function(){
232-
#{block.call if block}
233-
});
234-
}
235-
end
236-
237-
def set_props!(prop, &block)
238-
raise "No native ReactComponent associated" unless @native
239-
%x{
240-
#{@native}.replaceProps(#{prop.shallow_to_n}, function(){
241-
#{block.call if block}
242-
});
243-
}
244-
end
245-
246-
def set_state(state, &block)
247-
raise "No native ReactComponent associated" unless @native
248-
%x{
249-
#{@native}.setState(#{state.shallow_to_n}, function(){
250-
#{block.call if block}
251-
});
252-
}
253-
end
254-
255-
def set_state!(state, &block)
256-
raise "No native ReactComponent associated" unless @native
257-
%x{
258-
#{@native}.replaceState(#{state.shallow_to_n}, function(){
259-
#{block.call if block}
260-
});
261-
}
262-
end
263-
end
264163
end
265164
end

lib/react/opal/component/api.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module React
2+
module Component
3+
module API
4+
def state
5+
Hash.new(`#{self}.state`)
6+
end
7+
8+
def props
9+
Hash.new(`#{self}.props`)
10+
end
11+
12+
def force_update!
13+
`#{self}.forceUpdate()`
14+
end
15+
16+
def set_state(state, &block)
17+
%x{
18+
#{self}.setState(#{state.shallow_to_n}, function(){
19+
#{block.call if block}
20+
});
21+
}
22+
end
23+
24+
def dom_node
25+
raise "`dom_node` is deprecated in favor of `React.find_dom_node`"
26+
end
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)