Class: Felt::Label

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
lib/label.rb

Overview

Renders a label element for a form input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, form:, classes: nil, text: nil, **options) ⇒ Label

  • classes: Classes to add to the label element.

  • text: The label text to show. 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.

All remaining keyword arguments are passed to the label element. See ActionView::Helpers::FormBuilder#label for details.



24
25
26
27
28
29
30
# File 'lib/label.rb', line 24

def initialize(attribute:, form:, classes: nil, text: nil, **options)
  @attribute = attribute
  @classes = classes
  @form = form
  @text = text
  @options = options
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



8
9
10
# File 'lib/label.rb', line 8

def attribute
  @attribute
end

#formObject (readonly)

Returns the value of attribute form.



8
9
10
# File 'lib/label.rb', line 8

def form
  @form
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/label.rb', line 8

def options
  @options
end

Instance Method Details

#classesObject

Returns the classes to use for the label element



11
12
13
14
# File 'lib/label.rb', line 11

def classes
  @classes ||
    Felt.configuration.classes.dig(:label, :default, :default)
end

#textObject

Returns the text to render in the label. If no text is configured, returns nil.

Label texts are looked up in the following order:

  1. The text 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).



42
43
44
# File 'lib/label.rb', line 42

def text
  @text ||= translate("label")
end

#text?Boolean

Returns true if the input group has a label text configured

Returns:

  • (Boolean)


47
48
49
# File 'lib/label.rb', line 47

def text?
  text.present?
end