Class: Charming::Components::TextInput

Inherits:
Charming::Component show all
Includes:
KeyboardHandler
Defined in:
lib/charming/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

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.



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

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.



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

def cursor
  @cursor
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#handle_key(event) ⇒ Object



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

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

  super
end

#renderObject



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

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