Class: CCS::Components::GovUK::Field::Input::FileUpload

Inherits:
CCS::Components::GovUK::Field::Input show all
Defined in:
lib/ccs/components/govuk/field/input/file_upload.rb

Overview

GOV.UK File Upload

This is used for generating the file upload component from the GDS - Components - File Upload

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the file upload

{ class: 'govuk-file-upload' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, javascript: false) ⇒ FileUpload

Returns a new instance of FileUpload.

Parameters:

  • javascript (Boolean) (defaults to: false)

    used to enable JavaScript enhancements for the component



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ccs/components/govuk/field/input/file_upload.rb', line 30

def initialize(attribute:, javascript: false, **)
  super(attribute: attribute, **)

  @javascript = javascript

  return unless javascript

  @file_upload_javascript_options = {
    class: 'govuk-drop-zone',
    data: {
      module: 'govuk-file-upload'
    }
  }

  %i[choose_files_button no_file_chosen multiple_files_chosen drop_instruction entered_drop_zone left_drop_zone].each do |data_attribute|
    data_attribute_name = :"#{data_attribute}_text"
    if options[data_attribute_name]
      if options[data_attribute_name].is_a? Hash
        options[data_attribute_name].each do |key, value|
          @file_upload_javascript_options[:data][:"i18n.#{data_attribute.to_s.gsub('_', '-')}.#{key}"] = value
        end
      else
        @file_upload_javascript_options[:data][:"i18n.#{data_attribute.to_s.gsub('_', '-')}"] = options[data_attribute_name]
      end
    end
  end
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK File Upload component

Returns:

  • (ActiveSupport::SafeBuffer)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ccs/components/govuk/field/input/file_upload.rb', line 66

def render
  super do
    javascript_wrapper do
      if options[:form]
        options[:form].file_field(attribute, **options[:attributes])
      else
        context.file_field_tag(attribute, **options[:attributes])
      end
    end
  end
end