Class: Arara::TextFieldComponent

Inherits:
ActionView::Component::Base
  • Object
show all
Includes:
BaseComponent
Defined in:
app/components/arara/text_field_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseComponent

#default_html_tag, #html_class, #html_content, #html_data, #html_tag, included

Constructor Details

#initialize(variant: "default", label: nil, helper_text: nil, max_length: nil, textarea: false, id:, name: nil, value: nil, full_width: false, has_error: false, type: "text", placeholder: nil, **kw) ⇒ TextFieldComponent

Returns a new instance of TextFieldComponent.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/components/arara/text_field_component.rb', line 9

def initialize(variant: "default", label: nil, helper_text: nil, max_length: nil, textarea: false, id:, name: nil, value: nil, full_width: false, has_error: false, type: "text", placeholder: nil, **kw)
  super(tag: "div", variant: variant, **kw)

  @label = label
  @helper_text = helper_text
  @max_length = max_length
  @textarea = textarea
  @id = id
  @name = name
  @value = value
  @full_width = full_width
  @has_error = has_error
  @user_variant = "outlined" if textarea
  @type = type
  @placeholder = placeholder
end

Instance Attribute Details

#full_widthObject (readonly)

Returns the value of attribute full_width.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def full_width
  @full_width
end

#helper_textObject (readonly)

Returns the value of attribute helper_text.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def helper_text
  @helper_text
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def name
  @name
end

#placeholderObject (readonly)

Returns the value of attribute placeholder.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def placeholder
  @placeholder
end

#textareaObject (readonly)

Returns the value of attribute textarea.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def textarea
  @textarea
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'app/components/arara/text_field_component.rb', line 5

def value
  @value
end

Instance Method Details

#data_controllerObject



90
91
92
# File 'app/components/arara/text_field_component.rb', line 90

def data_controller
  "mdc-text-field"
end

#default_data_controllerObject



85
86
87
88
# File 'app/components/arara/text_field_component.rb', line 85

def default_data_controller
  "mdc-text-field"
  nil
end

#default_html_classObject



30
31
32
33
34
35
36
37
# File 'app/components/arara/text_field_component.rb', line 30

def default_html_class
  classes = ["mdc-text-field"]
  classes << "mdc-text-field--fullwidth" if full_width
  classes << "mdc-text-field--textarea" if is_textarea?
  classes << "mdc-text-field--error" if has_error?
  classes << "mdc-text-field--outlined" if user_variant == "outlined"
  classes.join(" ")
end

#has_error?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/components/arara/text_field_component.rb', line 26

def has_error?
  @has_error
end

#helper_line_classObject



74
75
76
77
78
# File 'app/components/arara/text_field_component.rb', line 74

def helper_line_class
  classes = ["mdc-text-field-helper-line"]
  classes << "mdc-text-field-helper-line--error" if has_error?
  classes.join(" ")
end

#helper_text_classObject



80
81
82
83
# File 'app/components/arara/text_field_component.rb', line 80

def helper_text_class
  classes = ["mdc-text-field-helper-text mdc-text-field-helper-text--persistent mdc-text-field-helper-text--validation-msg"]
  classes.join(" ")
end

#html_input_optionsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/components/arara/text_field_component.rb', line 53

def html_input_options
  opts = {
    class: "mdc-text-field__input",
    id: id,
    type: type,
    aria: {
      labelledby: "#{id}-label",
      controls: "#{id}-helper",
      describedby: "#{id}-helper"
    }
  }
  opts.merge!(name: name) if name
  opts.merge!(value: value) if value
  opts[:data] = opts.has_key?(:data) ? opts[:data] : {}
  opts[:data][:action] = "focusin->#{data_controller}#handleFocus"
  opts.merge!(placeholder: placeholder) if placeholder
  # opts.merge!(rows: 4, cols: 40) if textarea
  opts.merge!(maxlength: max_length) if max_length
  opts
end

#html_input_tagObject



48
49
50
51
# File 'app/components/arara/text_field_component.rb', line 48

def html_input_tag
  return "textarea" if textarea
  "input"
end

#html_optionsObject



39
40
41
42
43
44
45
46
# File 'app/components/arara/text_field_component.rb', line 39

def html_options
  opts = super
  opts.deep_symbolize_keys!
  opts[:data] = opts.has_key?(:data) ? opts[:data] : {}
  opts[:data][:target] = opts[:data].has_key?(:target) ? opts[:data][:target] : ""
  opts[:data][:target].concat(" #{data_controller}.element")
  opts
end

#is_outlined?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/components/arara/text_field_component.rb', line 94

def is_outlined?
  user_variant == "outlined" && is_textarea?
end

#is_textarea?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/components/arara/text_field_component.rb', line 98

def is_textarea?
  textarea
end