Module: React::Component

Defined in:
lib/react/opal/component.rb,
lib/react/opal/component/api.rb

Defined Under Namespace

Modules: API, ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/react/opal/component.rb', line 78

def method_missing(name, *args, &block)
  unless (React::HTML_TAGS.include?(name) || name == 'present' || name == '_p_tag')
    return super
  end

  if name == "present"
    name = args.shift
  end

  if name == "_p_tag"
    name = "p"
  end

  @buffer = [] unless @buffer
  if block
    current = @buffer
    @buffer = []
    result = block.call
    element = React.create_element(name, *args) { @buffer.count == 0 ? result : @buffer }
    @buffer = current
  else
    element = React.create_element(name, *args)
  end

  @buffer << element
  element
end

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/react/opal/component.rb', line 8

def self.included(base)
  base.include(API)
  base.include(React::Callbacks)
  base.class_eval do
    class_attribute :init_state, :validator, :context_types, :child_context_types, :child_context_get
    define_callback :before_mount
    define_callback :after_mount
    define_callback :before_receive_props
    define_callback :before_update
    define_callback :after_update
    define_callback :before_unmount
  end
  base.extend(ClassMethods)
end

Instance Method Details

#component_did_mountObject



46
47
48
# File 'lib/react/opal/component.rb', line 46

def component_did_mount
  self.run_callback(:after_mount)
end

#component_did_update(prev_props, prev_state) ⇒ Object



62
63
64
# File 'lib/react/opal/component.rb', line 62

def component_did_update(prev_props, prev_state)
  self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state))
end

#component_will_mountObject



42
43
44
# File 'lib/react/opal/component.rb', line 42

def component_will_mount
  self.run_callback(:before_mount)
end

#component_will_receive_props(next_props) ⇒ Object



50
51
52
# File 'lib/react/opal/component.rb', line 50

def component_will_receive_props(next_props)
  self.run_callback(:before_receive_props, Hash.new(next_props))
end

#component_will_unmountObject



66
67
68
# File 'lib/react/opal/component.rb', line 66

def component_will_unmount
  self.run_callback(:before_unmount)
end

#component_will_update(next_props, next_state) ⇒ Object



58
59
60
# File 'lib/react/opal/component.rb', line 58

def component_will_update(next_props, next_state)
  self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state))
end

#contextObject



34
35
36
# File 'lib/react/opal/component.rb', line 34

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

#emit(event_name, *args) ⇒ Object



38
39
40
# File 'lib/react/opal/component.rb', line 38

def emit(event_name, *args)
  self.params["on_#{event_name.to_s}"].call(*args)
end

#p(*args, &block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/react/opal/component.rb', line 70

def p(*args, &block)
  if block || args.count == 0 || (args.count == 1 && args.first.is_a?(Hash))
    _p_tag(*args, &block)
  else
    Kernel.p(*args)
  end
end

#paramsObject



23
24
25
26
27
28
# File 'lib/react/opal/component.rb', line 23

def params
  Hash.new(`#{self}.props`).inject({}) do |memo, (k, v)|
    memo[k.underscore] = v
    memo
  end
end

#refsObject



30
31
32
# File 'lib/react/opal/component.rb', line 30

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

#should_component_update?(next_props, next_state) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/react/opal/component.rb', line 54

def should_component_update?(next_props, next_state)
  self.respond_to?(:needs_update?) ? self.needs_update?(Hash.new(next_props), Hash.new(next_state)) : true
end

#to_nObject



106
107
108
# File 'lib/react/opal/component.rb', line 106

def to_n
  self
end