Class: SdrViewComponents::Forms::FieldComponent
- Inherits:
-
BaseComponent
- Object
- ViewComponent::Base
- BaseComponent
- SdrViewComponents::Forms::FieldComponent
- Defined in:
- app/components/sdr_view_components/forms/field_component.rb
Overview
Base component for all form fields.
Direct Known Subclasses
CheckboxComponent, FileComponent, RadioButtonComponent, SelectFieldComponent, TextAreaComponent, TextFieldComponent, ToggleComponent
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#variant ⇒ Object
readonly
Returns the value of attribute variant.
Instance Method Summary collapse
-
#container_args ⇒ Object
Returns a hash of arguments for the container element.
- #container_classes ⇒ Object
-
#error_args ⇒ Object
Returns a hash of arguments for the error element.
-
#help_text_args ⇒ Object
Returns a hash of arguments for the help text element.
- #help_text_below? ⇒ Boolean
-
#initialize(form:, field_name:, container_classes: [], variant: :default, **args) ⇒ FieldComponent
constructor
A new instance of FieldComponent.
-
#input_args ⇒ Object
Returns a hash of arguments for the input element.
-
#input_component ⇒ Object
Any component that inherits from FieldComponent must implement this method or provide its own template.
-
#label_args ⇒ Object
Returns a hash of arguments for the label element.
-
#label_field_name ⇒ Object
Subclasses may override, e.g., for radio buttons.
Methods inherited from BaseComponent
#args_for, #merge_actions, #merge_classes
Constructor Details
#initialize(form:, field_name:, container_classes: [], variant: :default, **args) ⇒ FieldComponent
Returns a new instance of FieldComponent.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 9 def initialize(form:, field_name:, container_classes: [], variant: :default, **args) @form = form @field_name = field_name @container_classes = container_classes @variant = variant # In the help_text_below variant, the help text is rendered below the input field. # This is necessary for some types of fields such as checkboxes. raise ArgumentError, 'invalid variant' unless %i[default help_text_below].include?(@variant) @args = args super() end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
22 23 24 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22 def args @args end |
#field_name ⇒ Object (readonly)
Returns the value of attribute field_name.
22 23 24 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22 def field_name @field_name end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
22 23 24 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22 def form @form end |
#variant ⇒ Object (readonly)
Returns the value of attribute variant.
22 23 24 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22 def variant @variant end |
Instance Method Details
#container_args ⇒ Object
Returns a hash of arguments for the container element. Prefix and argument with ‘container_’ in order for it to be passed to the container div. (i.e. :container_class will be passed as :class to the container div)
39 40 41 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 39 def container_args args_for(args:, prefix: 'container_') end |
#container_classes ⇒ Object
76 77 78 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 76 def container_classes merge_classes(@container_classes) end |
#error_args ⇒ Object
Returns a hash of arguments for the error element. Prefix and argument with ‘error_’ in order for it to be passed to to the invalid feedback element.
46 47 48 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 46 def error_args args_for(args:, prefix: 'error_') end |
#help_text_args ⇒ Object
Returns a hash of arguments for the help text element. Prefix and argument with ‘help_’ in order for it to be passed to to the help text element.
53 54 55 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 53 def help_text_args args_for(args:, prefix: 'help_').merge({ id: help_text_id }) end |
#help_text_below? ⇒ Boolean
80 81 82 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 80 def help_text_below? variant == :help_text_below end |
#input_args ⇒ Object
Returns a hash of arguments for the input element. Prefix and argument with ‘input_’ in order for it to be passed to the input element.
60 61 62 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 60 def input_args args_for(args:, prefix: 'input_').merge({ aria: field_aria }) end |
#input_component ⇒ Object
Any component that inherits from FieldComponent must implement this method or provide its own template.
26 27 28 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 26 def input_component raise NotImplementedError end |
#label_args ⇒ Object
Returns a hash of arguments for the label element. Prefix and argument with ‘label_’ in order for it to be passed to the label element.
67 68 69 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 67 def label_args args_for(args:, prefix: 'label_') end |
#label_field_name ⇒ Object
Subclasses may override, e.g., for radio buttons.
72 73 74 |
# File 'app/components/sdr_view_components/forms/field_component.rb', line 72 def label_field_name field_name end |