Class: Charming::Components::MultiSelectList
- Inherits:
-
List
- Object
- View
- Charming::Component
- List
- Charming::Components::MultiSelectList
- Defined in:
- lib/charming/presentation/components/multi_select_list.rb
Overview
MultiSelectList is a List variant where Space toggles per-item checkmarks and Enter submits the checked set. Renders ‘[x]` / `[ ]` prefixes.
‘handle_key` returns `[:submitted, [item, …]]` on Enter, :handled for toggles and navigation, nil otherwise. max_selections optionally caps how many items can be checked at once.
Constant Summary
Constants inherited from List
Constants included from KeyboardHandler
Instance Attribute Summary collapse
-
#selected_indices ⇒ Object
readonly
The set of selected (checked) item indices.
Attributes inherited from List
Instance Method Summary collapse
-
#handle_key(event) ⇒ Object
Space toggles the highlighted item, Enter submits the checked items.
-
#initialize(items:, selected_indices: [], max_selections: nil, **options) ⇒ MultiSelectList
constructor
Same options as List, plus selected_indices (initially checked items) and max_selections (cap on simultaneous checks; nil = unlimited).
-
#render ⇒ Object
Renders each visible item with a checkbox prefix; the highlighted row uses the selected style.
-
#selected_items ⇒ Object
The checked items, in list order.
Methods inherited from List
Methods inherited from Charming::Component
Methods inherited from View
Constructor Details
#initialize(items:, selected_indices: [], max_selections: nil, **options) ⇒ MultiSelectList
Same options as List, plus selected_indices (initially checked items) and max_selections (cap on simultaneous checks; nil = unlimited).
17 18 19 20 21 |
# File 'lib/charming/presentation/components/multi_select_list.rb', line 17 def initialize(items:, selected_indices: [], max_selections: nil, **) super(items: items, **) @selected_indices = selected_indices.to_a.map(&:to_i).uniq.select { |index| index.between?(0, items.length - 1) } @max_selections = max_selections end |
Instance Attribute Details
#selected_indices ⇒ Object (readonly)
The set of selected (checked) item indices.
13 14 15 |
# File 'lib/charming/presentation/components/multi_select_list.rb', line 13 def selected_indices @selected_indices end |
Instance Method Details
#handle_key(event) ⇒ Object
Space toggles the highlighted item, Enter submits the checked items.
24 25 26 27 28 29 30 31 32 |
# File 'lib/charming/presentation/components/multi_select_list.rb', line 24 def handle_key(event) case Charming.key_of(event) when :space then toggle_current when :enter then [:submitted, selected_items] else # Bypass List#handle_key (its Enter means single-select); use its navigation. (event) end end |
#render ⇒ Object
Renders each visible item with a checkbox prefix; the highlighted row uses the selected style.
41 42 43 44 45 |
# File 'lib/charming/presentation/components/multi_select_list.rb', line 41 def render visible_items.each_with_index.map do |item, index| render_checkbox_item(item, + index) end.join("\n") end |
#selected_items ⇒ Object
The checked items, in list order.
35 36 37 |
# File 'lib/charming/presentation/components/multi_select_list.rb', line 35 def selected_items selected_indices.sort.map { |index| items[index] } end |