Class: Charming::Presentation::Components::Form::Confirm
- Inherits:
-
Field
- Object
- View
- Charming::Presentation::Component
- Field
- Charming::Presentation::Components::Form::Confirm
- Defined in:
- lib/charming/presentation/components/form/confirm.rb
Overview
Confirm is a boolean Form field that renders a checkbox-style control. Space toggles the value; y/Right sets it to true; n/Left sets it to false. Required confirms must be accepted (value == true) to pass validation.
Instance Attribute Summary
Attributes inherited from Field
Instance Method Summary collapse
-
#handle_key(event) ⇒ Object
Handles the standard confirm keys: space toggles, y/right sets to true, n/left sets to false, and a space character (when the event exposes ‘char`) also toggles.
-
#initialize(name, value: false, **options) ⇒ Confirm
constructor
value is the initial boolean state (default: false).
-
#validate ⇒ Object
Returns [“must be accepted”] when required and the value is not true, otherwise the result of the base Field validation.
Methods inherited from Field
#bind, #focusable?, #render, #value
Methods inherited from View
#focused?, #layout_assigns, #render
Constructor Details
#initialize(name, value: false, **options) ⇒ Confirm
value is the initial boolean state (default: false). All other options are forwarded to Field.
13 14 15 16 |
# File 'lib/charming/presentation/components/form/confirm.rb', line 13 def initialize(name, value: false, **) super(name, **) @initial_value = value end |
Instance Method Details
#handle_key(event) ⇒ Object
Handles the standard confirm keys: space toggles, y/right sets to true, n/left sets to false, and a space character (when the event exposes ‘char`) also toggles.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/charming/presentation/components/form/confirm.rb', line 20 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 |
#validate ⇒ Object
Returns [“must be accepted”] when required and the value is not true, otherwise the result of the base Field validation.
38 39 40 41 42 |
# File 'lib/charming/presentation/components/form/confirm.rb', line 38 def validate return ["must be accepted"] if required? && value != true super end |