Class: LightningUiKit::InputComponent
Constant Summary
collapse
- INPUT_TYPES =
{
text: :text_field,
email: :email_field,
password: :password_field,
number: :number_field,
date: :date_field,
datetime: :datetime_local_field,
datetime_local: :datetime_local_field,
month: :month_field,
week: :week_field,
time: :time_field,
url: :url_field,
search: :search_field,
telephone: :telephone_field,
phone: :telephone_field
}.freeze
- STANDARD_INPUT_TYPES =
INPUT_TYPES.keys.freeze
Instance Method Summary
collapse
-
#classes ⇒ Object
-
#color_classes ⇒ Object
-
#color_html_options ⇒ Object
-
#control_classes ⇒ Object
-
#data ⇒ Object
-
#description_data ⇒ Object
-
#error_data ⇒ Object
-
#hidden_html_options ⇒ Object
-
#initialize(name:, value: nil, label: nil, form: nil, type: :text, description: nil, disabled: false, placeholder: nil, error: nil, input_options: {}, **options) ⇒ InputComponent
constructor
A new instance of InputComponent.
-
#input_classes ⇒ Object
-
#input_data ⇒ Object
-
#input_html_options ⇒ Object
-
#label_data ⇒ Object
-
#label_html_options ⇒ Object
-
#range_classes ⇒ Object
-
#range_html_options ⇒ Object
-
#render_color_input ⇒ Object
-
#render_hidden_input ⇒ Object
-
#render_input ⇒ Object
-
#render_label ⇒ Object
-
#render_range_input ⇒ Object
-
#standard_input_type? ⇒ Boolean
Methods included from Labelable
#effective_label, #render_label?
Methods included from Errors
#error_messages, #has_errors?, #infer_errors_from_association
#merge_classes
#heroicon
Constructor Details
#initialize(name:, value: nil, label: nil, form: nil, type: :text, description: nil, disabled: false, placeholder: nil, error: nil, input_options: {}, **options) ⇒ InputComponent
Returns a new instance of InputComponent.
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 24
def initialize(name:, value: nil, label: nil, form: nil, type: :text, description: nil, disabled: false, placeholder: nil, error: nil, input_options: {}, **options)
@name = name
@value = value
@disabled = disabled
@error = error
@label = label
@form = form
@type = type
@description = description
@placeholder = placeholder
@input_options = input_options
@options = options
end
|
Instance Method Details
#classes ⇒ Object
38
39
40
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 38
def classes
merge_classes(["lui:[&>[data-slot=label]+[data-slot=control]]:mt-3 lui:[&>[data-slot=label]+[data-slot=description]]:mt-1 lui:[&>[data-slot=description]+[data-slot=control]]:mt-3 lui:[&>[data-slot=control]+[data-slot=description]]:mt-3 lui:[&>[data-slot=control]+[data-slot=error]]:mt-3 lui:*:data-[slot=label]:font-medium", @options[:class]].compact.join(" "))
end
|
#color_classes ⇒ Object
119
120
121
122
123
124
125
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 119
def color_classes
"lui:h-10 lui:w-14 lui:cursor-pointer lui:appearance-none lui:rounded-lg lui:border lui:border-border lui:bg-transparent lui:p-1 " \
"lui:data-[hover]:border-border-hover lui:focus:outline-none lui:focus:ring-2 lui:focus:ring-focus " \
"[&::-webkit-color-swatch-wrapper]:lui:p-0 [&::-webkit-color-swatch]:lui:rounded-md [&::-webkit-color-swatch]:lui:border-0 " \
"[&::-moz-color-swatch]:lui:rounded-md [&::-moz-color-swatch]:lui:border-0 " \
"lui:disabled:opacity-50 lui:disabled:cursor-not-allowed"
end
|
#color_html_options ⇒ Object
111
112
113
114
115
116
117
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 111
def color_html_options
{
data: {lui_field_target: "field"},
class: color_classes,
disabled: @disabled
}.merge(@input_options).compact
end
|
#control_classes ⇒ Object
153
154
155
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 153
def control_classes
"lui:relative lui:block lui:w-full lui:before:pointer-events-none lui:before:absolute lui:before:inset-px lui:before:rounded-[7px] lui:before:bg-surface-input lui:before:shadow-sm lui:after:pointer-events-none lui:after:absolute lui:after:inset-0 lui:after:rounded-lg lui:after:ring-transparent lui:after:ring-inset lui:sm:focus-within:after:ring-2 lui:sm:focus-within:after:ring-focus lui:has-data-disabled:opacity-50 lui:has-data-disabled:before:bg-surface-hover lui:has-data-disabled:before:shadow-none lui:has-data-invalid:before:shadow-destructive-border/10"
end
|
#data ⇒ Object
42
43
44
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 42
def data
{controller: "lui-field"}.merge(@options[:data] || {})
end
|
#description_data ⇒ Object
58
59
60
61
62
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 58
def description_data
{slot: "description"}.merge(@options[:description_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
end
end
|
#error_data ⇒ Object
64
65
66
67
68
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 64
def error_data
{slot: "error"}.merge(@options[:error_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
end
end
|
#hidden_html_options ⇒ Object
107
108
109
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 107
def hidden_html_options
{data: input_data}.merge(@input_options).compact
end
|
70
71
72
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 70
def input_classes
"lui:peer lui:relative lui:block lui:w-full lui:appearance-none lui:rounded-lg lui:px-[calc(--spacing(3.5)-1px)] lui:py-[calc(--spacing(2.5)-1px)] lui:sm:px-[calc(--spacing(3)-1px)] lui:sm:py-[calc(--spacing(1.5)-1px)] lui:text-base/6 lui:text-foreground lui:placeholder:text-foreground-muted lui:sm:text-sm/6 lui:border lui:border-border lui:data-[hover]:border-border-hover lui:bg-transparent lui:focus:outline-hidden lui:data-invalid:border-destructive-border lui:data-invalid:data-[hover]:border-destructive-border/60 lui:data-disabled:border-border-hover"
end
|
46
47
48
49
50
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 46
def input_data
{lui_field_target: "field"}.merge(@options[:input_data] || {}).dup.tap do |data|
data[:invalid] = "true" if has_errors?
end
end
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 74
def input_html_options
base_options = {
data: input_data,
class: input_classes,
disabled: @disabled,
placeholder: @placeholder
}
case @type
when :number
base_options[:min] = @options[:min]
base_options[:max] = @options[:max]
base_options[:step] = @options[:step]
when :date, :datetime, :datetime_local, :month, :week, :time
base_options[:min] = @options[:min]
base_options[:max] = @options[:max]
end
base_options.merge(@input_options).compact
end
|
#label_data ⇒ Object
52
53
54
55
56
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 52
def label_data
{slot: "label"}.merge(@options[:label_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
end
end
|
#label_html_options ⇒ Object
135
136
137
138
139
140
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 135
def label_html_options
{
class: "lui:text-base/6 lui:text-foreground lui:select-none lui:data-disabled:opacity-50 lui:sm:text-sm/6",
data: label_data
}
end
|
#range_classes ⇒ Object
127
128
129
130
131
132
133
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 127
def range_classes
"lui:w-full lui:h-2 lui:appearance-none lui:cursor-pointer lui:bg-surface-tertiary lui:rounded-full lui:focus:outline-none " \
"[&::-webkit-slider-thumb]:lui:appearance-none [&::-webkit-slider-thumb]:lui:w-4 [&::-webkit-slider-thumb]:lui:h-4 [&::-webkit-slider-thumb]:lui:bg-interactive [&::-webkit-slider-thumb]:lui:rounded-full [&::-webkit-slider-thumb]:lui:cursor-pointer [&::-webkit-slider-thumb]:lui:shadow-sm [&::-webkit-slider-thumb]:lui:transition-transform [&::-webkit-slider-thumb]:hover:lui:scale-110 " \
"[&::-moz-range-thumb]:lui:appearance-none [&::-moz-range-thumb]:lui:w-4 [&::-moz-range-thumb]:lui:h-4 [&::-moz-range-thumb]:lui:bg-interactive [&::-moz-range-thumb]:lui:rounded-full [&::-moz-range-thumb]:lui:border-0 [&::-moz-range-thumb]:lui:cursor-pointer [&::-moz-range-thumb]:lui:shadow-sm [&::-moz-range-thumb]:lui:transition-transform [&::-moz-range-thumb]:hover:lui:scale-110 " \
"[&::-moz-range-track]:lui:bg-surface-tertiary [&::-moz-range-track]:lui:rounded-full " \
"lui:disabled:opacity-50 lui:disabled:cursor-not-allowed"
end
|
#range_html_options ⇒ Object
96
97
98
99
100
101
102
103
104
105
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 96
def range_html_options
{
data: input_data,
class: range_classes,
disabled: @disabled,
min: @options[:min],
max: @options[:max],
step: @options[:step]
}.merge(@input_options).compact
end
|
179
180
181
182
183
184
185
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 179
def render_color_input
if @form
@form.color_field(@name, **color_html_options)
else
helpers.color_field_tag(@name, @value, **color_html_options)
end
end
|
187
188
189
190
191
192
193
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 187
def render_hidden_input
if @form
@form.hidden_field(@name, **hidden_html_options)
else
helpers.hidden_field_tag(@name, @value, **hidden_html_options)
end
end
|
161
162
163
164
165
166
167
168
169
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 161
def render_input
method_name = INPUT_TYPES[@type]
if @form
@form.public_send(method_name, @name, **input_html_options)
else
tag_method = "#{method_name}_tag"
helpers.public_send(tag_method, @name, @value, **input_html_options)
end
end
|
#render_label ⇒ Object
142
143
144
145
146
147
148
149
150
151
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 142
def render_label
return unless render_label?
label_text = effective_label
if @form
@form.label(@name, label_text, **label_html_options)
else
helpers.label_tag(@name, label_text, **label_html_options)
end
end
|
171
172
173
174
175
176
177
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 171
def render_range_input
if @form
@form.range_field(@name, **range_html_options)
else
helpers.range_field_tag(@name, @value, **range_html_options)
end
end
|
157
158
159
|
# File 'app/components/lightning_ui_kit/input_component.rb', line 157
def standard_input_type?
STANDARD_INPUT_TYPES.include?(@type)
end
|