Class: NitroKit::Dropzone

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/dropzone.rb

Constant Summary collapse

CONTROLLER_MESSAGE_KEYS =

Strings the Stimulus controller needs at runtime. Nitro translates them here and hands them to the controller as Stimulus values so no user-facing English lives in JavaScript.

%w[
  progress_for
  remove_file
  uploading
  uploading_percent
  uploaded
  upload_failed
  ready
  status.empty
  status.selected.one
  status.selected.other
  status.uploading.one
  status.uploading.other
  status.attention.one
  status.attention.other
  status.uploaded.one
  status.uploaded.other
  status.ready.one
  status.ready.other
  errors.upload_failed
  errors.upload_failed_detail
  errors.uploads_in_progress
  errors.failed_files
  errors.too_large
  errors.not_accepted
  errors.max_files.one
  errors.max_files.other
].freeze
PRESENTATIONS =

:input keeps the native file input visible beside the drop target. :minimal leaves the drop target as the only visible affordance; the input stays in the accessibility tree and keeps its own focus ring.

%i[input minimal].freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, label: I18n.t("nitro_kit.dropzone.label"), description: nil, presentation: :input, direct_upload: true, multiple: false, accept: nil, max_files: 1, max_bytes: nil, disabled: false, required: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Dropzone

Returns a new instance of Dropzone.



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
88
89
90
91
92
93
94
95
96
97
98
# File 'app/components/nitro_kit/dropzone.rb', line 42

def initialize(
  id:,
  name:,
  label: I18n.t("nitro_kit.dropzone.label"),
  description: nil,
  presentation: :input,
  direct_upload: true,
  multiple: false,
  accept: nil,
  max_files: 1,
  max_bytes: nil,
  disabled: false,
  required: false,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @identifier = component_id(id)
  @name = form_name(name)
  @label = required_text(:label, label)
  @description = optional_text(:description, description)
  @presentation = validate_choice!(:presentation, presentation, PRESENTATIONS)
  @direct_upload = validate_boolean!(:direct_upload, direct_upload)
  @multiple = validate_boolean!(:multiple, multiple)
  @accept = optional_text(:accept, accept)
  @max_files = positive_integer(:max_files, max_files)
  @max_bytes = positive_integer(:max_bytes, max_bytes, allow_nil: true)
  @disabled = validate_boolean!(:disabled, disabled)
  @required = validate_boolean!(:required, required)

  if !@multiple && @max_files != 1
    raise ArgumentError, "max_files must be 1 when multiple is false"
  end

  super(
    component: :dropzone,
    attributes: {
      id: @identifier,
      aria: { disabled: @disabled ? true : nil },
      data: {
        presentation: @presentation,
        controller: @disabled ? nil : "nk--dropzone",
        state: @disabled ? "disabled" : "idle",
        action: @disabled ? nil : dropzone_actions,
        nk__dropzone_direct_upload_value: @direct_upload,
        nk__dropzone_max_files_value: @max_files,
        nk__dropzone_max_bytes_value: @max_bytes,
        nk__dropzone_accept_value: @accept
      }.compact.merge(@disabled ? {} : controller_message_values)
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def description
  @description
end

#identifierObject (readonly)

Returns the value of attribute identifier.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def identifier
  @identifier
end

#labelObject (readonly) Also known as: html_label

Returns the value of attribute label.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def label
  @label
end

#max_bytesObject (readonly)

Returns the value of attribute max_bytes.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def max_bytes
  @max_bytes
end

#max_filesObject (readonly)

Returns the value of attribute max_files.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def max_files
  @max_files
end

#nameObject (readonly)

Returns the value of attribute name.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def name
  @name
end

#presentationObject (readonly)

Returns the value of attribute presentation.



103
104
105
# File 'app/components/nitro_kit/dropzone.rb', line 103

def presentation
  @presentation
end

Instance Method Details

#view_templateObject



105
106
107
108
109
110
111
112
113
114
# File 'app/components/nitro_kit/dropzone.rb', line 105

def view_template
  div(**root_attributes) do
    render_message
    render_input
    render_status
    render_error
    render_preview_list
    render_preview_template
  end
end