Module: GovukDesignSystem::ButtonHelper

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

Instance Method Summary collapse

Instance Method Details

#govukButton(element: nil, text: nil, html: nil, name: nil, type: nil, value: nil, disabled: nil, href: nil, classes: "", attributes: {}, preventDoubleClick: nil, isStartButton: nil) ⇒ Object



4
5
6
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
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/govuk_design_system/button_helper.rb', line 4

def govukButton(element: nil, text: nil, html: nil, name: nil, type: nil, value: nil, disabled: nil, href: nil, classes: "", attributes: {}, preventDoubleClick: nil, isStartButton: nil)
  # Set classes for this component
  attributes[:class] = class_names("govuk-button", classes, { "govuk-button--disabled" => disabled })
  attributes[:data] ||= {}
  attributes[:data][:module] ||= "govuk-button"

  # Element should be lower-case if set
  element&.downcase!

  # Determine type of element to use, if not explicitly set
  element ||= (href ? "a" : "button")

  if isStartButton
    iconHtml = '<svg class="govuk-button__start-icon" xmlns="http://www.w3.org/2000/svg" width="17.5" height="19" viewBox="0 0 33 40" aria-hidden="true" focusable="false"><path fill="currentColor" d="M0 0h13l20 20-20 20H0l20-20z"/></svg>'.html_safe
    attributes[:class] += " govuk-button--start"
  end

  # Define common attributes we can use for both button and input types

  button_attributes = {}

  if name
    button_attributes[:name] = name
  end

  if disabled
    button_attributes[:disabled] = "disabled"
    button_attributes["aria-disabled"] = "true"
  end

  if preventDoubleClick
    button_attributes["data-prevent-double-click"] = "true"
  end

  # Actually create a button... or a link!
  case element
  when "a"

    attributes[:role] = "button"
    attributes[:draggable] = "false"

    link_to (href || "#"), attributes do
      concat(html || text)
      concat iconHtml.to_s
    end

  when "button"
    attributes[:value] = value if value
    attributes[:type] = type if type

    attributes.merge!(button_attributes)

    tag.button(**attributes) do
      concat(html || text)
      concat iconHtml.to_s
    end

  when "input"
    attributes[:value] = text
    attributes[:type] = type || "submit"

    attributes.merge!(button_attributes)

     "input", attributes
  end
end