Class: Felt::InputGroup::Base
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Felt::InputGroup::Base
- Defined in:
- lib/input_group/base.rb
Overview
Renders a stacked input group element. This is the base class for all input groups and should not be instantiated directly.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#input_options ⇒ Object
readonly
Returns the value of attribute input_options.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#classes ⇒ Object
Returns the classes to use for the root element of the input.
-
#error_classes ⇒ Object
Returns the classes to use for the help text.
-
#errors ⇒ Object
Returns the error messages to output in the input group.
-
#errors? ⇒ Boolean
Returns true if the input group has errors.
-
#help ⇒ Object
Returns the help text for the input group.
-
#help? ⇒ Boolean
Returns true if the input group has help configured.
-
#help_classes ⇒ Object
Returns the classes to use for the help text.
-
#hint ⇒ Object
Returns the hint for the input group.
-
#hint? ⇒ Boolean
Returns true if the input group has a hint configured.
-
#hint_classes ⇒ Object
Returns the classes to use for the hint text.
-
#initialize(attribute:, form:, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **options) ⇒ Base
constructor
-
hint: The hint for the input group.
-
-
#input_classes ⇒ Object
Returns the classes to use for the input field.
-
#label ⇒ Object
Returns the label for the input group.
-
#label? ⇒ Boolean
Returns true if the input group has a label configured.
-
#label_classes ⇒ Object
Returns the classes to use for the label field.
-
#placeholder ⇒ Object
Returns the placeholder for the input group.
-
#placeholder? ⇒ Boolean
Returns true if the input group has a placeholder configured.
Constructor Details
#initialize(attribute:, form:, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **options) ⇒ Base
-
hint: The hint for the input group. If not provided, the hint will be looked up in the ‘forms.<object_name>.<attribute>` translation. See #hint for more details. To disable the hint, pass an empty string.
-
input_options: The options to pass directly to the input field.
-
label: The label text for the input group. If not provided, the text will be looked up in the ‘forms.<object_name>.<attribute>` translation. See #label for more details. To disable the label, pass an empty string.
-
placeholder: The placeholder for the input field. If not provided, the placeholder will be looked up in the ‘forms.<object_name>.<attribute>` translation. See #placeholder for more details. To disable the placeholder, pass an empty string.
All remaining keyword arguments are passed to the wrapping div element of the input group. See ActionView::Helpers::TagHelper#content_tag for details.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/input_group/base.rb', line 103 def initialize(attribute:, form:, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **) @attribute = attribute @form = form @help = help @hint = hint @input_options = @label = label @options = @placeholder = placeholder end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
10 11 12 |
# File 'lib/input_group/base.rb', line 10 def attribute @attribute end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
10 11 12 |
# File 'lib/input_group/base.rb', line 10 def form @form end |
#input_options ⇒ Object (readonly)
Returns the value of attribute input_options.
10 11 12 |
# File 'lib/input_group/base.rb', line 10 def @input_options end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/input_group/base.rb', line 10 def @options end |
Instance Method Details
#classes ⇒ Object
Returns the classes to use for the root element of the input.
13 14 15 |
# File 'lib/input_group/base.rb', line 13 def classes classes_from_configuration(:input_group, self.class.config_key) end |
#error_classes ⇒ Object
Returns the classes to use for the help text.
34 35 36 37 |
# File 'lib/input_group/base.rb', line 34 def error_classes classes_from_configuration(:error, self.class.config_key, state_key) || classes_from_configuration(:error, :default, state_key) end |
#errors ⇒ Object
Returns the error messages to output in the input group. Returns [] if no errors.
This returns the full error messages for the attribute, see ActiveModel::Errors#full_messages for more details.
22 23 24 25 26 |
# File 'lib/input_group/base.rb', line 22 def errors return [] if form.object.nil? form.object.errors.(attribute) end |
#errors? ⇒ Boolean
Returns true if the input group has errors.
29 30 31 |
# File 'lib/input_group/base.rb', line 29 def errors? errors.any? end |
#help ⇒ Object
Returns the help text for the input group. If no help is configured, returns nil.
Help text is looked up in the following order:
-
The help argument passed to the component.
-
The ‘help` key in the `forms.<object_name>.<attribute>` translation.
46 47 48 49 |
# File 'lib/input_group/base.rb', line 46 def help @help ||= translate("help") end |
#help? ⇒ Boolean
Returns true if the input group has help configured
52 53 54 |
# File 'lib/input_group/base.rb', line 52 def help? help.present? end |
#help_classes ⇒ Object
Returns the classes to use for the help text.
57 58 59 60 |
# File 'lib/input_group/base.rb', line 57 def help_classes classes_from_configuration(:help, self.class.config_key, state_key) || classes_from_configuration(:help, :default, state_key) end |
#hint ⇒ Object
Returns the hint for the input group. If no hint is configured, returns nil.
Hints are looked up in the following order:
-
The hint argument passed to the component.
-
The ‘hint` key in the `forms.<object_name>.<attribute>` translation.
68 69 70 71 |
# File 'lib/input_group/base.rb', line 68 def hint @hint ||= translate("hint") end |
#hint? ⇒ Boolean
Returns true if the input group has a hint configured
74 75 76 |
# File 'lib/input_group/base.rb', line 74 def hint? hint.present? end |
#hint_classes ⇒ Object
Returns the classes to use for the hint text.
79 80 81 82 |
# File 'lib/input_group/base.rb', line 79 def hint_classes classes_from_configuration(:hint, self.class.config_key, state_key) || classes_from_configuration(:hint, :default, state_key) end |
#input_classes ⇒ Object
Returns the classes to use for the input field.
115 116 117 118 |
# File 'lib/input_group/base.rb', line 115 def input_classes classes_from_configuration(:input, self.class.config_key, state_key) || classes_from_configuration(:input, :default, state_key) end |
#label ⇒ Object
Returns the label for the input group. If no label is configured, returns nil.
Labels are looked up in the following order:
-
The label argument passed to the component.
-
The ‘label` key in the `forms.<object_name>.<attribute>` translation.
-
The translation value found under ‘helpers.label.<modelname>.<attribute>` (like with ActionView::Helpers::FormBuilder#label).
135 136 137 138 |
# File 'lib/input_group/base.rb', line 135 def label @label ||= translate("label") end |
#label? ⇒ Boolean
Returns true if the input group has a label configured
141 142 143 |
# File 'lib/input_group/base.rb', line 141 def label? label.present? end |
#label_classes ⇒ Object
Returns the classes to use for the label field.
121 122 123 124 |
# File 'lib/input_group/base.rb', line 121 def label_classes classes_from_configuration(:label, self.class.config_key, state_key) || classes_from_configuration(:label, :default, state_key) end |
#placeholder ⇒ Object
Returns the placeholder for the input group. If no placeholder is configured, returns nil.
Placeholders are looked up in the following order:
-
The placeholder argument passed to the component.
-
The ‘placeholder` key in the `forms.<object_name>.<attribute>` translation.
153 154 155 156 |
# File 'lib/input_group/base.rb', line 153 def placeholder @placeholder ||= translate("placeholder") end |
#placeholder? ⇒ Boolean
Returns true if the input group has a placeholder configured
159 160 161 |
# File 'lib/input_group/base.rb', line 159 def placeholder? placeholder.present? end |