Class: ViewComponent::InputComponent::InputTextComponent

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

Direct Known Subclasses

InputMobileComponent

Constant Summary collapse

INPUT_SIZE_STYLE =
{
  md: 'input-text-md',
  lg: 'input-text-lg'
}.freeze
ICON_SIZE =
{
  md: 'input-text-icon-md',
  lg: 'input-text-icon-lg'
}.freeze
INPUT_WRAPPER_STYLES =
{
  md: 'input-text-div-base-md',
  lg: 'input-text-div-base-lg'
}.freeze
LABEL_STYLES =
{
  md: 'input-text-label-md',
  lg: 'input-text-label-lg'
}.freeze
SUBTEXT_SIZE =
{
  md: 'input-text-subtext-md',
  lg: 'input-text-subtext-lg'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComponentHelper

#class_list, #resolve_error

Constructor Details

#initialize(form:, name:, label:, type:, placeholder:, value:, subtext:, error:, icon_name:, icon_position:, disabled:, size:, html_options:) ⇒ InputTextComponent

Returns a new instance of InputTextComponent.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 36

def initialize(form:, name:, label:, type:, placeholder:, value:, subtext:, error:,
               icon_name:, icon_position:, disabled:, size:, html_options:)
  error_message = resolve_error(form, name, error)

  self.form = form
  self.name = name
  self.label = label
  self.type = type
  self.value = value
  self.subtext = error_message.presence || subtext # Prioritise error over subtext
  self.error = error_message
  self.icon_name = icon_name
  self.icon_position = icon_position
  self.disabled = disabled
  self.size = size
  self.html_options = html_options
  self.html_options[:placeholder] = placeholder
  self.html_options[:class] = input_style
  self.html_options[:disabled] = disabled if disabled
end

Instance Attribute Details

#disabledObject

Returns the value of attribute disabled.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def disabled
  @disabled
end

#errorObject

Returns the value of attribute error.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def error
  @error
end

#formObject

Returns the value of attribute form.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def form
  @form
end

#html_optionsObject

Returns the value of attribute html_options.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def html_options
  @html_options
end

#icon_nameObject

Returns the value of attribute icon_name.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def icon_name
  @icon_name
end

#icon_positionObject

Returns the value of attribute icon_position.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def icon_position
  @icon_position
end

#labelObject

Returns the value of attribute label.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def label
  @label
end

#nameObject

Returns the value of attribute name.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def name
  @name
end

#sizeObject

Returns the value of attribute size.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def size
  @size
end

#subtextObject

Returns the value of attribute subtext.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def subtext
  @subtext
end

#typeObject

Returns the value of attribute type.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def type
  @type
end

#valueObject

Returns the value of attribute value.



33
34
35
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 33

def value
  @value
end

Instance Method Details

#icon_sizeObject



119
120
121
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 119

def icon_size
  ICON_SIZE[size.to_sym]
end

#icon_styleObject



107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 107

def icon_style
  base = ['input-text-icon-base']
  color_style = if disabled
                  'text-disabled-color'
                elsif error.present?
                  'text-danger-dark'
                else
                  'text-disabled-color group-focus-within:text-letter-color'
                end
  class_list(base, '', color_style)
end

#input_styleObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 57

def input_style
  base = ['input-text-base']
  color_style = if disabled
                  'text-disabled-color'
                elsif error.present?
                  'text-danger focus:text-danger-dark'
                else
                  'text-disabled-color focus:text-letter-color'
                end

  size_style = INPUT_SIZE_STYLE[size.to_sym]
  class_list(base, size_style, color_style)
end

#input_wrapper_styleObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 79

def input_wrapper_style
  base = ['input-text-div-base group']
  color_style = if disabled
                  'border-disabled-color'
                elsif error.present?
                  'border-danger focus-within:border-danger-dark ring-danger-dark'
                else
                  'border-slate-grey-50 focus-within:border-primary ring-primary'
                end

  size_style = INPUT_WRAPPER_STYLES[size.to_sym]
  class_list(base, size_style, color_style)
end

#label_styleObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 93

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

  size_style = LABEL_STYLES[size.to_sym]
  class_list(base, size_style, color_style)
end

#placeholderObject



71
72
73
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 71

def placeholder
  html_options[:placeholder]
end

#placeholder=(value) ⇒ Object



75
76
77
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 75

def placeholder=(value)
  html_options[:placeholder] = value
end

#subtext_styleObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/view_component/input_component/input_text_component.rb', line 123

def subtext_style
  base = []
  color_style = if disabled
                  'text-disabled-color'
                elsif error.present?
                  'text-danger-dark'
                else
                  'text-letter-color-light'
                end
  size_style = SUBTEXT_SIZE[size.to_sym]
  class_list(base, size_style, color_style)
end