Method: React::API.orig_convert_props

Defined in:
lib/react/ref_callback.rb

.orig_convert_propsObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/react/ref_callback.rb', line 9

def self.convert_props(args)
  # merge args together into a single properties hash
  properties = {}
  args.each do |arg|
    if arg.is_a? String
      properties[arg] = true
    elsif arg.is_a? Hash
      arg.each do |key, value|
        if ['class', 'className', 'class_name'].include? key
          next unless value

          if value.is_a?(String)
            value = value.split(' ')
          elsif !value.is_a?(Array)
            raise "The class param must be a string or array of strings"
          end

          properties['className'] = [*properties['className'], *value]
        elsif key == 'style'
          next unless value

          if !value.is_a?(Hash)
            raise "The style param must be a Hash"
          end

          properties['style'] = (properties['style'] || {}).merge(value)
        elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
          properties[key] = (properties[key] || {}).merge(value)
        else
          properties[key] = value
        end
      end
    end
  end
  # process properties according to react rules
  props = {}
  properties.each do |key, value|
    if ["style", "dangerously_set_inner_HTML"].include? key
      props[lower_camelize(key)] = value.to_n

    elsif key == "className"
      props[key] = value.join(' ')

    elsif key == "key"
      props["key"] = value.to_key

    elsif key == 'ref' && value.respond_to?(:call)
      # currently react still accepts the syntax ref: :foo meaning set refs.foo to the ref.
      # in hyperstack release 0.1 we can put this behavior on a switch or use the notation `ref_key: :foo` for old school
      props[key] = %x{
                      function(dom_node){
                        if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {
                          #{ value.call(`dom_node.__opalInstance`) };
                        } else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) {
                          #{ value.call(`ReactDOM.findDOMNode(dom_node)`) };
                        } else {
                          #{ value.call(`dom_node`) };
                        }
                      }
                    }
    elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
      value.each { |k, v| props["#{key}-#{k.gsub(/__|_/, '__' => '_', '_' => '-')}"] = v.to_n }
    else
      props[React.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value
    end
  end
  props
end