Class: Charming::Presentation::Components::Form
- Inherits:
-
Charming::Presentation::Component
- Object
- View
- Charming::Presentation::Component
- Charming::Presentation::Components::Form
- Defined in:
- lib/charming/presentation/components/form.rb,
lib/charming/presentation/components/form/note.rb,
lib/charming/presentation/components/form/field.rb,
lib/charming/presentation/components/form/input.rb,
lib/charming/presentation/components/form/select.rb,
lib/charming/presentation/components/form/builder.rb,
lib/charming/presentation/components/form/confirm.rb,
lib/charming/presentation/components/form/textarea.rb
Defined Under Namespace
Classes: Builder, Confirm, Field, Input, Note, Select, Textarea
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #handle_key(event) ⇒ Object
-
#initialize(fields:, state: nil, theme: nil) ⇒ Form
constructor
A new instance of Form.
- #render ⇒ Object
- #values ⇒ Object
Methods inherited from View
Constructor Details
#initialize(fields:, state: nil, theme: nil) ⇒ Form
Returns a new instance of Form.
9 10 11 12 13 14 15 |
# File 'lib/charming/presentation/components/form.rb', line 9 def initialize(fields:, state: nil, theme: nil) super(theme: theme) @fields = fields @state = normalize_state(state || {}) bind_fields clamp_focus end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/charming/presentation/components/form.rb', line 7 def fields @fields end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/charming/presentation/components/form.rb', line 7 def state @state end |
Instance Method Details
#handle_key(event) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/charming/presentation/components/form.rb', line 17 def handle_key(event) key = Charming.key_of(event) return :cancelled if key == :escape return submit if submit_shortcut?(event) return move_focus(tab_direction(event)) if key == :tab result = handle_current_field(event) return result if result advance_or_submit if key == :enter end |
#render ⇒ Object
33 34 35 36 37 |
# File 'lib/charming/presentation/components/form.rb', line 33 def render fields.each_with_index.map do |field, index| field.render(active: index == state[:focus_index]) end.join("\n") end |
#values ⇒ Object
29 30 31 |
# File 'lib/charming/presentation/components/form.rb', line 29 def values state[:values] end |