Class: HakumiComponents::Upload::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
T::Sig
Includes:
Concerns::FormField
Defined in:
app/components/hakumi_components/upload/component.rb

Constant Summary collapse

PayloadValue =
T.type_alias { T.nilable(Types::ValidationComparable) }
PayloadHash =
T.type_alias { T::Hash[Types::HtmlKey, PayloadValue] }
FileListHash =
T.type_alias { T::Hash[Types::HtmlKey, PayloadValue] }
FileListInput =
T.type_alias { T.any(HakumiComponents::Upload::FileEntry, FileListHash, String, Symbol) }
AvatarSize =
T.type_alias { T.any(Symbol, Integer) }
AvatarIcon =
T.type_alias { Types::HtmlKey }
FileListScalar =
T.type_alias { Types::StringOrSymbol }
LIST_TYPES =
T.let(%i[text picture picture_card avatar].freeze, T::Array[Symbol])

Constants inherited from BaseComponent

BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SIZES, BaseComponent::SizeValue, BaseComponent::SymbolInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::FormField

#describedby_ids, #error?, #error_message, #form_field_caption, #form_field_contract, #form_field_errors, #form_field_html_options, #form_field_label, #form_field_name, #form_field_required, #form_field_rules, #form_field_standalone, #form_item_attributes, #form_item_classes, #input_id, #render_caption, #render_error, #render_explain, #render_label, #standalone?

Methods included from Concerns::FormFieldInterface

#form_field_contract

Methods inherited from BaseComponent

#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?

Constructor Details

#initialize(action:, name:, label: nil, caption: nil, accept: nil, multiple: false, max_count: nil, max_size: nil, disabled: false, list_type: :text, show_upload_list: true, drag: false, headers: nil, data: nil, default_file_list: [], with_credentials: false, auto_upload: true, inline_trigger: false, required: false, standalone: true, rules: nil, errors: [], avatar_shape: :circle, avatar_size: 80, avatar_icon: :user, preview_label: nil, remove_label: nil, close_label: nil, previous_label: nil, next_label: nil, **html_options) ⇒ Component

Returns a new instance of Component.



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/components/hakumi_components/upload/component.rb', line 55

def initialize(
  action:,
  name:,
  label: nil,
  caption: nil,
  accept: nil,
  multiple: false,
  max_count: nil,
  max_size: nil,
  disabled: false,
  list_type: :text,
  show_upload_list: true,
  drag: false,
  headers: nil,
  data: nil,
  default_file_list: [],
  with_credentials: false,
  auto_upload: true,
  inline_trigger: false,
  required: false,
  standalone: true,
  rules: nil,
  errors: [],
  avatar_shape: :circle,
  avatar_size: 80,
  avatar_icon: :user,
  preview_label: nil,
  remove_label: nil,
  close_label: nil,
  previous_label: nil,
  next_label: nil,
  **html_options
)
  @action = action
  @name = name
  @label = label
  @caption = caption
  @accept = accept
  @multiple = multiple
  @max_count = max_count
  @max_size = max_size
  @disabled = T.let(cast_boolean(disabled) == true, T::Boolean)
  @list_type = list_type
  @show_upload_list = T.let(cast_boolean(show_upload_list) != false, T::Boolean)
  @drag = T.let(cast_boolean(drag) == true, T::Boolean)
  @headers = T.let(normalize_payload(headers), T.nilable(T::Hash[Symbol, PayloadValue]))
  @data = T.let(normalize_payload(data), T.nilable(T::Hash[Symbol, PayloadValue]))
  @default_file_list = T.let(normalize_file_list(default_file_list), T::Array[HakumiComponents::Upload::FileEntry])
  @with_credentials = T.let(cast_boolean(with_credentials) == true, T::Boolean)
  @auto_upload = T.let(cast_boolean(auto_upload) != false, T::Boolean)
  @inline_trigger = T.let(cast_boolean(inline_trigger) == true, T::Boolean)
  @required = T.let(cast_boolean(required) == true, T::Boolean)
  @standalone = T.let(cast_boolean(standalone) != false, T::Boolean)
  @rules = T.let(rules, T.nilable(T::Array[Types::ValidationRule]))
  @errors = T.let(errors, Types::FormFieldErrors)
  @avatar_shape = avatar_shape
  @avatar_size = avatar_size
  @avatar_icon = avatar_icon
  @preview_label = preview_label
  @remove_label = remove_label
  @close_label = close_label
  @previous_label = previous_label
  @next_label = next_label
  @html_options = T.let(html_options, Types::HtmlAttributes)
  initialize_form_field_contract!(
    name: @name,
    label: @label,
    caption: @caption,
    errors: @errors,
    standalone: @standalone,
    required: @required,
    html_options: @html_options,
    rules: @rules
  )

  validate_props!
end

Instance Attribute Details

#avatar_iconObject (readonly)

Returns the value of attribute avatar_icon.



258
259
260
# File 'app/components/hakumi_components/upload/component.rb', line 258

def avatar_icon
  @avatar_icon
end

#avatar_shapeObject (readonly)

Returns the value of attribute avatar_shape.



264
265
266
# File 'app/components/hakumi_components/upload/component.rb', line 264

def avatar_shape
  @avatar_shape
end

#avatar_sizeObject (readonly)

Returns the value of attribute avatar_size.



261
262
263
# File 'app/components/hakumi_components/upload/component.rb', line 261

def avatar_size
  @avatar_size
end

Instance Method Details

#action_labelObject



188
189
190
# File 'app/components/hakumi_components/upload/component.rb', line 188

def action_label
  content.presence || "Upload"
end

#avatar_overlay_classObject



253
254
255
# File 'app/components/hakumi_components/upload/component.rb', line 253

def avatar_overlay_class
  @avatar_shape == :circle ? "hakumi-upload-avatar-overlay--circle" : "hakumi-upload-avatar-overlay--square"
end

#close_label_textObject



233
234
235
# File 'app/components/hakumi_components/upload/component.rb', line 233

def close_label_text
  @close_label || t_default(:close, default: "Close")
end

#disabled?Boolean

Returns:

  • (Boolean)


267
268
269
# File 'app/components/hakumi_components/upload/component.rb', line 267

def disabled?
  @disabled
end

#drag?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'app/components/hakumi_components/upload/component.rb', line 272

def drag?
  @drag
end

#file_item_classes(file) ⇒ Object



155
156
157
158
159
# File 'app/components/hakumi_components/upload/component.rb', line 155

def file_item_classes(file)
  classes = [ "hakumi-upload-list-item", "hakumi-upload-list-item-#{file.status}" ]
  classes << "hakumi-upload-list-item-disabled" if disabled?
  classes.join(" ")
end

#inline_trigger?Boolean

Returns:

  • (Boolean)


287
288
289
# File 'app/components/hakumi_components/upload/component.rb', line 287

def inline_trigger?
  @inline_trigger
end

#input_attributesObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'app/components/hakumi_components/upload/component.rb', line 298

def input_attributes
  {
    type: "file",
    id: input_id,
    name: form_field_name,
    accept: @accept,
    multiple: @multiple ? true : nil,
    disabled: @disabled ? true : nil,
    "aria-invalid": error? ? "true" : nil,
    "aria-describedby": describedby_ids,
    data: {
      action: "change->hakumi--upload#handleInput",
      hakumi__upload_target: "input"
    }
  }.compact
end

#list_type_avatar?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'app/components/hakumi_components/upload/component.rb', line 248

def list_type_avatar?
  @list_type == :avatar
end

#list_type_classObject



167
168
169
# File 'app/components/hakumi_components/upload/component.rb', line 167

def list_type_class
  @list_type.to_s.tr("_", "-")
end

#list_type_picture?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'app/components/hakumi_components/upload/component.rb', line 162

def list_type_picture?
  %i[picture picture_card].include?(@list_type)
end

#next_label_textObject



243
244
245
# File 'app/components/hakumi_components/upload/component.rb', line 243

def next_label_text
  @next_label || t_default(:next, default: "Next")
end

#normalized_file_listObject



183
184
185
# File 'app/components/hakumi_components/upload/component.rb', line 183

def normalized_file_list
  @default_file_list
end

#picture_card?Boolean

Returns:

  • (Boolean)


282
283
284
# File 'app/components/hakumi_components/upload/component.rb', line 282

def picture_card?
  @list_type == :picture_card
end

#preview_label_textObject



223
224
225
# File 'app/components/hakumi_components/upload/component.rb', line 223

def preview_label_text
  @preview_label || t_default(:preview, default: "Preview")
end

#previous_label_textObject



238
239
240
# File 'app/components/hakumi_components/upload/component.rb', line 238

def previous_label_text
  @previous_label || t_default(:previous, default: "Previous")
end

#progress_percent(file) ⇒ Object



172
173
174
# File 'app/components/hakumi_components/upload/component.rb', line 172

def progress_percent(file)
  file.percent
end

#progress_style(file) ⇒ Object



177
178
179
180
# File 'app/components/hakumi_components/upload/component.rb', line 177

def progress_style(file)
  percent = progress_percent(file)
  percent.positive? ? "--hakumi-upload-progress: #{percent}%" : nil
end

#remove_label_textObject



228
229
230
# File 'app/components/hakumi_components/upload/component.rb', line 228

def remove_label_text
  @remove_label || t_default(:remove, default: "Remove")
end

#show_upload_list?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'app/components/hakumi_components/upload/component.rb', line 277

def show_upload_list?
  @show_upload_list
end

#stimulus_valuesObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/components/hakumi_components/upload/component.rb', line 193

def stimulus_values
  {
    action: @action,
    name: form_field_name.to_s,
    accept: @accept,
    multiple: @multiple,
    maxCount: @max_count,
    maxSize: @max_size,
    disabled: @disabled,
    listType: list_type_class,
    showUploadList: @show_upload_list,
    drag: @drag,
    headers: @headers,
    data: @data,
    defaultFileList: normalized_file_list.map(&:to_h),
    withCredentials: @with_credentials,
    autoUpload: @auto_upload,
    inlineTrigger: @inline_trigger,
    avatarShape: @avatar_shape,
    avatarSize: @avatar_size,
    avatarIcon: @avatar_icon,
    previewLabel: preview_label_text,
    removeLabel: remove_label_text,
    closeLabel: close_label_text,
    previousLabel: previous_label_text,
    nextLabel: next_label_text
  }
end

#upload_list_classesObject



148
149
150
151
152
# File 'app/components/hakumi_components/upload/component.rb', line 148

def upload_list_classes
  list_classes = [ "hakumi-upload-list", "hakumi-upload-list-#{list_type_class}" ]
  list_classes << "hakumi-upload-list-empty" if normalized_file_list.empty?
  list_classes.compact.join(" ")
end

#wrapper_attributesObject



292
293
294
295
# File 'app/components/hakumi_components/upload/component.rb', line 292

def wrapper_attributes
  data = stimulus_attrs("hakumi--upload", stimulus_values)
  merge_attributes({ class: wrapper_classes, data: data }, form_field_html_options.except(:class))
end

#wrapper_classesObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/components/hakumi_components/upload/component.rb', line 134

def wrapper_classes
  html_class = form_field_html_options[:class]
  class_names(
    "upload",
    {
      disabled: @disabled,
      drag: @drag,
      list: list_type_class
    },
    html_class.is_a?(String) ? [ html_class ] : []
  )
end