Class: Potty::Widgets::InputItem

Inherits:
ListItem
  • Object
show all
Defined in:
lib/potty/widgets/list_item.rb

Overview

Text input item — inline text editing within a List. Shares the LineEditor model with the TextInput widget.

Instance Attribute Summary

Attributes inherited from ListItem

#color, #text, #value

Instance Method Summary collapse

Methods inherited from ListItem

#activate, #disabled?, #render_custom

Constructor Details

#initialize(label, default: "", &on_submit) ⇒ InputItem

Returns a new instance of InputItem.



66
67
68
69
70
# File 'lib/potty/widgets/list_item.rb', line 66

def initialize(label, default: "", &on_submit)
  super(label)
  @editor = LineEditor.new(default)
  @on_submit = on_submit
end

Instance Method Details

#display_textObject



81
82
83
# File 'lib/potty/widgets/list_item.rb', line 81

def display_text
  "#{@text}: #{@editor.text}_"
end

#handle_key(ch) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/potty/widgets/list_item.rb', line 85

def handle_key(ch)
  case ch
  when *Keys::ENTERS
    @on_submit&.call(@editor.text)
  when *Keys::BACKSPACES
    @editor.backspace
  when Keys::LEFT
    @editor.left
  when Keys::RIGHT
    @editor.right
  when Keys::SPACE..(Keys::DEL_ASCII - 1) # Printable ASCII
    @editor.insert(ch.chr)
  else
    return false
  end
  true
end

#input_valueObject

Back-compat accessor for the entered text.



73
74
75
# File 'lib/potty/widgets/list_item.rb', line 73

def input_value
  @editor.text
end

#input_value=(value) ⇒ Object



77
78
79
# File 'lib/potty/widgets/list_item.rb', line 77

def input_value=(value)
  @editor.text = value
end