Class: StimulusPlumbers::Form::FieldComponent

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_plumbers/form/field_component.rb

Constant Summary collapse

OPTIONS =
%i[label details error required label_visibility layout].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, attribute:, input_id:, label: nil, details: nil, error: nil, required: false, label_visibility: :visible, layout: :stacked) ⇒ FieldComponent

Returns a new instance of FieldComponent.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stimulus_plumbers/form/field_component.rb', line 17

def initialize(object:,
               attribute:,
               input_id:,
               label: nil,
               details: nil,
               error: nil,
               required: false,
               label_visibility: :visible,
               layout: :stacked)
  @object           = object
  @attribute        = attribute
  @input_id         = input_id
  @label_text       = label || attribute.to_s.humanize
  @details          = details
  @error_override   = error
  @required         = required
  @label_visibility = label_visibility.to_sym
  @layout           = layout.to_sym
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def attribute
  @attribute
end

#detailsObject (readonly)

Returns the value of attribute details.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def details
  @details
end

#input_idObject (readonly)

Returns the value of attribute input_id.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def input_id
  @input_id
end

#label_textObject (readonly)

Returns the value of attribute label_text.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def label_text
  @label_text
end

#label_visibilityObject (readonly)

Returns the value of attribute label_visibility.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def label_visibility
  @label_visibility
end

#layoutObject (readonly)

Returns the value of attribute layout.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def layout
  @layout
end

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def object
  @object
end

#requiredObject (readonly)

Returns the value of attribute required.



8
9
10
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8

def required
  @required
end

Instance Method Details

#described_byObject



68
69
70
71
72
73
# File 'lib/stimulus_plumbers/form/field_component.rb', line 68

def described_by
  ids = []
  ids << hint_id  if details.present?
  ids << error_id if errors.any?
  ids.join(" ").presence
end

#error?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/stimulus_plumbers/form/field_component.rb', line 47

def error?
  errors.any?
end

#error_idObject



64
65
66
# File 'lib/stimulus_plumbers/form/field_component.rb', line 64

def error_id
  "#{input_id}_error"
end

#errorsObject



37
38
39
40
41
42
43
44
45
# File 'lib/stimulus_plumbers/form/field_component.rb', line 37

def errors
  if @error_override
    Array(@error_override)
  elsif object.respond_to?(:errors)
    object.errors[@attribute]
  else
    []
  end
end

#hint_idObject



60
61
62
# File 'lib/stimulus_plumbers/form/field_component.rb', line 60

def hint_id
  "#{input_id}_hint"
end

#html_optsObject



51
52
53
54
55
56
57
58
# File 'lib/stimulus_plumbers/form/field_component.rb', line 51

def html_opts
  attrs = { id: input_id }
  attrs[:"aria-describedby"] = described_by if described_by
  attrs[:"aria-invalid"]     = "true"       if error?
  attrs[:required]           = true         if required
  attrs[:"aria-required"]    = "true"       if required
  attrs
end

#label_hidden?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/stimulus_plumbers/form/field_component.rb', line 75

def label_hidden?
  label_visibility == :exclusive
end