Class: Charming::Components::TextInput
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::TextInput
- 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
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #handle_key(event) ⇒ Object
-
#initialize(value: "", placeholder: "", width: nil, cursor: nil) ⇒ TextInput
constructor
A new instance of TextInput.
- #render ⇒ Object
Methods inherited from View
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
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
20 21 22 |
# File 'lib/charming/components/text_input.rb', line 20 def cursor @cursor end |
#value ⇒ Object (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 |
#render ⇒ Object
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 |