Class: ViewComponent::InputComponent::FileSelectorComponent

Inherits:
Object
  • Object
show all
Includes:
ComponentHelper
Defined in:
app/helpers/view_component/input_component/file_selector_component.rb

Constant Summary collapse

FILE_SELECTOR_TYPES =
%w[image document video].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComponentHelper

#class_list, #resolve_error

Constructor Details

#initialize(type:, form: nil, name: nil, label: nil, support_text: nil, support_text_two: nil, error: nil, disabled: false, html_options: {}) ⇒ FileSelectorComponent

Returns a new instance of FileSelectorComponent.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 13

def initialize(type:, form: nil, name: nil, label: nil,
               support_text: nil, support_text_two: nil, error: nil, disabled: false, html_options: {})
  raise "Invalid or missing file type: #{type}" unless FILE_SELECTOR_TYPES.include?(type)

  error_message = resolve_error(form, name, error)

  self.form = form
  self.name = name
  self.label = label
  self.support_text = (error_message.presence || support_text)
  self.support_text_two = support_text_two
  self.error = error_message
  self.disabled = disabled
  self.html_options = html_options
  self.type = type
end

Instance Attribute Details

#disabledObject

Returns the value of attribute disabled.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def disabled
  @disabled
end

#errorObject

Returns the value of attribute error.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def error
  @error
end

#formObject

Returns the value of attribute form.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def form
  @form
end

#html_optionsObject

Returns the value of attribute html_options.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def html_options
  @html_options
end

#labelObject

Returns the value of attribute label.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def label
  @label
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def name
  @name
end

#support_textObject

Returns the value of attribute support_text.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def support_text
  @support_text
end

#support_text_twoObject

Returns the value of attribute support_text_two.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def support_text_two
  @support_text_two
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 10

def type
  @type
end

Instance Method Details

#accepted_typesObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 30

def accepted_types
  case type
  when 'image'
    'image/*'
  when 'document'
    '.csv,.pdf,.doc,.docx'
  when 'video'
    'video/*,.mp4,.mov,.avi,.mkv'
  else
    '*/*'
  end
end

#choose_file_styleObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 94

def choose_file_style
  base = ['general-text-md-normal ']

  color_style =
    if disabled
      'text-disabled-color'
    else
      'text-primary'
    end

  class_list(base, color_style)
end

#label_styleObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 58

def label_style
  base = ['general-text-sm-normal px-1']

  color_style =
    if disabled
      'text-disabled-color'
    elsif error.present?
      'text-danger-dark'
    else
      'text-letter-color-light'
    end

  class_list(base, color_style)
end

#preview_styleObject



73
74
75
76
77
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 73

def preview_style
  base = ['file-selector-preview']

  class_list(base)
end

#support_text_styleObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 107

def support_text_style
  base = ['file-selector-support-text general-text-sm-normal']

  color_style =
    if disabled
      'text-disabled-color'
    elsif error.present?
      'text-danger-dark'
    else
      'text-slate-grey-50'
    end

  class_list(base, color_style)
end

#upload_icon_styleObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 79

def upload_icon_style
  base = ['file-selector-upload-icon']

  color_style =
    if disabled
      'fill-disabled-color'
    elsif error.present?
      'fill-danger'
    else
      'fill-primary'
    end

  class_list(base, color_style)
end

#wrapper_styleObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/view_component/input_component/file_selector_component.rb', line 43

def wrapper_style
  base = ['file-selector-component-base radius-lg md:radius-xl']

  color_style =
    if disabled
      'textarea-disabled-state pointer-events-none cursor-not-allowed'
    elsif error.present?
      'border-danger'
    else
      'border-slate-grey-50 hover:border-primary-light focus:border-primary-light'
    end

  class_list(base, color_style)
end