Class: Felt::InputGroup::Base

Inherits:
ViewComponent::Base
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options)
  @attribute = attribute
  @classes = classes
  @form = form
  @help = help
  @hint = hint
  @input_options = input_options
  @label = label
  @options = options
  @placeholder = placeholder
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



10
11
12
# File 'lib/input_group/base.rb', line 10

def attribute
  @attribute
end

#formObject (readonly)

Returns the value of attribute form.



10
11
12
# File 'lib/input_group/base.rb', line 10

def form
  @form
end

#input_optionsObject (readonly)

Returns the value of attribute input_options.



10
11
12
# File 'lib/input_group/base.rb', line 10

def input_options
  @input_options
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/input_group/base.rb', line 10

def options
  @options
end

Instance Method Details

#classesObject

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_keyObject



18
19
20
# File 'lib/input_group/base.rb', line 18

def config_key
  raise "Must be implemented in a subclass"
end

#error_classesObject

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

#errorsObject

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.full_messages_for(attribute)
end

#errors?Boolean

Returns true if the input group has errors.

Returns:

  • (Boolean)


34
35
36
# File 'lib/input_group/base.rb', line 34

def errors?
  errors.any?
end

#helpObject

Returns the help text for the input group. If no help is configured, returns nil.

Help text is looked up in the following order:

  1. The help argument passed to the component.

  2. 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

Returns:

  • (Boolean)


57
58
59
# File 'lib/input_group/base.rb', line 57

def help?
  help.present?
end

#help_classesObject

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

#hintObject

Returns the hint for the input group. If no hint is configured, returns nil.

Hints are looked up in the following order:

  1. The hint argument passed to the component.

  2. 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

Returns:

  • (Boolean)


79
80
81
# File 'lib/input_group/base.rb', line 79

def hint?
  hint.present?
end

#hint_classesObject

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_classesObject

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

#labelObject

Returns the label for the input group. If no label is configured, returns nil.

Labels are looked up in the following order:

  1. The label argument passed to the component.

  2. The ‘label` key in the `forms.<object_name>.<attribute>` translation.

  3. 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

Returns:

  • (Boolean)


149
150
151
# File 'lib/input_group/base.rb', line 149

def label?
  label.present?
end

#label_classesObject

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

#placeholderObject

Returns the placeholder for the input group. If no placeholder is configured, returns nil.

Placeholders are looked up in the following order:

  1. The placeholder argument passed to the component.

  2. 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

Returns:

  • (Boolean)


167
168
169
# File 'lib/input_group/base.rb', line 167

def placeholder?
  placeholder.present?
end