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
CheckboxField, EmailField, PasswordField, TextField, Wrapper
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.
- #config_key ⇒ Object
-
#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:, classes: nil, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **options) ⇒ Base
constructor
-
classes: CSS classes to add to the wrapping input group element.
-
-
#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:, classes: nil, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **options) ⇒ Base
-
classes: CSS classes to add to the wrapping input group element.
-
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.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/input_group/base.rb', line 110 def initialize(attribute:, form:, classes: nil, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **) @attribute = attribute @classes = classes @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 16 |
# File 'lib/input_group/base.rb', line 13 def classes @classes || classes_from_configuration(:input_group, config_key) end |
#config_key ⇒ Object
18 19 20 |
# File 'lib/input_group/base.rb', line 18 def config_key raise "Must be implemented in a subclass" end |
#error_classes ⇒ Object
Returns the classes to use for the help text.
39 40 41 42 |
# File 'lib/input_group/base.rb', line 39 def error_classes classes_from_configuration(:error, 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.
27 28 29 30 31 |
# File 'lib/input_group/base.rb', line 27 def errors return [] if form.object.nil? form.object.errors.(attribute) end |
#errors? ⇒ Boolean
Returns true if the input group has errors.
34 35 36 |
# File 'lib/input_group/base.rb', line 34 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.
51 52 53 54 |
# File 'lib/input_group/base.rb', line 51 def help @help ||= translate("help") end |
#help? ⇒ Boolean
Returns true if the input group has help configured
57 58 59 |
# File 'lib/input_group/base.rb', line 57 def help? help.present? end |
#help_classes ⇒ Object
Returns the classes to use for the help text.
62 63 64 65 |
# File 'lib/input_group/base.rb', line 62 def help_classes classes_from_configuration(:help, 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.
73 74 75 76 |
# File 'lib/input_group/base.rb', line 73 def hint @hint ||= translate("hint") end |
#hint? ⇒ Boolean
Returns true if the input group has a hint configured
79 80 81 |
# File 'lib/input_group/base.rb', line 79 def hint? hint.present? end |
#hint_classes ⇒ Object
Returns the classes to use for the hint text.
84 85 86 87 |
# File 'lib/input_group/base.rb', line 84 def hint_classes classes_from_configuration(:hint, config_key, state_key) || classes_from_configuration(:hint, :default, state_key) end |
#input_classes ⇒ Object
Returns the classes to use for the input field.
123 124 125 126 |
# File 'lib/input_group/base.rb', line 123 def input_classes classes_from_configuration(:input, 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).
143 144 145 146 |
# File 'lib/input_group/base.rb', line 143 def label @label ||= translate("label") end |
#label? ⇒ Boolean
Returns true if the input group has a label configured
149 150 151 |
# File 'lib/input_group/base.rb', line 149 def label? label.present? end |
#label_classes ⇒ Object
Returns the classes to use for the label field.
129 130 131 132 |
# File 'lib/input_group/base.rb', line 129 def label_classes classes_from_configuration(:label, 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.
161 162 163 164 |
# File 'lib/input_group/base.rb', line 161 def placeholder @placeholder ||= translate("placeholder") end |
#placeholder? ⇒ Boolean
Returns true if the input group has a placeholder configured
167 168 169 |
# File 'lib/input_group/base.rb', line 167 def placeholder? placeholder.present? end |