class React::API

Public Class Methods

create_element(type, properties = {}) { |].each do |ele| params << to_n| ... } click to toggle source
# File lib/react/api.rb, line 5
def self.create_element(type, properties = {}, &block)
  params = []

  # Component Spec or Nomral DOM
  if `(typeof type === 'function')`
    params << type
  elsif type.kind_of?(Class)
    raise "Provided class should define `render` method"  if !(type.method_defined? :render)
    params << self.native_component_class(type)
  else
    raise "#{type} not implemented" unless HTML_TAGS.include?(type)
    params << type
  end

  # Passed in properties
  props = {}
  properties.map do |key, value|
     if key == "class_name" && value.is_a?(Hash)
       props[lower_camelize(key)] = value.inject([]) {|ary, (k,v)| v ? ary.push(k) : ary}.join(" ")
     else
       props[React::ATTRIBUTES.include?(lower_camelize(key)) ? lower_camelize(key) : key] = value
     end
  end
  params << props.shallow_to_n

  # Children Nodes
  if block_given?
    children = [yield].flatten.each do |ele|
      params << ele.to_n
    end
  end