Class: LightningUiKit::FileInputComponent
Instance Method Summary
collapse
-
#classes ⇒ Object
-
#control_classes ⇒ Object
-
#data ⇒ Object
-
#description_data ⇒ Object
-
#error_data ⇒ Object
-
#file_input_classes ⇒ Object
-
#file_input_html_options ⇒ Object
-
#initialize(name:, value: nil, autofocus: false, label: nil, form: nil, description: nil, multiple: false, accept: nil, disabled: false, error: nil, **options) ⇒ FileInputComponent
constructor
A new instance of FileInputComponent.
-
#input_data ⇒ Object
-
#label_data ⇒ Object
-
#label_html_options ⇒ Object
-
#render_file_input ⇒ Object
-
#render_label ⇒ Object
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, autofocus: false, label: nil, form: nil, description: nil, multiple: false, accept: nil, disabled: false, error: nil, **options) ⇒ FileInputComponent
Returns a new instance of FileInputComponent.
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 5
def initialize(name:, value: nil, autofocus: false, label: nil, form: nil, description: nil, multiple: false, accept: nil, disabled: false, error: nil, **options)
@name = name
@value = value
@disabled = disabled
@autofocus = autofocus
@accept = accept
@multiple = multiple
@error = error
@label = label
@form = form
@description = description
@options = options
end
|
Instance Method Details
#classes ⇒ Object
19
20
21
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 19
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
|
#control_classes ⇒ Object
93
94
95
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 93
def control_classes
"lui:relative lui:block lui:w-full 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"
end
|
#data ⇒ Object
23
24
25
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 23
def data
{controller: "lui-field"}.merge(@options[:data] || {})
end
|
#description_data ⇒ Object
40
41
42
43
44
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 40
def description_data
{slot: "description"}.merge(@options[:description_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
end
end
|
#error_data ⇒ Object
46
47
48
49
50
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 46
def error_data
{slot: "error"}.merge(@options[:error_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
end
end
|
70
71
72
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 70
def file_input_classes
"lui:relative lui:block lui:w-full lui:appearance-none lui:rounded-lg 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 lui:data-disabled:cursor-not-allowed lui:data-disabled:border-border-hover lui:data-disabled:file:cursor-not-allowed lui:file:cursor-pointer lui:cursor-pointer lui:file:border-0 lui:file:px-[calc(--spacing(3.5)-1px)] lui:file:py-[calc(--spacing(2.5)-1px)] lui:file:sm:px-[calc(--spacing(3)-1px)] lui:file:sm:py-[calc(--spacing(1.5)-1px)] lui:file:mr-4 lui:file:bg-surface-tertiary lui:file:rounded-l-[calc(var(--radius-lg)-1px)]"
end
|
74
75
76
77
78
79
80
81
82
83
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 74
def file_input_html_options
{
data: input_data,
class: file_input_classes,
disabled: @disabled,
autofocus: @autofocus,
multiple: @multiple,
accept: @accept
}
end
|
27
28
29
30
31
32
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 27
def input_data
{lui_field_target: "field"}.merge(@options[:input_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
data[:invalid] = "true" if has_errors?
end
end
|
#label_data ⇒ Object
34
35
36
37
38
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 34
def label_data
{slot: "label"}.merge(@options[:label_data] || {}).dup.tap do |data|
data[:disabled] = "true" if @disabled
end
end
|
#label_html_options ⇒ Object
52
53
54
55
56
57
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 52
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
|
85
86
87
88
89
90
91
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 85
def render_file_input
if @form
@form.file_field(@name, **file_input_html_options)
else
helpers.file_field_tag(@name, value: @value, **file_input_html_options)
end
end
|
#render_label ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'app/components/lightning_ui_kit/file_input_component.rb', line 59
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
|