Class: ViewComponent::InputComponent::TextareaComponent

Inherits:
Object
  • Object
show all
Includes:
ComponentHelper
Defined in:
app/helpers/view_component/input_component/textarea_component.rb

Constant Summary collapse

TEXTAREA_SIZES =
%w[md lg].freeze
TEXTAREA_SIZE_STYLE =
{
  md: 'textarea-component-md main-text-md-normal',
  lg: 'textarea-component-lg main-text-lg-medium'
}.freeze
LABEL_STYLES =
{
  md: 'general-text-sm-normal',
  lg: 'general-text-md-normal'
}.freeze
SUPPORT_TEXT_STYLES =
{
  md: 'general-text-sm-normal px-3',
  lg: 'general-text-md-normal px-3 md:px-4'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComponentHelper

#class_list, #resolve_error

Constructor Details

#initialize(form:, name:, label:, placeholder:, value:, rows:, size:, support_text:, error:, disabled:, html_options:) ⇒ TextareaComponent

Returns a new instance of TextareaComponent.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 28

def initialize(form:, name:, label:, placeholder:, value:, rows:, size:, support_text:,
               error:, disabled:, html_options:)
  raise "Incorrect textarea size: #{size}" unless TEXTAREA_SIZES.include?(size)

  error_message = resolve_error(form, name, error)

  self.form = form
  self.name = name
  self.label = label
  self.placeholder = placeholder
  self.value = value
  self.rows = rows
  self.size = size
  self.support_text = (error_message.presence || support_text)
  self.error = error_message
  self.disabled = disabled
  self.html_options = html_options

  self.html_options[:placeholder] = placeholder
  self.html_options[:rows] ||= rows
  self.html_options[:disabled] = true if disabled
  self.html_options[:class] = textarea_style
end

Instance Attribute Details

#disabledObject

Returns the value of attribute disabled.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def disabled
  @disabled
end

#errorObject

Returns the value of attribute error.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def error
  @error
end

#formObject

Returns the value of attribute form.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def form
  @form
end

#html_optionsObject

Returns the value of attribute html_options.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def html_options
  @html_options
end

#labelObject

Returns the value of attribute label.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def label
  @label
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def name
  @name
end

#placeholderObject

Returns the value of attribute placeholder.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def placeholder
  @placeholder
end

#rowsObject

Returns the value of attribute rows.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def rows
  @rows
end

#sizeObject

Returns the value of attribute size.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def size
  @size
end

#support_textObject

Returns the value of attribute support_text.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def support_text
  @support_text
end

#valueObject

Returns the value of attribute value.



25
26
27
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 25

def value
  @value
end

Instance Method Details

#label_styleObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 68

def label_style
  base = ['textarea-label']
  size_style = LABEL_STYLES[size.to_sym]

  color_style =
    if disabled
      'text-disabled-color'
    elsif error.present?
      'text-danger-dark'
    else
      'text-letter-color-light group-focus-within:text-primary'
    end

  class_list(base, size_style, color_style)
end

#support_text_styleObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 84

def support_text_style
  base = ['textarea-support-text']
  size_style = SUPPORT_TEXT_STYLES[size.to_sym]

  color_style =
    if disabled
      'text-disabled-color'
    elsif error.present?
      'text-danger-dark'
    else
      'text-slate-grey-50'
    end

  class_list(base, size_style, color_style)
end

#textarea_styleObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/view_component/input_component/textarea_component.rb', line 52

def textarea_style
  base = ['textarea-component-base field-sizing-content']
  size_style = TEXTAREA_SIZE_STYLE[size.to_sym]

  color_style =
    if disabled
      'textarea-disabled-state'
    elsif error.present?
      'textarea-error-state'
    else
      'textarea-active-state'
    end

  class_list(base, size_style, color_style)
end