Class: StimulusPlumbers::Form::Field
- Inherits:
-
Object
- Object
- StimulusPlumbers::Form::Field
- Defined in:
- lib/stimulus_plumbers/form/field.rb
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Class Method Summary collapse
Instance Method Summary collapse
- #described_by(object, attribute, input_id) ⇒ Object
- #error?(object, attribute) ⇒ Boolean
-
#initialize(template, label: nil, hint: nil, error: nil, required: false, hide_label: false, layout: :stacked, **kwargs) ⇒ Field
constructor
A new instance of Field.
- #label_hidden? ⇒ Boolean
- #render(object, attribute, input_id:, &block) ⇒ Object
- #render_errors(object, attribute, input_id) ⇒ Object
- #render_hint(input_id) ⇒ Object
Constructor Details
#initialize(template, label: nil, hint: nil, error: nil, required: false, hide_label: false, layout: :stacked, **kwargs) ⇒ Field
Returns a new instance of Field.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/stimulus_plumbers/form/field.rb', line 12 def initialize( template, label: nil, hint: nil, error: nil, required: false, hide_label: false, layout: :stacked, **kwargs ) @template = template @label = label @hint = hint @error_override = error @required = required @hide_label = hide_label @layout = layout.to_sym @kwargs = kwargs end |
Instance Attribute Details
#label ⇒ Object (readonly)
Returns the value of attribute label.
6 7 8 |
# File 'lib/stimulus_plumbers/form/field.rb', line 6 def label @label end |
#layout ⇒ Object (readonly)
Returns the value of attribute layout.
6 7 8 |
# File 'lib/stimulus_plumbers/form/field.rb', line 6 def layout @layout end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
6 7 8 |
# File 'lib/stimulus_plumbers/form/field.rb', line 6 def required @required end |
Class Method Details
.label_id(input_id) ⇒ Object
8 9 10 |
# File 'lib/stimulus_plumbers/form/field.rb', line 8 def self.label_id(input_id) [input_id, "label"].compact.join("_") end |
Instance Method Details
#described_by(object, attribute, input_id) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/stimulus_plumbers/form/field.rb', line 40 def described_by(object, attribute, input_id) ids = [] ids << hint_id(input_id) if @hint.present? ids.concat(build_error_ids(object, attribute, input_id)) ids.join(" ").presence end |
#error?(object, attribute) ⇒ Boolean
36 37 38 |
# File 'lib/stimulus_plumbers/form/field.rb', line 36 def error?(object, attribute) build_errors(object, attribute).any? end |
#label_hidden? ⇒ Boolean
32 33 34 |
# File 'lib/stimulus_plumbers/form/field.rb', line 32 def label_hidden? @hide_label end |
#render(object, attribute, input_id:, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/stimulus_plumbers/form/field.rb', line 47 def render(object, attribute, input_id:, &block) @label ||= attribute.to_s.humanize error = error?(object, attribute) aria = build_aria(object, attribute, input_id) generated_opts = (input_id, aria) field_html = @template.capture(generated_opts, @kwargs, error, &block) Fields::Group.new(@template).render(layout: @layout, error: error) do @template.safe_join( [ field_label(input_id), field_html, render_hint(input_id), render_errors(object, attribute, input_id) ] ) end end |
#render_errors(object, attribute, input_id) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/stimulus_plumbers/form/field.rb', line 69 def render_errors(object, attribute, input_id) errs = build_errors(object, attribute) return if errs.none? @template.safe_join( errs.map.with_index do |, i| Fields::Error.new(@template).render(message: , id: build_error_ids(object, attribute, input_id)[i]) end ) end |