-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel.rb
More file actions
87 lines (73 loc) · 2.05 KB
/
label.rb
File metadata and controls
87 lines (73 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
module Flowbite
class Input
# https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-label
class Label < ViewComponent::Base
STATES = [
DEFAULT = :default,
DISABLED = :disabled,
ERROR = :error
].freeze
class << self
def classes(state: :default, style: :default)
style = styles.fetch(style)
style.fetch(state)
end
def styles
Flowbite::Styles.from_hash(
{
default: {
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
end
def call
if content?
@form.label(@attribute, content, **options)
else
@form.label(@attribute, **options)
end
end
def errors?
return false unless @object
@object.errors.include?(@attribute.intern)
end
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
# Returns an array with the CSS classes to apply to the label element
def classes
self.class.classes(state: state) + @class
end
protected
def disabled?
!!@disabled
end
# Returns the state of the label.
#
# See the STATES constant for valid values.
#
# @return [Symbol] the state of the label
def state
return DISABLED if disabled?
return ERROR if errors?
DEFAULT
end
private
def options
{
class: classes
}.merge(@options)
end
end
end
end