Class: Charming::Presentation::Components::Form::Input

Inherits:
Field show all
Defined in:
lib/charming/presentation/components/form/input.rb

Instance Attribute Summary

Attributes inherited from Field

#help, #label, #name, #state

Instance Method Summary collapse

Methods inherited from Field

#focusable?, #render, #validate, #value

Methods inherited from View

#focused?, #layout_assigns, #render

Constructor Details

#initialize(name, value: "", placeholder: "", width: nil, **options) ⇒ Input

Returns a new instance of Input.



8
9
10
11
12
13
# File 'lib/charming/presentation/components/form/input.rb', line 8

def initialize(name, value: "", placeholder: "", width: nil, **options)
  super(name, **options)
  @initial_value = value
  @placeholder = placeholder
  @width = width
end

Instance Method Details

#bind(state) ⇒ Object



15
16
17
18
19
# File 'lib/charming/presentation/components/form/input.rb', line 15

def bind(state)
  super
  state[:values][name] = @initial_value if state[:values][name].nil?
  field_state[:cursor] = state[:values][name].to_s.length unless field_state.key?(:cursor)
end

#handle_key(event) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/charming/presentation/components/form/input.rb', line 21

def handle_key(event)
  text_input = input
  result = text_input.handle_key(event)
  return nil unless result == :handled

  state[:values][name] = text_input.value
  field_state[:cursor] = text_input.cursor
  :handled
end