Class: Uchi::Flowbite::Input::Label

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/uchi/flowbite/input/label.rb

Overview

Constant Summary collapse

STATES =
[
  DEFAULT = :default,
  DISABLED = :disabled,
  ERROR = :error
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, form:, class: nil, disabled: false, options: {}) ⇒ Label

Returns a new instance of Label.



42
43
44
45
46
47
48
49
# File 'app/components/uchi/flowbite/input/label.rb', line 42

def initialize(attribute:, form:, class: nil, disabled: false, options: {})
  @attribute = attribute
  @class = Array.wrap(binding.local_variable_get(:class))
  @disabled = disabled
  @form = form
  @object = form.object
  @options = options
end

Class Method Details

.classes(state: :default, style: :default) ⇒ Object



14
15
16
17
# File 'app/components/uchi/flowbite/input/label.rb', line 14

def classes(state: :default, style: :default)
  style = styles.fetch(style)
  style.fetch(state)
end

.stylesObject



19
20
21
22
23
24
25
26
27
# File 'app/components/uchi/flowbite/input/label.rb', line 19

def styles
  {
    default: Uchi::Flowbite::Style.new(
      default: ["block", "mb-2.5", "text-sm", "font-medium", "text-heading"],
      disabled: ["block", "mb-2.5", "text-sm", "font-medium", "text-fg-disabled"],
      error: ["block", "mb-2.5", "text-sm", "font-medium", "text-fg-danger-strong"]
    )
  }.freeze
end

Instance Method Details

#callObject



30
31
32
33
34
35
36
# File 'app/components/uchi/flowbite/input/label.rb', line 30

def call
  if content?
    @form.label(@attribute, content, **options)
  else
    @form.label(@attribute, **options)
  end
end

#classesObject

Returns an array with the CSS classes to apply to the label element



52
53
54
# File 'app/components/uchi/flowbite/input/label.rb', line 52

def classes
  self.class.classes(state: state) + @class
end

#errors?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/components/uchi/flowbite/input/label.rb', line 38

def errors?
  @object.errors.include?(@attribute.intern)
end