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

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

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

Returns a new instance of Select.



8
9
10
11
12
13
# File 'lib/charming/presentation/components/form/select.rb', line 8

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



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

def bind(state)
  super
  ensure_selection
end

#handle_key(event) ⇒ Object



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

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

  save_selection(selection.selected_index)
  :handled
end