Module: React

Defined in:
lib/react/opal/event.rb,
lib/react/opal/testing.rb,
lib/react/opal/version.rb,
lib/react/opal/callbacks.rb,
lib/react/opal/component.rb,
lib/react/opal/top_level.rb,
lib/react/opal/validator.rb,
lib/react/opal/component/api.rb,
lib/react/opal/native_element.rb,
lib/react/opal/props_children.rb,
lib/react/opal/component_factory.rb

Defined Under Namespace

Modules: Callbacks, Component, PropsChildren, Testing Classes: ComponentFactory, Event, NativeElement, Validator

Constant Summary collapse

VERSION =
'0.14.1'
HTML_TAGS =
%w(a abbr address area article aside audio b base bdi bdo big blockquote body br
button canvas caption cite code col colgroup data datalist dd del details dfn
dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5
h6 head header hr html i iframe img input ins kbd keygen label legend li link
main map mark menu menuitem meta meter nav noscript object ol optgroup option
output p param picture pre progress q rp rt ruby s samp script section select
small source span strong style sub summary sup table tbody td textarea tfoot th
thead time title tr track u ul var video wbr)

Class Method Summary collapse

Class Method Details

.camel_case_hash_keys(input) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/react/opal/top_level.rb', line 57

def self.camel_case_hash_keys(input)
  as_array = input.map do |key, value|
    new_value = block_given? ? yield(key, value) : value
    [lower_camelize(key), new_value]
  end
  Hash[as_array]
end

.create_element(type, properties = {}) ⇒ Object



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
# File 'lib/react/opal/top_level.rb', line 14

def self.create_element(type, properties = {})
  params = []

  # Component Spec or Normal DOM
  native = `(typeof type === 'function')` || HTML_TAGS.include?(type)
  params << if native
              type
            elsif type.kind_of?(Class)
              raise "Provided class should define `render` method" if !(type.method_defined? :render)
              React::ComponentFactory.native_component_class(type)
            else
              raise "#{type} not implemented"
            end

  # Passed in properties
  props = camel_case_hash_keys(properties) do |key, value|
    if key == "class_name" && value.is_a?(Hash)
      value.inject([]) { |ary, (k, v)| v ? ary.push(k) : ary }.join(" ")
    elsif key == 'value_link'
      process_value_link value
    else
      value
    end
  end

  params << props.shallow_to_n

  # Children Nodes
  if block_given?
    [yield].flatten.each do |ele|
      params << ele
    end
  end

  element = `React.createElement.apply(null, #{params})`
  React::NativeElement.new(element)
end

.expose_native_class(*args) ⇒ Object



92
93
94
95
96
# File 'lib/react/opal/top_level.rb', line 92

def self.expose_native_class(*args)
  args.each do |klass|
    `window[#{klass.to_s}] = #{React::ComponentFactory.native_component_class(klass)}`
  end
end

.find_dom_node(component) ⇒ Object



98
99
100
# File 'lib/react/opal/top_level.rb', line 98

def self.find_dom_node(component)
  `ReactDOM.findDOMNode(#{component})`
end

.is_valid_element(element) ⇒ Object



76
77
78
# File 'lib/react/opal/top_level.rb', line 76

def self.is_valid_element(element)
  `React.isValidElement(#{element})`
end

.lower_camelize(str) ⇒ Object



52
53
54
55
# File 'lib/react/opal/top_level.rb', line 52

def self.lower_camelize(str)
  camelized = str.camelize
  camelized[0].downcase + camelized[1..-1]
end


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

def self.process_value_link(arguments)
  arguments = arguments.call if arguments.is_a? Proc
  camel_case_hash_keys(arguments).to_n
end

.render(element, container) ⇒ Object



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

def self.render(element, container)
  component = Native(`ReactDOM.render(#{element}, container, function(){#{yield if block_given?}})`)
  component.class.include(React::Component::API)
  component
end

.render_to_static_markup(element) ⇒ Object



84
85
86
# File 'lib/react/opal/top_level.rb', line 84

def self.render_to_static_markup(element)
  `ReactDOMServer.renderToStaticMarkup(#{element})`
end

.render_to_string(element) ⇒ Object



80
81
82
# File 'lib/react/opal/top_level.rb', line 80

def self.render_to_string(element)
  `ReactDOMServer.renderToString(#{element})`
end

.unmount_component_at_node(node) ⇒ Object



88
89
90
# File 'lib/react/opal/top_level.rb', line 88

def self.unmount_component_at_node(node)
  `ReactDOM.unmountComponentAtNode(node)`
end