class Hyalite::InputWrapper

Public Class Methods

new(dom_component) click to toggle source
# File lib/hyalite/input_wrapper.rb, line 9
def initialize(dom_component)
  @dom_component = dom_component
end

Public Instance Methods

force_update_if_mounted(instance) click to toggle source
# 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) click to toggle source
# 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_wrapper() click to toggle source
# 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) click to toggle source
# 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_wrapper() click to toggle source
# File lib/hyalite/input_wrapper.rb, line 23
def unmount_wrapper
end
update_wrapper() click to toggle source
# 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