Class: React::API
Overview
TOOO - the code to deal with components should be moved to a module that will be included in a class which will then create the JS component for that class. That module will then be included in React::Component, but can be used by any class wanting to become a react component (but without other DSL characteristics.)
Constant Summary collapse
- @@component_classes =
{}
Class Method Summary collapse
- .add_after_error_hook(klass) ⇒ Object
- .add_after_error_hook_to_native(native_comp) ⇒ Object
- .clear_component_class_cache ⇒ Object
- .convert_props(properties) ⇒ Object
- .create_element(type, *args, &block) ⇒ Object
- .create_native_react_class(type) ⇒ Object
- .eval_native_react_component(name) ⇒ Object
- .import_native_component(opal_class, native_class) ⇒ Object
- .native_react_component?(name = nil) ⇒ Boolean
- .orig_convert_props ⇒ Object
Class Method Details
.add_after_error_hook(klass) ⇒ Object
43 44 45 |
# File 'lib/react/api.rb', line 43 def self.add_after_error_hook(klass) add_after_error_hook_to_native(@@component_classes[klass]) end |
.add_after_error_hook_to_native(native_comp) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/react/api.rb', line 47 def self.add_after_error_hook_to_native(native_comp) return unless native_comp %x{ native_comp.prototype.componentDidCatch = function(error, info) { this.__opalInstanceSyncSetState = false; this.__opalInstance.$component_did_catch(error, Opal.Hash.$new(info)); } } end |
.clear_component_class_cache ⇒ Object
191 192 193 |
# File 'lib/react/api.rb', line 191 def self.clear_component_class_cache @@component_classes = {} end |
.convert_props(properties) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/react/api.rb', line 195 def self.convert_props(args) # merge args together into a single properties hash properties = {} args.each do |arg| if arg.is_a? String properties[arg] = true elsif arg.is_a? Hash arg.each do |key, value| if ['class', 'className', 'class_name'].include? key next unless value if value.is_a?(String) value = value.split(' ') elsif !value.is_a?(Array) raise "The class param must be a string or array of strings" end properties['className'] = [*properties['className'], *value] elsif key == 'style' next unless value if !value.is_a?(Hash) raise "The style param must be a Hash" end properties['style'] = (properties['style'] || {}).merge(value) elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash) properties[key] = (properties[key] || {}).merge(value) else properties[key] = value end end end end # process properties according to react rules props = {} properties.each do |key, value| if ["style", "dangerously_set_inner_HTML"].include? key props[lower_camelize(key)] = value.to_n elsif key == "className" props[key] = value.join(' ') elsif key == "key" props["key"] = value.to_key elsif key == 'ref' && value.respond_to?(:call) # currently react still accepts the syntax ref: :foo meaning set refs.foo to the ref. # in hyperstack release 0.1 we can put this behavior on a switch or use the notation `ref_key: :foo` for old school props[key] = %x{ function(dom_node){ if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) { #{ value.call(`dom_node.__opalInstance`) }; } else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) { #{ value.call(`ReactDOM.findDOMNode(dom_node)`) }; } else { #{ value.call(`dom_node`) }; } } } elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash) value.each { |k, v| props["#{key}-#{k.gsub(/__|_/, '__' => '_', '_' => '-')}"] = v.to_n } else props[React.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value end end props end |
.create_element(type, *args, &block) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/react/api.rb', line 158 def self.create_element(type, *args, &block) params = [] # Component Spec, Normal DOM, String or Native Component ncc = @@component_classes[type] if ncc params << ncc elsif type.is_a?(Class) params << create_native_react_class(type) elsif block_given? || React::Component::Tags::HTML_TAGS.include?(type) params << type elsif type.is_a?(String) return React::Element.new(type) else raise "#{type} not implemented" end # Convert Passed in properties properties = convert_props(args) params << properties.shallow_to_n # Children Nodes if block_given? a = [yield].flatten %x{ for(var i=0, l=a.length; i<l; i++) { params.push(a[i].$to_n()); } } end React::Element.new(`React.createElement.apply(null, #{params})`, type, properties, block) end |
.create_native_react_class(type) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/react/api.rb', line 57 def self.create_native_react_class(type) raise "Provided class should define `render` method" if !(type.method_defined? :render) render_fn = (type.method_defined? :_render_wrapper) ? :_render_wrapper : :render # 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" @@component_classes[type] ||= begin comp = %x{ class extends React.Component { constructor(props) { super(props); this.mixins = #{type.respond_to?(:native_mixins) ? type.native_mixins : `[]`}; this.statics = #{type.respond_to?(:static_call_backs) ? type.static_call_backs.to_n : `{}`}; this.state = {}; this.__opalInstanceInitializedState = false; this.__opalInstanceSyncSetState = true; this.__opalInstance = #{type.new(`this`)}; this.__opalInstanceInitializedState = true; this.__opalInstanceSyncSetState = false; this.__name = #{type.name}; } static get displayName() { if (typeof this.__name != "undefined") { return this.__name; } else { return #{type.name}; } } static set displayName(name) { this.__name = name; } static get defaultProps() { return #{type.respond_to?(:default_props) ? type.default_props.to_n : `{}`}; } static get propTypes() { return #{type.respond_to?(:prop_types) ? type.prop_types.to_n : `{}`}; } componentWillMount() { if (#{type.method_defined? :component_will_mount}) { this.__opalInstanceSyncSetState = true; this.__opalInstance.$component_will_mount(); this.__opalInstanceSyncSetState = false; } } componentDidMount() { this.__opalInstance.is_mounted = true if (#{type.method_defined? :component_did_mount}) { this.__opalInstanceSyncSetState = false; this.__opalInstance.$component_did_mount(); } } componentWillReceiveProps(next_props) { if (#{type.method_defined? :component_will_receive_props}) { this.__opalInstanceSyncSetState = true; this.__opalInstance.$component_will_receive_props(Opal.Hash.$new(next_props)); this.__opalInstanceSyncSetState = false; } } shouldComponentUpdate(next_props, next_state) { if (#{type.method_defined? :should_component_update?}) { this.__opalInstanceSyncSetState = false; return this.__opalInstance["$should_component_update?"](Opal.Hash.$new(next_props), Opal.Hash.$new(next_state)); } else { return true; } } componentWillUpdate(next_props, next_state) { if (#{type.method_defined? :component_will_update}) { this.__opalInstanceSyncSetState = false; this.__opalInstance.$component_will_update(Opal.Hash.$new(next_props), Opal.Hash.$new(next_state)); } } componentDidUpdate(prev_props, prev_state) { if (#{type.method_defined? :component_did_update}) { this.__opalInstanceSyncSetState = false; this.__opalInstance.$component_did_update(Opal.Hash.$new(prev_props), Opal.Hash.$new(prev_state)); } } componentWillUnmount() { if (#{type.method_defined? :component_will_unmount}) { this.__opalInstanceSyncSetState = false; this.__opalInstance.$component_will_unmount(); } this.__opalInstance.is_mounted = false; } render() { this.__opalInstanceSyncSetState = false; return this.__opalInstance.$send(render_fn).$to_n(); } } } # check to see if there is an after_error callback. If there is add a # componentDidCatch handler. Because legacy behavior is to allow any object # that responds to render to act as a component we have to make sure that # we have a callbacks_for method. This all becomes much easier once issue # #270 is resolved. if type.respond_to?(:callbacks_for) && type.callbacks_for(:after_error) != [] add_after_error_hook_to_native comp end comp end end |
.eval_native_react_component(name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/react/api.rb', line 23 def self.eval_native_react_component(name) component = `eval(name)` raise "#{name} is not defined" if `#{component} === undefined` is_component_class = `#{component}.prototype !== undefined` && (`!!#{component}.prototype.isReactComponent` || `!!#{component}.prototype.render`) is_functional_component = `typeof #{component} === "function"` unless is_component_class || is_functional_component raise 'does not appear to be a native react component' end component end |
.import_native_component(opal_class, native_class) ⇒ Object
18 19 20 21 |
# File 'lib/react/api.rb', line 18 def self.import_native_component(opal_class, native_class) opal_class.instance_variable_set("@native_import", true) @@component_classes[opal_class] = native_class end |
.native_react_component?(name = nil) ⇒ Boolean
36 37 38 39 40 41 |
# File 'lib/react/api.rb', line 36 def self.native_react_component?(name = nil) return false unless name eval_native_react_component(name) rescue nil end |
.orig_convert_props ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/react/ref_callback.rb', line 9 def self.convert_props(args) # merge args together into a single properties hash properties = {} args.each do |arg| if arg.is_a? String properties[arg] = true elsif arg.is_a? Hash arg.each do |key, value| if ['class', 'className', 'class_name'].include? key next unless value if value.is_a?(String) value = value.split(' ') elsif !value.is_a?(Array) raise "The class param must be a string or array of strings" end properties['className'] = [*properties['className'], *value] elsif key == 'style' next unless value if !value.is_a?(Hash) raise "The style param must be a Hash" end properties['style'] = (properties['style'] || {}).merge(value) elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash) properties[key] = (properties[key] || {}).merge(value) else properties[key] = value end end end end # process properties according to react rules props = {} properties.each do |key, value| if ["style", "dangerously_set_inner_HTML"].include? key props[lower_camelize(key)] = value.to_n elsif key == "className" props[key] = value.join(' ') elsif key == "key" props["key"] = value.to_key elsif key == 'ref' && value.respond_to?(:call) # currently react still accepts the syntax ref: :foo meaning set refs.foo to the ref. # in hyperstack release 0.1 we can put this behavior on a switch or use the notation `ref_key: :foo` for old school props[key] = %x{ function(dom_node){ if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) { #{ value.call(`dom_node.__opalInstance`) }; } else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) { #{ value.call(`ReactDOM.findDOMNode(dom_node)`) }; } else { #{ value.call(`dom_node`) }; } } } elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash) value.each { |k, v| props["#{key}-#{k.gsub(/__|_/, '__' => '_', '_' => '-')}"] = v.to_n } else props[React.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value end end props end |