Class: Felt::Hint

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

Overview

Renders a hint element for a form input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

  • classes: Classes to add to the hint element.

  • text: The hint text to show. If not provided, the text will be looked up in the ‘forms.<object_name>.<attribute>` translation. See #hint for more details. To disable the hint, pass an empty string.

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



24
25
26
27
28
29
30
# File 'lib/hint.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/hint.rb', line 8

def attribute
  @attribute
end

#formObject (readonly)

Returns the value of attribute form.



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

def form
  @form
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#classesObject

Returns the classes to use for the hint element



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

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

#render?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/hint.rb', line 32

def render?
  text.present?
end

#textObject

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

Hint texts are looked up in the following order:

  1. The text argument passed to the component.

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

  3. The translation value found under ‘helpers.hint.<modelname>.<attribute>` (like with ActionView::Helpers::FormBuilder#hint).



46
47
48
# File 'lib/hint.rb', line 46

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

#text?Boolean

Returns true if the input group has a hint text configured

Returns:

  • (Boolean)


51
52
53
# File 'lib/hint.rb', line 51

def text?
  text.present?
end