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

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

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 View

#focused?, #layout_assigns

Constructor Details

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

Returns a new instance of Textarea.



8
9
10
11
12
13
14
# File 'lib/charming/presentation/components/form/textarea.rb', line 8

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



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

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



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/charming/presentation/components/form/textarea.rb', line 23

def handle_key(event)
  area = text_area
  result = area.handle_key(event)
  return nil unless result == :handled

  state[:values][name] = area.value
  field_state[:cursor] = area.cursor
  field_state[:offset] = area.offset
  field_state[:preferred_column] = area.preferred_column
  :handled
end

#render(active: false) ⇒ Object



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

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