Module: React::Component::API

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
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
49
50
51
# File 'lib/react/component/api.rb', line 4

def self.included(base)
  base.instance_exec do
    base_module = base.to_s.deconstantize
    if base_module != ''
      base_module.constantize.define_singleton_method(base.to_s.demodulize) do |*args, &block|
        `Opal.React.internal_prepare_args_and_render(#{base}.react_component, args, block)`
      end
    else
      Object.define_method(base.to_s) do |*args, &block|
        `Opal.React.internal_prepare_args_and_render(#{base}.react_component, args, block)`
      end
    end

    attr_accessor :props
    attr_accessor :state

    def ref(ref_name, &block)
      defined_refs.JS[ref_name] = block_given? ? block : `null`
    end

    def defined_refs
      @defined_ref ||= `{}`
    end

    def default_state_defined
      @default_state_defined
    end

    def state
      return @default_state if @default_state
      @default_state_defined = true
      %x{
        var native_state = {state: {}};
        native_state.setState = function(new_state, callback) {
          for (var key in new_state) {
            this.state[key] = new_state[key];
          }
          if (callback) { callback.call(); }
        }
      }
      @default_state = React::Component::State.new(`native_state`)
    end

    def render(&block)
      `base.render_block = block`
    end
  end
end

Instance Method Details

#display_nameObject



53
54
55
# File 'lib/react/component/api.rb', line 53

def display_name
  @native.JS[:displayName]
end

#force_update(&block) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/react/component/api.rb', line 57

def force_update(&block)
  if block_given?
    # this maybe needs instance_exec too
    @native.JS.forceUpdate(`function() { block.$call(); }`)
  else
    @native.JS.forceUpdate
  end
end

#get_react_element(arg, &block) ⇒ Object Also known as: gre



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/react/component/api.rb', line 66

def get_react_element(arg, &block)
  if block_given?
    # execute block, fetch last element from buffer
    %x{
      let last_buffer_length = Opal.React.render_buffer[Opal.React.render_buffer.length - 1].length;
      let last_buffer_element = Opal.React.render_buffer[Opal.React.render_buffer.length - 1][last_buffer_length - 1];
      block.$call();
      // console.log("get_react_element popping", Opal.React.render_buffer, Opal.React.render_buffer.toString())
      let new_element = Opal.React.render_buffer[Opal.React.render_buffer.length - 1].pop();
      if (last_buffer_element === new_element) { #{raise "Block did not create any React element!"} }
      return new_element;
    }
  else
    # element was rendered before being passed as arg
    # fetch last element from buffer
    # `console.log("get_react_element popping", Opal.React.render_buffer, Opal.React.render_buffer.toString())`
    `Opal.React.render_buffer[Opal.React.render_buffer.length - 1].pop()`
  end
end

#ref(name) ⇒ Object



95
96
97
# File 'lib/react/component/api.rb', line 95

def ref(name)
  `#@native[name]`
end

#render_react_element(el) ⇒ Object Also known as: rre



87
88
89
90
91
92
# File 'lib/react/component/api.rb', line 87

def render_react_element(el)
  # push el to buffer
  `Opal.React.render_buffer[Opal.React.render_buffer.length - 1].push(el)`
  # `console.log("render_react_element pushed", Opal.React.render_buffer, Opal.React.render_buffer.toString())`
  nil
end

#ruby_ref(name) ⇒ Object



99
100
101
102
# File 'lib/react/component/api.rb', line 99

def ruby_ref(name)
  return `#@native[name]` if `(typeof #@native[name] === 'function')`
  React::Ref::new(`#@native[name]`)
end

#set_state(updater, &callback) ⇒ Object



104
105
106
# File 'lib/react/component/api.rb', line 104

def set_state(updater, &callback)
  @state.set_state(updater, &callback)
end