Class: React::Element

Inherits:
Object
  • Object
show all
Includes:
Native
Defined in:
lib/react/element.rb

Instance Method Summary collapse

Constructor Details

#initialize(native_element) ⇒ Element

Returns a new instance of Element.



10
11
12
# File 'lib/react/element.rb', line 10

def initialize(native_element)
  @native = native_element
end

Instance Method Details

#childrenObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/react/element.rb', line 32

def children
  nodes = self.props.children
  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(React::Element.new(`context`))}
          })
        }
      else
        Enumerator.new(`React.Children.count(#{self.to_n})`) do |y|
          %x{
            React.Children.forEach(#{self.to_n}, function(context){
              #{y << React::Element.new(`context`)}
            })
          }
        end
      end
    end
  end

  nodes
end

#on(event_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/react/element.rb', line 14

def on(event_name)
  name = event_name.to_s.event_camelize
  if React::Event::BUILT_IN_EVENTS.include?("on#{name}")
    self.props["on#{name}"] = %x{
      function(event){
        #{yield React::Event.new(`event`)}
      }
    }
  else
    self.props["_on#{name}"] = %x{
      function(){
        #{yield *Array(`arguments`)}
      }
    }
  end
  self
end