Class: Charming::Presentation::Components::Form::Select

Inherits:
Field show all
Defined in:
lib/charming/presentation/components/form/select.rb

Overview

Select is a single-choice Form field backed by a List widget. The selected option becomes the field’s value; navigation keys (up/down/home/end) cycle through options and Enter has no effect (selection is applied immediately on key release).

Instance Attribute Summary

Attributes inherited from Field

#help, #label, #name, #state

Instance Method Summary collapse

Methods inherited from Field

#focusable?, #render, #validate, #value

Methods inherited from View

#focused?, #layout_assigns, #render

Constructor Details

#initialize(name, options:, selected_index: 0, option_label: :to_s.to_proc, **field_options) ⇒ Select

options is the array of selectable values. selected_index defaults to 0. option_label is a callable used to extract the display string (default: ‘to_s`). All other options are forwarded to Field.



14
15
16
17
18
19
# File 'lib/charming/presentation/components/form/select.rb', line 14

def initialize(name, options:, selected_index: 0, option_label: :to_s.to_proc, **field_options)
  super(name, **field_options)
  @options = options
  @initial_selected_index = selected_index
  @option_label = option_label
end

Instance Method Details

#bind(state) ⇒ Object

Binds the field, then ensures the persisted selection (or initial/derived one) is applied.



22
23
24
25
# File 'lib/charming/presentation/components/form/select.rb', line 22

def bind(state)
  super
  ensure_selection
end

#handle_key(event) ⇒ Object

Forwards key events to the underlying List, syncing the chosen option index back into the field state. Returns :handled when consumed.



29
30
31
32
33
34
35
36
# File 'lib/charming/presentation/components/form/select.rb', line 29

def handle_key(event)
  selection = list
  result = selection.handle_key(event)
  return nil unless result == :handled

  save_selection(selection.selected_index)
  :handled
end