Class: LightningUiKit::DropzoneComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Errors, Labelable
Defined in:
app/components/lightning_ui_kit/dropzone_component.rb

Instance Method Summary collapse

Methods included from Labelable

#effective_label, #render_label?

Methods included from Errors

#error_messages, #has_errors?, #infer_errors_from_association

Methods inherited from BaseComponent

#merge_classes

Methods included from HeroiconHelper

#heroicon

Constructor Details

#initialize(name:, value: nil, label: nil, form: nil, description: nil, multiple: false, accept: nil, disabled: false, placeholder: nil, error: nil, **options) ⇒ DropzoneComponent

Returns a new instance of DropzoneComponent.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 5

def initialize(name:, value: nil, label: nil, form: nil, description: nil, multiple: false, accept: nil, disabled: false, placeholder: nil, error: nil, **options)
  @name = name
  @value = value
  @disabled = disabled
  @accept = accept
  @multiple = multiple
  @error = error
  @label = label
  @form = form
  @description = description
  @placeholder = placeholder
  @options = options
end

Instance Method Details

#classesObject



19
20
21
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 19

def classes
  merge_classes(["lui:space-y-4 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

#dataObject



23
24
25
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 23

def data
  {controller: "lui-dropzone"}.merge(@options[:data] || {})
end

#description_dataObject



41
42
43
44
45
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 41

def description_data
  {slot: "description"}.merge(@options[:description_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#error_dataObject



47
48
49
50
51
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 47

def error_data
  {slot: "error"}.merge(@options[:error_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#file_input_html_optionsObject



71
72
73
74
75
76
77
78
79
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 71

def file_input_html_options
  {
    multiple: @multiple,
    class: "lui:hidden",
    direct_upload: true,
    data: input_data,
    accept: @accept
  }
end

#input_dataObject



27
28
29
30
31
32
33
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 27

def input_data
  (@options[:input_data] || {}).dup.tap do |data|
    data[:lui_dropzone_target] = "input"
    data[:action] = "dragover->lui-dropzone#activate drop->lui-dropzone#uploadFiles change->lui-dropzone#uploadFiles"
    data[:invalid] = "true" if has_errors?
  end
end

#label_dataObject



35
36
37
38
39
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 35

def label_data
  {slot: "label"}.merge(@options[:label_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#label_html_optionsObject



53
54
55
56
57
58
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 53

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

#render_file_inputObject



81
82
83
84
85
86
87
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 81

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_labelObject



60
61
62
63
64
65
66
67
68
69
# File 'app/components/lightning_ui_kit/dropzone_component.rb', line 60

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