Module: React::PropsChildren

Included in:
NativeElement
Defined in:
lib/react/opal/props_children.rb

Instance Method Summary collapse

Instance Method Details

#childrenObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/react/opal/props_children.rb', line 7

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

#propsObject



3
4
5
# File 'lib/react/opal/props_children.rb', line 3

def props
  Hash.new(`#{self}.props`)
end