Class: Charming::Presentation::Components::TextInput

Inherits:
Charming::Presentation::Component show all
Includes:
KeyboardHandler
Defined in:
lib/charming/presentation/components/text_input.rb

Constant Summary collapse

KEY_ACTIONS =

Maps editing keys (left/right/home/end/backspace/delete) to the instance methods they dispatch via KeyboardHandler. Each symbol key (e.g., :left) maps to a method (e.g., :move_left) that adjusts cursor position or text content.

{
  left: :move_left,
  right: :move_right,
  home: :move_home,
  end: :move_end,
  backspace: :delete_before_cursor,
  delete: :delete_at_cursor
}.freeze

Constants included from KeyboardHandler

KeyboardHandler::VIM_KEYMAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(value: "", placeholder: "", width: nil, cursor: nil) ⇒ TextInput

Returns a new instance of TextInput.



23
24
25
26
27
28
29
30
# File 'lib/charming/presentation/components/text_input.rb', line 23

def initialize(value: "", placeholder: "", width: nil, cursor: nil)
  super()
  @value = value.dup
  @placeholder = placeholder
  @width = width
  @cursor = cursor || @value.length
  clamp_position
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



21
22
23
# File 'lib/charming/presentation/components/text_input.rb', line 21

def cursor
  @cursor
end

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/charming/presentation/components/text_input.rb', line 21

def value
  @value
end

Instance Method Details

#handle_key(event) ⇒ Object



32
33
34
35
36
# File 'lib/charming/presentation/components/text_input.rb', line 32

def handle_key(event)
  return :handled if character_event?(event) && insert(event.char)

  super
end

#renderObject



38
39
40
41
# File 'lib/charming/presentation/components/text_input.rb', line 38

def render
  rendered = render_value
  @width ? style.width(@width).render(rendered) : rendered
end