Class: Charming::Presentation::Components::List
- Inherits:
-
Charming::Presentation::Component
- Object
- View
- Charming::Presentation::Component
- Charming::Presentation::Components::List
- Includes:
- KeyboardHandler
- Defined in:
- lib/charming/presentation/components/list.rb
Overview
List is a vertically-scrollable selectable list. Supports keyboard navigation (up/down/home/end, Enter to activate) and mouse click selection. When a height is given, the list renders a fixed-height window over its items with auto-scroll keeping the selected item in view.
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
Constants included from KeyboardHandler
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
The item array and the currently selected index within it.
-
#selected_index ⇒ Object
readonly
The item array and the currently selected index within it.
Instance Method Summary collapse
-
#handle_key(event) ⇒ Object
Handles key events.
-
#handle_mouse(event) ⇒ Object
Handles mouse events: a click within the visible window selects the clicked row.
-
#initialize(items:, selected_index: 0, height: nil, label: nil, theme: nil, keymap: :vim) ⇒ List
constructor
items is the array of selectable objects.
-
#render ⇒ Object
Renders the visible window of items, prefixing each with “> ” (and applying the selected style) or “ ”.
-
#selected_item ⇒ Object
Returns the currently selected item, or nil when the list is empty.
Methods inherited from View
Constructor Details
#initialize(items:, selected_index: 0, height: nil, label: nil, theme: nil, keymap: :vim) ⇒ List
items is the array of selectable objects. selected_index defaults to 0. height optionally constrains the visible window; label is a callable that extracts the display string from an item (defaults to ‘to_s`). keymap selects the keybinding style (`:vim` enables h/j/k/l → left/down/up/right).
30 31 32 33 34 35 36 37 38 |
# File 'lib/charming/presentation/components/list.rb', line 30 def initialize(items:, selected_index: 0, height: nil, label: nil, theme: nil, keymap: :vim) super(theme: theme) @items = items @selected_index = selected_index @height = height @label = label || :to_s.to_proc @keymap = keymap clamp_position end |
Instance Attribute Details
#items ⇒ Object (readonly)
The item array and the currently selected index within it.
24 25 26 |
# File 'lib/charming/presentation/components/list.rb', line 24 def items @items end |
#selected_index ⇒ Object (readonly)
The item array and the currently selected index within it.
24 25 26 |
# File 'lib/charming/presentation/components/list.rb', line 24 def selected_index @selected_index end |
Instance Method Details
#handle_key(event) ⇒ Object
Handles key events. Returns ‘[:selected, item]` on Enter when an item is selected; otherwise delegates to the KeyboardHandler for navigation keys.
42 43 44 45 46 |
# File 'lib/charming/presentation/components/list.rb', line 42 def handle_key(event) return [:selected, selected_item] if Charming.key_of(event) == :enter && selected_item super end |
#handle_mouse(event) ⇒ Object
Handles mouse events: a click within the visible window selects the clicked row. Returns :handled on a successful click, nil otherwise.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/charming/presentation/components/list.rb', line 50 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
Renders the visible window of items, prefixing each with “> ” (and applying the selected style) or “ ”.
69 70 71 72 73 |
# File 'lib/charming/presentation/components/list.rb', line 69 def render visible_items.each_with_index.map do |item, index| render_item(item, + index) end.join("\n") end |
#selected_item ⇒ Object
Returns the currently selected item, or nil when the list is empty.
63 64 65 |
# File 'lib/charming/presentation/components/list.rb', line 63 def selected_item items[selected_index] end |