module React

Constants

ATTRIBUTES
HTML_TAGS
VERSION

Public Class Methods

clear_component_class_cache() click to toggle source
# File lib/react/api.rb, line 40
def self.clear_component_class_cache
  @@component_classes = {}
end
create_element(type, properties = {}, &block) click to toggle source
# File lib/react/top_level.rb, line 25
def self.create_element(type, properties = {}, &block)
  React::API.create_element(type, properties, &block)
end
expose_native_class(*args) click to toggle source
# File lib/react/top_level.rb, line 51
def self.expose_native_class(*args)
  args.each do |klass|
    `window[#{klass.to_s}] = #{React::API.native_component_class(klass)}`
  end
end
is_valid_element(element) click to toggle source
# File lib/react/top_level.rb, line 35
def self.is_valid_element(element)
  `React.isValidElement(#{element})`
end
native_component_class(type) click to toggle source
# File lib/react/api.rb, line 44
def self.native_component_class(type)
  @@component_classes[type.to_s] ||= %x{
    React.createClass({
      propTypes: #{type.respond_to?(:prop_types) ? type.prop_types.to_n : `{}`},
      getDefaultProps: function(){
        return #{type.respond_to?(:default_props) ? type.default_props.to_n : `{}`};
      },
      getInitialState: function(){
        return #{type.respond_to?(:initial_state) ? type.initial_state.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;
      },
      displayName: #{type.to_s},
      render: function() {
        var instance = this._getOpalInstance.apply(this);
        return instance.$render();
      }
    })
  }
end
render(element, container) click to toggle source
# File lib/react/top_level.rb, line 29
def self.render(element, container)
  component = Native(`React.render(#{element}, container, function(){#{yield if block_given?}})`)
  component.class.include(React::Component::API)
  component
end
render_to_static_markup(element) click to toggle source
# File lib/react/top_level.rb, line 43
def self.render_to_static_markup(element)
  `React.renderToStaticMarkup(#{element})`
end
render_to_string(element) click to toggle source
# File lib/react/top_level.rb, line 39
def self.render_to_string(element)
  `React.renderToString(#{element})`
end
unmount_component_at_node(node) click to toggle source
# File lib/react/top_level.rb, line 47
def self.unmount_component_at_node(node)
  `React.unmountComponentAtNode(node)`
end

Private Class Methods

lower_camelize(snake_cased_word) click to toggle source
# File lib/react/api.rb, line 102
def self.lower_camelize(snake_cased_word)
  words = snake_cased_word.split("_")
  result = [words.first]
  result.concat(words[1..-1].map {|word| word[0].upcase + word[1..-1] })
  result.join("")
end