Class: Charming::Presentation::Components::Form::Confirm

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

Instance Attribute Summary

Attributes inherited from Field

#help, #label, #name, #state

Instance Method Summary collapse

Methods inherited from Field

#bind, #focusable?, #render, #value

Methods inherited from View

#focused?, #layout_assigns, #render

Constructor Details

#initialize(name, value: false, **options) ⇒ Confirm

Returns a new instance of Confirm.



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

def initialize(name, value: false, **options)
  super(name, **options)
  @initial_value = value
end

Instance Method Details

#handle_key(event) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/charming/presentation/components/form/confirm.rb', line 13

def handle_key(event)
  case Charming.key_of(event)
  when :space
    toggle
  when :y, :right
    state[:values][name] = true
  when :n, :left
    state[:values][name] = false
  else
    return nil unless event.respond_to?(:char) && event.char == " "

    toggle
  end
  :handled
end

#validateObject



29
30
31
32
33
# File 'lib/charming/presentation/components/form/confirm.rb', line 29

def validate
  return ["must be accepted"] if required? && value != true

  super
end