Class: Charming::Components::Form::Textarea

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

Overview

Textarea is a multi-line Form field backed by a TextArea widget. The cursor offset, top-visible row, and preferred vertical column are all persisted in the form's per-field state so the field behaves consistently when refocused mid-edit.

Instance Attribute Summary

Attributes inherited from Field

#help, #label, #name, #state

Instance Method Summary collapse

Methods inherited from Field

#focusable?, #validate, #value

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(name, value: "", placeholder: "", width: nil, height: nil, **options) ⇒ Textarea

value is the initial text. placeholder is shown when the value is empty. width and height constrain the rendered area. All other options are forwarded to Field (label, required, validate, help, theme).



13
14
15
16
17
18
19
# File 'lib/charming/presentation/components/form/textarea.rb', line 13

def initialize(name, value: "", placeholder: "", width: nil, height: nil, **options)
  super(name, **options)
  @initial_value = value
  @placeholder = placeholder
  @width = width
  @height = height
end

Instance Method Details

#bind(state) ⇒ Object

Binds the field, seeds the initial value, and initializes the cursor/offset state.



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

def bind(state)
  super
  state[:values][name] = @initial_value if state[:values][name].nil?
  field_state[:cursor] = state[:values][name].to_s.length unless field_state.key?(:cursor)
  field_state[:offset] ||= 0
end

#handle_key(event) ⇒ Object

Forwards key events to the underlying TextArea, syncing the value, cursor, offset, and preferred column back into the form state. Returns :handled when consumed.



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

def handle_key(event)
  forward_to_text_area(:handle_key, event)
end

#handle_paste(event) ⇒ Object

Forwards pasted text (newlines preserved) to the underlying TextArea the same way.



36
37
38
# File 'lib/charming/presentation/components/form/textarea.rb', line 36

def handle_paste(event)
  forward_to_text_area(:handle_paste, event)
end

#render(active: false) ⇒ Object

Renders the field with its label on the first line, body lines indented, and optional help/error lines below.



42
43
44
45
46
# File 'lib/charming/presentation/components/form/textarea.rb', line 42

def render(active: false)
  label_line = "#{active ? ">" : " "} #{label}:"
  label_line = theme.selected.render(label_line) if active
  [label_line, *body_lines, help_line, *error_lines].compact.join("\n")
end