Class: React::API

Inherits:
Object show all
Defined in:
lib/react/api.rb

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

Class Method Details

.clear_component_class_cacheObject



124
125
126
# File 'lib/react/api.rb', line 124

def self.clear_component_class_cache
  @@component_classes = {}
end

.convert_props(properties) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/react/api.rb', line 128

def self.convert_props(properties)
  raise "Component parameters must be a hash. Instead you sent #{properties}" unless properties.is_a? Hash
  props = {}
  properties.map do |key, value|
    if key == "class_name" && value.is_a?(Hash)
      props[lower_camelize(key)] = `React.addons.classSet(#{value.to_n})`
    elsif key == "class"
      props["className"] = value
    elsif ["style", "dangerously_set_inner_HTML"].include? key
      props[lower_camelize(key)] = value.to_n
    elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
      value.each { |k, v| props["#{key}-#{k.tr('_', '-')}"] = v.to_n }
    else
      props[React::ATTRIBUTES.include?(lower_camelize(key)) ? lower_camelize(key) : key] = value
    end
  end
  props.shallow_to_n
end

.create_element(type, properties = {}, &block) ⇒ Object



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
# File 'lib/react/api.rb', line 96

def self.create_element(type, properties = {}, &block)
  params = []

  # Component Spec, Normal DOM, String or Native Component
  if @@component_classes[type]
    params << @@component_classes[type]
  elsif type.kind_of?(Class)
    params << create_native_react_class(type)
  elsif 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

  # Passed in properties
  params << convert_props(properties)

  # Children Nodes
  if block_given?
    children = [yield].flatten.each do |ele|
      params << ele.to_n
    end
  end
  return React::Element.new(`React.createElement.apply(null, #{params})`, type, properties, block)
end

.create_native_react_class(type) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/react/api.rb', line 38

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] ||= %x{
    React.createClass({
      displayName: #{type.name},
      propTypes: #{type.respond_to?(:prop_types) ? type.prop_types.to_n : `{}`},
      getDefaultProps: function(){
        return #{type.respond_to?(:default_props) ? type.default_props.to_n : `{}`};
      },
      mixins: #{type.respond_to?(:native_mixins) ? type.native_mixins : `[]`},
      statics: #{type.respond_to?(:static_call_backs) ? type.static_call_backs.to_n : `{}`},
      componentWillMount: function() {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.component_will_mount if type.method_defined? :component_will_mount};
      },
      componentDidMount: function() {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.component_did_mount if type.method_defined? :component_did_mount};
      },
      componentWillReceiveProps: function(next_props) {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.component_will_receive_props(`next_props`) if type.method_defined? :component_will_receive_props};
      },
      shouldComponentUpdate: function(next_props, next_state) {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.should_component_update?(`next_props`, `next_state`) if type.method_defined? :should_component_update?};
      },
      componentWillUpdate: function(next_props, next_state) {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.component_will_update(`next_props`, `next_state`) if type.method_defined? :component_will_update};
      },
      componentDidUpdate: function(prev_props, prev_state) {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.component_did_update(`prev_props`, `prev_state`) if type.method_defined? :component_did_update};
      },
      componentWillUnmount: function() {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.component_will_unmount if type.method_defined? :component_will_unmount};
      },
      _getOpalInstance: function() {
        if (this.__opalInstance == undefined) {
          var instance = #{type.new(`this`)};
        } else {
          var instance = this.__opalInstance;
        }
        this.__opalInstance = instance;
        return instance;
      },
      render: function() {
        var instance = this._getOpalInstance.apply(this);
        return #{`instance`.send(render_fn).to_n};
      }
    })
  }
end

.eval_native_react_component(name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/react/api.rb', line 22

def self.eval_native_react_component(name)
  component = `eval(name)`
  raise "#{name} is not defined" if `#{component} === undefined`
  unless `#{component}.prototype !== undefined` &&
         (`!!#{component}.prototype.isReactComponent` || `!!#{component}.prototype.render`)
    raise 'does not appear to be a native react component'
  end
  component
end

.import_native_component(opal_class, native_class) ⇒ Object



18
19
20
# File 'lib/react/api.rb', line 18

def self.import_native_component(opal_class, native_class)
  @@component_classes[opal_class] = native_class
end

.native_react_component?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/react/api.rb', line 32

def self.native_react_component?(name)
  eval_native_react_component(name)
rescue
  nil
end