module React::Component::ClassMethods

Public Instance Methods

consume_context(item, klass) click to toggle source
# File lib/react/opal/component.rb, line 177
def consume_context(item, klass)
  self.context_types ||= {}
  self.context_types[item] = get_prop_type(klass)
end
default_props() click to toggle source
# File lib/react/opal/component.rb, line 131
def default_props
  self.validator ? self.validator.default_props : {}
end
define_state(*states) { || ... } click to toggle source
# File lib/react/opal/component.rb, line 196
def define_state(*states)
  raise "Block could be only given when define exactly one state" if block_given? && states.count > 1

  self.init_state = {} unless self.init_state

  if block_given?
    self.init_state[states[0]] = yield
  end
  states.each do |name|
    # getter
    define_method("#{name}") do
      self.state[name]
    end
    # setter
    define_method("#{name}=") do |new_state|
      hash = {}
      hash[name] = new_state
      self.set_state(hash)

      new_state
    end
  end
end
define_state_prop(prop, &block) click to toggle source
# File lib/react/opal/component.rb, line 143
def define_state_prop(prop, &block)
  define_state prop
  update_value = lambda do |new_value|
    new_value = instance_exec(new_value, &block) if block
    self.send("#{prop}=", new_value)
  end
  before_mount do
    # need to execute in context of each object
    instance_exec params[prop], &update_value
  end
  before_receive_props do |new_props|
    # need to execute in context of each object
    instance_exec new_props[prop], &update_value
  end
end
get_prop_type(klass) click to toggle source
# File lib/react/opal/component.rb, line 159
def get_prop_type(klass)
  if klass == Proc
    `React.PropTypes.func`
  elsif klass.is_a?(Proc)
    `React.PropTypes.object`
  elsif klass == Boolean
    `React.PropTypes.bool`
  elsif klass.ancestors.include?(Numeric)
    `React.PropTypes.number`
  elsif klass == String
    `React.PropTypes.string`
  elsif klass == Array
    `React.PropTypes.array`
  else
    `React.PropTypes.object`
  end
end
initial_state() click to toggle source
# File lib/react/opal/component.rb, line 127
def initial_state
  self.init_state || {}
end
params(&block) click to toggle source
# File lib/react/opal/component.rb, line 135
def params(&block)
  if self.validator
    self.validator.evaluate_more_rules(&block)
  else
    self.validator = React::Validator.build(&block)
  end
end
prop_types() click to toggle source
# File lib/react/opal/component.rb, line 111
def prop_types
  if self.validator
    {
        _componentValidator: %x{
        function(props, propName, componentName) {
          var errors = #{validator.validate(Hash.new(`props`))};
          var error = new Error(#{"In component `" + self.name + "`\n" + `errors`.join("\n")});
          return #{`errors`.count > 0 ? `error` : `undefined`};
        }
      }
    }
  else
    {}
  end
end
provide_context(item, klass, &block) click to toggle source
# File lib/react/opal/component.rb, line 182
def provide_context(item, klass, &block)
  self.child_context_types ||= {}
  self.child_context_types[item] = get_prop_type(klass)
  self.child_context_get ||= {}
  self.child_context_get[item] = block
  unless method_defined?(:get_child_context)
    define_method(:get_child_context) do
      Hash[self.child_context_get.map do |item, blk|
             [item, instance_eval(&blk)]
           end].to_n
    end
  end
end