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

Inherits:
Charming::Presentation::Component show all
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

Overview

Form is a multi-field form component with built-in focus traversal, validation, and submit/cancel handling. Fields are produced by ‘Form::Builder` (see `controller.form`) and bound to a per-form mutable state hash. Tab/Shift+Tab cycles focus through focusable fields, Enter advances to the next field (or submits on the last), Escape cancels, and Ctrl+S submits from any field.

Defined Under Namespace

Classes: Builder, Confirm, Field, Input, Note, Select, Textarea

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(fields:, state: nil, theme: nil) ⇒ Form

fields is the array of form field objects. state is a hash for storing field values/errors and the current focus index; usually ‘session[form_name]`.



17
18
19
20
21
22
23
# File 'lib/charming/presentation/components/form.rb', line 17

def initialize(fields:, state: nil, theme: nil)
  super(theme: theme)
  @fields = fields
  @state = normalize_state(state || {})
  bind_fields
  clamp_focus
end

Instance Attribute Details

#fieldsObject (readonly)

The list of field objects and the mutable state hash the form is bound to.



13
14
15
# File 'lib/charming/presentation/components/form.rb', line 13

def fields
  @fields
end

#stateObject (readonly)

The list of field objects and the mutable state hash the form is bound to.



13
14
15
# File 'lib/charming/presentation/components/form.rb', line 13

def state
  @state
end

Instance Method Details

#handle_key(event) ⇒ Object

Handles key events: Escape cancels, Ctrl+S submits, Tab cycles focus, Enter advances or submits, and unhandled keys are passed to the focused field.



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

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

#renderObject

Renders each field on its own line, marking the active field with ‘active: true`.



45
46
47
48
49
# File 'lib/charming/presentation/components/form.rb', line 45

def render
  fields.each_with_index.map do |field, index|
    field.render(active: index == state[:focus_index])
  end.join("\n")
end

#valuesObject

Returns a hash of ‘=> value` for the current field values.



40
41
42
# File 'lib/charming/presentation/components/form.rb', line 40

def values
  state[:values]
end