Module: GovukDesignSystem::FileUploadHelper

Defined in:
app/helpers/govuk_design_system/file_upload_helper.rb

Instance Method Summary collapse

Instance Method Details

#govukFileUpload(id:, name:, label:, value: nil, hint: nil, errorMessage: nil, formGroup: {}, classes: "", attributes: {}, described_by: nil) ⇒ Object

Use the [file upload component](design-system.service.gov.uk/components/file-upload/) to help users select and upload a file.

Code based upon [nunjucks template](github.com/alphagov/govuk-frontend/blob/master/src/govuk/components/file-upload/template.njk)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
# File 'app/helpers/govuk_design_system/file_upload_helper.rb', line 7

def govukFileUpload(id:, name:, label:, value: nil, hint: nil, errorMessage: nil, formGroup: {}, classes: "", attributes: {}, described_by: nil)
  form_group_classes = "govuk-form-group"
  form_group_classes += " govuk-form-group--error" if errorMessage
  form_group_classes += " #{formGroup[:classes]}" if formGroup[:classes]

  # a record of other elements that we need to associate with the input using
  # aria-describedby – for example hints or error messages
  described_by ||= ""

  ("div", class: form_group_classes) do
    concat govukLabel(html: label[:html], text: label[:text], classes: label[:classes], isPageHeading: label[:isPageHeading], attributes: (label[:attributes] || {}), for: id)

    if hint
      hint_id = "#{id}-hint"
      described_by += " #{hint_id}"

      concat govukHint(
        id: hint_id,
        classes: hint[:classes],
        attributes: hint[:attributes],
        html: hint[:html],
        text: hint[:text]
      )
    end

    if errorMessage
      error_id = "#{id}-error"
      described_by += " #{error_id}"

      concat govukErrorMessage(
        id: error_id,
        classes: errorMessage[:classes],
        attributes: errorMessage[:attributes],
        html: errorMessage[:html],
        text: errorMessage[:text],
        visuallyHiddenText: errorMessage[:visuallyHiddenText]
      )
    end

    attributes[:class] = "govuk-file-upload"
    attributes[:class] += " govuk-file-upload--error" if errorMessage
    attributes[:class] += " #{classes}" if classes
    attributes[:id] = id
    attributes[:name] = name
    attributes[:type] = "file"
    attributes[:value] = value if value
    attributes["aria-describedby"] = described_by if described_by

    concat ("input", nil, attributes)
  end
end