Class: Felt::Label
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Felt::Label
- Defined in:
- lib/label.rb
Overview
Renders a label element for a form input.
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#classes ⇒ Object
Returns the classes to use for the label element.
-
#initialize(attribute:, form:, classes: nil, text: nil, **options) ⇒ Label
constructor
-
classes: Classes to add to the label element.
-
-
#text ⇒ Object
Returns the text to render in the label.
-
#text? ⇒ Boolean
Returns true if the input group has a label text configured.
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, **) @attribute = attribute @classes = classes @form = form @text = text @options = end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
8 9 10 |
# File 'lib/label.rb', line 8 def attribute @attribute end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
8 9 10 |
# File 'lib/label.rb', line 8 def form @form end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/label.rb', line 8 def @options end |
Instance Method Details
#classes ⇒ Object
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 |
#text ⇒ Object
Returns the text to render in the label. If no text is configured, returns nil.
Label texts are looked up in the following order:
-
The text 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).
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
47 48 49 |
# File 'lib/label.rb', line 47 def text? text.present? end |