Class: Charming::Components::List
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::List
- Includes:
- KeyboardHandler
- Defined in:
- lib/charming/components/list.rb
Constant Summary collapse
- KEY_ACTIONS =
Maps navigation key symbols to instance methods consumed by the KeyboardHandler mixin: :up moves selection up, :down moves down, :home jumps to first item, :end jumps to last. See Viewport#KEY_ACTIONS and Table#KEY_ACTIONS for identical pattern.
{ up: :move_up, down: :move_down, home: :move_home, end: :move_end }.freeze
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#selected_index ⇒ Object
readonly
Returns the value of attribute selected_index.
Instance Method Summary collapse
- #handle_key(event) ⇒ Object
- #handle_mouse(event) ⇒ Object
-
#initialize(items:, selected_index: 0, height: nil, label: nil, theme: nil) ⇒ List
constructor
A new instance of List.
- #render ⇒ Object
- #selected_item ⇒ Object
Methods inherited from View
Constructor Details
#initialize(items:, selected_index: 0, height: nil, label: nil, theme: nil) ⇒ List
Returns a new instance of List.
20 21 22 23 24 25 26 27 |
# File 'lib/charming/components/list.rb', line 20 def initialize(items:, selected_index: 0, height: nil, label: nil, theme: nil) super(theme: theme) @items = items @selected_index = selected_index @height = height @label = label || :to_s.to_proc clamp_position end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
18 19 20 |
# File 'lib/charming/components/list.rb', line 18 def items @items end |
#selected_index ⇒ Object (readonly)
Returns the value of attribute selected_index.
18 19 20 |
# File 'lib/charming/components/list.rb', line 18 def selected_index @selected_index end |
Instance Method Details
#handle_key(event) ⇒ Object
29 30 31 32 33 |
# File 'lib/charming/components/list.rb', line 29 def handle_key(event) return [:selected, selected_item] if Charming.key_of(event) == :enter && selected_item super end |
#handle_mouse(event) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/charming/components/list.rb', line 35 def handle_mouse(event) return nil unless @height return nil unless event.respond_to?(:click?) && event.click? clicked = event.y return nil if clicked.negative? || clicked >= visible_items.length @selected_index = + clicked clamp_position :handled end |
#render ⇒ Object
51 52 53 54 55 |
# File 'lib/charming/components/list.rb', line 51 def render visible_items.each_with_index.map do |item, index| render_item(item, + index) end.join("\n") end |
#selected_item ⇒ Object
47 48 49 |
# File 'lib/charming/components/list.rb', line 47 def selected_item items[selected_index] end |