Class: React::Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(native_element, type, properties, block) ⇒ Element

Returns a new instance of Element.



16
17
18
19
20
21
# File 'lib/react/element.rb', line 16

def initialize(native_element, type, properties, block)
  @type = type
  @properties = (`typeof #{properties} === 'undefined'` ? nil : properties) || {}
  @block = block
  @native = native_element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(class_name, args = {}, &new_block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/react/element.rb', line 58

def method_missing(class_name, args = {}, &new_block)
  class_name = class_name.split("__").collect { |s| s.gsub("_", "-") }.join("_")
  new_props = properties.dup
  new_props["class"] = "#{new_props['class']} #{class_name} #{args.delete("class")} #{args.delete('className')}".split(" ").uniq.join(" ")
  new_props.merge! args
  React::RenderingContext.replace(
    self,
    React::RenderingContext.build { React::RenderingContext.render(type, new_props, &new_block) }
  )
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/react/element.rb', line 12

def block
  @block
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#waiting_on_resourcesObject

Returns the value of attribute waiting_on_resources.



14
15
16
# File 'lib/react/element.rb', line 14

def waiting_on_resources
  @waiting_on_resources
end

Instance Method Details

#as_nodeObject



69
70
71
# File 'lib/react/element.rb', line 69

def as_node
  React::RenderingContext.as_node(self)
end

#deleteObject



73
74
75
# File 'lib/react/element.rb', line 73

def delete
  React::RenderingContext.delete(self)
end

#on(event_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/react/element.rb', line 23

def on(event_name)
  name = event_name.to_s.event_camelize
  props = if React::Event::BUILT_IN_EVENTS.include?("on#{name}")
    {"on#{name}" => %x{
      function(event){
        #{yield React::Event.new(`event`)}
      }
    }}
  else
    {"_on#{name}" => %x{
      function(){
        #{yield *Array(`arguments`)}
      }
    }}
  end
  @native = `React.cloneElement(#{self.to_n}, #{props.to_n})`
  @properties.merge! props
  self
end

#render(props = {}) ⇒ Object

for rendering children



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/react/element.rb', line 43

def render(props = {})  # for rendering children
  if props.empty?
    React::RenderingContext.render(self)
  else
    React::RenderingContext.render(
      Element.new(
        `React.cloneElement(#{self.to_n}, #{API.convert_props(props)})`,
        type,
        properties.merge(props),
        block
      )
    )
  end
end