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.

Direct Known Subclasses

CheckboxField, EmailField, PasswordField, TextField

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options)
  @attribute = attribute
  @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
# File 'lib/input_group/base.rb', line 13

def classes
  classes_from_configuration(:input_group, self.class.config_key)
end

#error_classesObject

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

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



22
23
24
25
26
# File 'lib/input_group/base.rb', line 22

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)


29
30
31
# File 'lib/input_group/base.rb', line 29

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.



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

Returns:

  • (Boolean)


52
53
54
# File 'lib/input_group/base.rb', line 52

def help?
  help.present?
end

#help_classesObject

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

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



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

Returns:

  • (Boolean)


74
75
76
# File 'lib/input_group/base.rb', line 74

def hint?
  hint.present?
end

#hint_classesObject

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_classesObject

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

#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).



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

Returns:

  • (Boolean)


141
142
143
# File 'lib/input_group/base.rb', line 141

def label?
  label.present?
end

#label_classesObject

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

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



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

Returns:

  • (Boolean)


159
160
161
# File 'lib/input_group/base.rb', line 159

def placeholder?
  placeholder.present?
end