Class: Charming::Presentation::Components::Form::Input
- Inherits:
-
Field
- Object
- View
- Charming::Presentation::Component
- Field
- Charming::Presentation::Components::Form::Input
- Defined in:
- lib/charming/presentation/components/form/input.rb
Overview
Input is a single-line Form field backed by a TextInput widget. The cursor position is persisted in the form’s per-field state so the field can be refocused mid-edit.
Instance Attribute Summary
Attributes inherited from Field
Instance Method Summary collapse
-
#bind(state) ⇒ Object
Binds the field to the form state, sets the initial value if absent, and initializes the per-field cursor offset to the end of the value.
-
#handle_key(event) ⇒ Object
Forwards key events to the underlying TextInput, syncing the value and cursor back into the form state.
-
#initialize(name, value: "", placeholder: "", width: nil, **options) ⇒ Input
constructor
value is the initial text.
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
value is the initial text. placeholder is shown when the value is empty. width optionally constrains the rendered width. All other options are forwarded to Field (label, required, validate, help, theme).
13 14 15 16 17 18 |
# File 'lib/charming/presentation/components/form/input.rb', line 13 def initialize(name, value: "", placeholder: "", width: nil, **) super(name, **) @initial_value = value @placeholder = placeholder @width = width end |
Instance Method Details
#bind(state) ⇒ Object
Binds the field to the form state, sets the initial value if absent, and initializes the per-field cursor offset to the end of the value.
22 23 24 25 26 |
# File 'lib/charming/presentation/components/form/input.rb', line 22 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
Forwards key events to the underlying TextInput, syncing the value and cursor back into the form state. Returns :handled when the event was consumed.
30 31 32 33 34 35 36 37 38 |
# File 'lib/charming/presentation/components/form/input.rb', line 30 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 |