Class: StimulusPlumbers::Form::FieldComponent
- Inherits:
-
Object
- Object
- StimulusPlumbers::Form::FieldComponent
- 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
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#input_id ⇒ Object
readonly
Returns the value of attribute input_id.
-
#label_text ⇒ Object
readonly
Returns the value of attribute label_text.
-
#label_visibility ⇒ Object
readonly
Returns the value of attribute label_visibility.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Instance Method Summary collapse
- #described_by ⇒ Object
- #error? ⇒ Boolean
- #error_id ⇒ Object
- #errors ⇒ Object
- #hint_id ⇒ Object
- #html_opts ⇒ Object
-
#initialize(object:, attribute:, input_id:, label: nil, details: nil, error: nil, required: false, label_visibility: :visible, layout: :stacked) ⇒ FieldComponent
constructor
A new instance of FieldComponent.
- #label_hidden? ⇒ Boolean
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
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
8 9 10 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8 def attribute @attribute end |
#details ⇒ Object (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_id ⇒ Object (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_text ⇒ Object (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_visibility ⇒ Object (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 |
#layout ⇒ Object (readonly)
Returns the value of attribute layout.
8 9 10 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8 def layout @layout end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
8 9 10 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 8 def object @object end |
#required ⇒ Object (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_by ⇒ Object
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
47 48 49 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 47 def error? errors.any? end |
#error_id ⇒ Object
64 65 66 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 64 def error_id "#{input_id}_error" end |
#errors ⇒ Object
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_id ⇒ Object
60 61 62 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 60 def hint_id "#{input_id}_hint" end |
#html_opts ⇒ Object
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
75 76 77 |
# File 'lib/stimulus_plumbers/form/field_component.rb', line 75 def label_hidden? label_visibility == :exclusive end |