1
1
require 'active_support/core_ext/class/attribute'
2
2
require 'react/opal/callbacks'
3
3
require 'react/opal/ext/hash'
4
+ require 'react/opal/component/api'
4
5
5
6
module React
6
7
module Component
7
8
def self . included ( base )
8
9
base . include ( API )
9
10
base . include ( React ::Callbacks )
10
11
base . class_eval do
11
- class_attribute :init_state , :validator , :context_types , :child_context_types , :child_context_get
12
+ class_attribute :init_state , :validator
12
13
define_callback :before_mount
13
14
define_callback :after_mount
14
15
define_callback :before_receive_props
@@ -19,29 +20,19 @@ def self.included(base)
19
20
base . extend ( ClassMethods )
20
21
end
21
22
22
- def initialize ( native_element )
23
- @native = native_element
24
- end
25
-
26
23
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
28
28
end
29
29
30
30
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` )
41
32
end
42
33
43
34
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 )
45
36
end
46
37
47
38
def component_will_mount
@@ -108,12 +99,15 @@ def method_missing(name, *args, &block)
108
99
element
109
100
end
110
101
102
+ def to_n
103
+ self
104
+ end
111
105
112
106
module ClassMethods
113
107
def prop_types
114
108
if self . validator
115
109
{
116
- _componentValidator : %x{
110
+ _componentValidator : %x{
117
111
function(props, propName, componentName) {
118
112
var errors = #{ validator . validate ( Hash . new ( `props` ) ) } ;
119
113
var error = new Error(#{ "In component `" + self . name + "`\n " + `errors` . join ( "\n " ) } );
@@ -142,55 +136,6 @@ def params(&block)
142
136
end
143
137
end
144
138
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
-
194
139
def define_state ( *states )
195
140
raise "Block could be only given when define exactly one state" if block_given? && states . count > 1
196
141
@@ -202,12 +147,10 @@ def define_state(*states)
202
147
states . each do |name |
203
148
# getter
204
149
define_method ( "#{ name } " ) do
205
- return unless @native
206
150
self . state [ name ]
207
151
end
208
152
# setter
209
153
define_method ( "#{ name } =" ) do |new_state |
210
- return unless @native
211
154
hash = { }
212
155
hash [ name ] = new_state
213
156
self . set_state ( hash )
@@ -217,49 +160,5 @@ def define_state(*states)
217
160
end
218
161
end
219
162
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
264
163
end
265
164
end
0 commit comments