Class: Hyalite::InputWrapper

Inherits:
Object
  • Object
show all
Includes:
LinkedValueUtils
Defined in:
lib/hyalite/input_wrapper.rb

Instance Method Summary collapse

Methods included from LinkedValueUtils

checked, execute_on_change, value

Constructor Details

#initialize(dom_component) ⇒ InputWrapper

Returns a new instance of InputWrapper.



9
10
11
# File 'lib/hyalite/input_wrapper.rb', line 9

def initialize(dom_component)
  @dom_component = dom_component
end

Instance Method Details

#force_update_if_mounted(instance) ⇒ Object



43
44
45
46
47
# File 'lib/hyalite/input_wrapper.rb', line 43

def force_update_if_mounted(instance)
  if instance.root_node_id
    update_wrapper
  end
end

#handle_change(event) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hyalite/input_wrapper.rb', line 71

def handle_change(event)
  props = @dom_component.current_element.props
  return_value = execute_on_change(props, event)

  Hyalite.updates.asap { force_update_if_mounted(@dom_component) }

  if props[:type] == 'radio' && props[:name]
    root_node = Mount.node(root_node_id)
    query_root = root_node

    while query_root.parent
      query_root = query_root.parent
    end

    group = query_root =~ "input[name='#{name.to_json}'][type='radio']"

    group.each do |other_node|
      next if other_node == root_node || other_node.form != root_node.form

      other_id = Mount.node_id(other_node)
      other_instance = Mount.instances_by_root_id(other_id)
      Hyalite.updates.asap { force_update_if_mounted(other_instance) }
    end
  end

  return_value
end

#mount_wrapperObject



13
14
15
16
17
18
19
20
21
# File 'lib/hyalite/input_wrapper.rb', line 13

def mount_wrapper
  props = @dom_component.current_element.props
  @wrapper_state = {
    initialChecked: props[:default_checked] || false,
    initialValue: props[:default_value],
    listeners: nil,
    onChange: -> (event) { handle_change(event) }
  }
end

#native_props(inst) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hyalite/input_wrapper.rb', line 26

def native_props(inst)
  props = @dom_component.current_element.props

  if props[:checked] || @wrapper_state[:initialChecked]
    props = props.merge({
      checked: props[:checked] || @wrapper_state[:initialChecked],
    })
  end

  props.merge({
    defaultChecked: nil,
    defaultValue: nil,
    value: props[:value] || @wrapper_state[:initialValue],
    onChange: @wrapper_state[:onChange],
  })
end

#unmount_wrapperObject



23
24
# File 'lib/hyalite/input_wrapper.rb', line 23

def unmount_wrapper
end

#update_wrapperObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hyalite/input_wrapper.rb', line 49

def update_wrapper
  props = @dom_component.current_element.props
  if props.has_key?(:checked)
    checked = props[:checked]
    node = Mount.node(@dom_component.root_node_id)
    if checked
      node.attributes[:checked] = checked
    else
      node.remove_attribute(:checked)
    end
  end

  value = LinkedValueUtils.value(props)
  if value
    DOMOperations.update_property_by_id(
      @dom_component.root_node_id,
      'value',
      value.to_s
    )
  end
end