@@ -44,94 +44,106 @@ def self.create_native_react_class(type)
44
44
raise "Provided class should define `render` method" if !( type . method_defined? :render )
45
45
render_fn = ( type . method_defined? :_render_wrapper ) ? :_render_wrapper : :render
46
46
# this was hashing type.to_s, not sure why but .to_s does not work as it Foo::Bar::View.to_s just returns "View"
47
- @@component_classes [ type ] ||= %x{
48
- class extends React.Component {
49
- constructor(props) {
50
- super(props);
51
- this.mixins = #{ type . respond_to? ( :native_mixins ) ? type . native_mixins : `[]` } ;
52
- this.statics = #{ type . respond_to? ( :static_call_backs ) ? type . static_call_backs . to_n : `{}` } ;
53
- this.state = {};
54
- this.__opalInstanceInitializedState = false;
55
- this.__opalInstanceSyncSetState = true;
56
- this.__opalInstance = #{ type . new ( `this` ) } ;
57
- this.__opalInstanceInitializedState = true;
58
- this.__opalInstanceSyncSetState = false;
59
- this.__name = #{ type . name } ;
60
- }
61
- static get displayName() {
62
- if (typeof this.__name != "undefined") {
63
- return this.__name;
64
- } else {
65
- return #{ type . name } ;
66
- }
67
- }
68
- static set displayName(name) {
69
- this.__name = name;
70
- }
71
- static get defaultProps() {
72
- return #{ type . respond_to? ( :default_props ) ? type . default_props . to_n : `{}` } ;
73
- }
74
- static get propTypes() {
75
- return #{ type . respond_to? ( :prop_types ) ? type . prop_types . to_n : `{}` } ;
76
- }
77
- componentWillMount() {
78
- if (#{ type . method_defined? :component_will_mount } ) {
47
+
48
+ @@component_classes [ type ] ||= begin
49
+ comp = %x{
50
+ class extends React.Component {
51
+ constructor(props) {
52
+ super(props);
53
+ this.mixins = #{ type . respond_to? ( :native_mixins ) ? type . native_mixins : `[]` } ;
54
+ this.statics = #{ type . respond_to? ( :static_call_backs ) ? type . static_call_backs . to_n : `{}` } ;
55
+ this.state = {};
56
+ this.__opalInstanceInitializedState = false;
79
57
this.__opalInstanceSyncSetState = true;
80
- this.__opalInstance.$component_will_mount();
58
+ this.__opalInstance = #{ type . new ( `this` ) } ;
59
+ this.__opalInstanceInitializedState = true;
81
60
this.__opalInstanceSyncSetState = false;
61
+ this.__name = #{ type . name } ;
82
62
}
83
- }
84
- componentDidMount( ) {
85
- this.__opalInstance.is_mounted = true
86
- if ( #{ type . method_defined? :component_did_mount } ) {
87
- this.__opalInstanceSyncSetState = false ;
88
- this.__opalInstance.$component_did_mount();
63
+ static get displayName() {
64
+ if (typeof this.__name != "undefined" ) {
65
+ return this.__name;
66
+ } else {
67
+ return #{ type . name } ;
68
+ }
89
69
}
90
- }
91
- componentWillReceiveProps(next_props) {
92
- if (#{ type . method_defined? :component_will_receive_props } ) {
93
- this.__opalInstanceSyncSetState = true;
94
- this.__opalInstance.$component_will_receive_props(Opal.Hash.$new(next_props));
95
- this.__opalInstanceSyncSetState = false;
70
+ static set displayName(name) {
71
+ this.__name = name;
96
72
}
97
- }
98
- shouldComponentUpdate(next_props, next_state) {
99
- if (#{ type . method_defined? :should_component_update? } ) {
100
- this.__opalInstanceSyncSetState = false;
101
- return this.__opalInstance["$should_component_update?"](Opal.Hash.$new(next_props), Opal.Hash.$new(next_state));
102
- } else { return true; }
103
- }
104
- componentWillUpdate(next_props, next_state) {
105
- if (#{ type . method_defined? :component_will_update } ) {
106
- this.__opalInstanceSyncSetState = false;
107
- this.__opalInstance.$component_will_update(Opal.Hash.$new(next_props), Opal.Hash.$new(next_state));
73
+ static get defaultProps() {
74
+ return #{ type . respond_to? ( :default_props ) ? type . default_props . to_n : `{}` } ;
108
75
}
109
- }
110
- componentDidUpdate(prev_props, prev_state) {
111
- if (#{ type . method_defined? :component_did_update } ) {
112
- this.__opalInstanceSyncSetState = false;
113
- this.__opalInstance.$component_did_update(Opal.Hash.$new(prev_props), Opal.Hash.$new(prev_state));
76
+ static get propTypes() {
77
+ return #{ type . respond_to? ( :prop_types ) ? type . prop_types . to_n : `{}` } ;
114
78
}
115
- }
116
- componentWillUnmount() {
117
- if (#{ type . method_defined? :component_will_unmount } ) {
79
+ componentWillMount() {
80
+ if (#{ type . method_defined? :component_will_mount } ) {
81
+ this.__opalInstanceSyncSetState = true;
82
+ this.__opalInstance.$component_will_mount();
83
+ this.__opalInstanceSyncSetState = false;
84
+ }
85
+ }
86
+ componentDidMount() {
87
+ this.__opalInstance.is_mounted = true
88
+ if (#{ type . method_defined? :component_did_mount } ) {
89
+ this.__opalInstanceSyncSetState = false;
90
+ this.__opalInstance.$component_did_mount();
91
+ }
92
+ }
93
+ componentWillReceiveProps(next_props) {
94
+ if (#{ type . method_defined? :component_will_receive_props } ) {
95
+ this.__opalInstanceSyncSetState = true;
96
+ this.__opalInstance.$component_will_receive_props(Opal.Hash.$new(next_props));
97
+ this.__opalInstanceSyncSetState = false;
98
+ }
99
+ }
100
+ shouldComponentUpdate(next_props, next_state) {
101
+ if (#{ type . method_defined? :should_component_update? } ) {
102
+ this.__opalInstanceSyncSetState = false;
103
+ return this.__opalInstance["$should_component_update?"](Opal.Hash.$new(next_props), Opal.Hash.$new(next_state));
104
+ } else { return true; }
105
+ }
106
+ componentWillUpdate(next_props, next_state) {
107
+ if (#{ type . method_defined? :component_will_update } ) {
108
+ this.__opalInstanceSyncSetState = false;
109
+ this.__opalInstance.$component_will_update(Opal.Hash.$new(next_props), Opal.Hash.$new(next_state));
110
+ }
111
+ }
112
+ componentDidUpdate(prev_props, prev_state) {
113
+ if (#{ type . method_defined? :component_did_update } ) {
114
+ this.__opalInstanceSyncSetState = false;
115
+ this.__opalInstance.$component_did_update(Opal.Hash.$new(prev_props), Opal.Hash.$new(prev_state));
116
+ }
117
+ }
118
+ componentWillUnmount() {
119
+ if (#{ type . method_defined? :component_will_unmount } ) {
120
+ this.__opalInstanceSyncSetState = false;
121
+ this.__opalInstance.$component_will_unmount();
122
+ }
123
+ this.__opalInstance.is_mounted = false;
124
+ }
125
+
126
+ render() {
118
127
this.__opalInstanceSyncSetState = false;
119
- this.__opalInstance.$component_will_unmount ();
128
+ return this.__opalInstance.$send(render_fn).$to_n ();
120
129
}
121
- this.__opalInstance.is_mounted = false;
122
130
}
123
- componentDidCatch(error, info) {
124
- if (#{ type . method_defined? :component_did_catch } ) {
131
+ }
132
+ # check to see if there is an after_error callback. If there is add a
133
+ # componentDidCatch handler. Because legacy behavior is to allow any object
134
+ # that responds to render to act as a component we have to make sure that
135
+ # we have a callbacks_for method. This all becomes much easier once issue
136
+ # #270 is resolved.
137
+ if type . respond_to? ( :callbacks_for ) && type . callbacks_for ( :after_error ) != [ ]
138
+ %x{
139
+ comp.prototype.componentDidCatch = function(error, info) {
125
140
this.__opalInstanceSyncSetState = false;
126
141
this.__opalInstance.$component_did_catch(error, Opal.Hash.$new(info));
127
142
}
128
143
}
129
- render() {
130
- this.__opalInstanceSyncSetState = false;
131
- return this.__opalInstance.$send(render_fn).$to_n();
132
- }
133
- }
134
- }
144
+ end
145
+ comp
146
+ end
135
147
end
136
148
137
149
def self . create_element ( type , properties = { } , &block )
@@ -182,7 +194,7 @@ def self.convert_props(properties)
182
194
183
195
elsif key == "key"
184
196
props [ "key" ] = value . to_key
185
-
197
+
186
198
elsif key == 'ref' && value . is_a? ( Proc )
187
199
props [ key ] = %x{
188
200
function(dom_node){
0 commit comments