class React::Element

Public Class Methods

new() click to toggle source
# File lib/react/element.rb, line 5
def self.new
  raise "use React.create_element instead"
end

Public Instance Methods

children() click to toggle source
# File lib/react/element.rb, line 50
def children
  nodes = `self.props.children`
  
  if `React.Children.count(nodes)` == 0
    `[]`
  elsif `React.Children.count(nodes)` == 1
    if `(typeof nodes === 'string') || (typeof nodes === 'number')`
      [nodes]
    else
      `[React.Children.only(nodes)]`
    end
  else
    # Not sure the overhead of doing this..
    class << nodes
      include Enumerable

      def to_n
        self
      end

      def each(&block)
        if block_given?
          %x{
            React.Children.forEach(#{self.to_n}, function(context){
              #{block.call(`context`)}
            })
          }
        else
          Enumerator.new(`React.Children.count(#{self.to_n})`) do |y|
            %x{
              React.Children.forEach(#{self.to_n}, function(context){
                #{y << `context`}
              })
            }
          end
        end
      end
    end
    
    nodes
  end
end
element_type() click to toggle source
# File lib/react/element.rb, line 9
def element_type
  `self.type`
end
key() click to toggle source
# File lib/react/element.rb, line 13
def key
  Native(`self.key`)
end
on(event_name) click to toggle source
# File lib/react/element.rb, line 25
def on(event_name)
  name = event_name.to_s.event_camelize
  
  
  if React::Event::BUILT_IN_EVENTS.include?("on#{name}")
    prop_key = "on#{name}"
    callback =  %x{
      function(event){
        #{yield React::Event.new(`event`)}
      }
    }
  else
    prop_key = "_on#{name}"
    callback = %x{
      function(){
        #{yield *Array(`arguments`)}
      }
    }
  end
  
  `self.props[#{prop_key}] = #{callback}`
  
  self
end
props() click to toggle source
# File lib/react/element.rb, line 17
def props
  Hash.new(`self.props`)
end
ref() click to toggle source
# File lib/react/element.rb, line 21
def ref
  Native(`self.ref`)
end
to_n() click to toggle source
# File lib/react/element.rb, line 93
def to_n
  self
end