Class: Spina::Forms::TextFieldComponent

Inherits:
ApplicationComponent show all
Defined in:
app/components/spina/forms/text_field_component.rb

Direct Known Subclasses

PasswordFieldComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f, method, size: "md", autofocus: false, placeholder: nil) ⇒ TextFieldComponent

Returns a new instance of TextFieldComponent.



6
7
8
9
10
11
12
# File 'app/components/spina/forms/text_field_component.rb', line 6

def initialize(f, method, size: "md", autofocus: false, placeholder: nil)
  @f = f
  @method = method
  @size = size
  @autofocus = autofocus
  @placeholder = placeholder
end

Instance Attribute Details

#autofocusObject

Returns the value of attribute autofocus.



4
5
6
# File 'app/components/spina/forms/text_field_component.rb', line 4

def autofocus
  @autofocus
end

#fObject

Returns the value of attribute f.



4
5
6
# File 'app/components/spina/forms/text_field_component.rb', line 4

def f
  @f
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'app/components/spina/forms/text_field_component.rb', line 4

def method
  @method
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'app/components/spina/forms/text_field_component.rb', line 4

def size
  @size
end

Instance Method Details

#controllersObject



14
15
16
17
18
# File 'app/components/spina/forms/text_field_component.rb', line 14

def controllers
  contr = []
  contr << "autofocus" if autofocus
  contr.join(" ")
end

#error_messagesObject



37
38
39
# File 'app/components/spina/forms/text_field_component.rb', line 37

def error_messages
  f.object.errors[method.to_sym]
end

#error_stylesObject



29
30
31
32
33
34
35
# File 'app/components/spina/forms/text_field_component.rb', line 29

def error_styles
  if has_errors?
    "border-red-500 ring-red-500 ring-1"
  else
    ""
  end
end

#has_errors?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/components/spina/forms/text_field_component.rb', line 41

def has_errors?
  error_messages.present?
end

#placeholderObject



45
46
47
# File 'app/components/spina/forms/text_field_component.rb', line 45

def placeholder
  @placeholder || f.object.class.human_attribute_name(method)
end

#size_stylesObject



20
21
22
23
24
25
26
27
# File 'app/components/spina/forms/text_field_component.rb', line 20

def size_styles
  case size
  when "lg"
    "px-4 py-3"
  else
    "text-sm"
  end
end