Class: GovukPublishingComponents::Presenters::ButtonHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/button_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_assigns) ⇒ ButtonHelper



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
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 30

def initialize(local_assigns)
  @disable_ga4 = local_assigns[:disable_ga4]
  @href = local_assigns[:href]
  @text = local_assigns[:text]
  @title = local_assigns[:title]
  @info_text = local_assigns[:info_text]
  @info_text_classes = %w[gem-c-button__info-text]
  if local_assigns[:margin_bottom]
    margin_class = get_margin_bottom(local_assigns[:margin_bottom], true)
    @info_text_classes << margin_class
  end
  @rel = local_assigns[:rel]
  @data_attributes = local_assigns[:data_attributes]&.symbolize_keys || {}
  @data_attributes[:module] = "govuk-button #{data_attributes[:module]}".strip if link?
  @margin_bottom = local_assigns[:margin_bottom]
  @inline_layout = local_assigns[:inline_layout]
  @target = local_assigns[:target]
  @type = local_assigns[:type]
  @start = local_assigns[:start]
  @data_attributes[:ga4_attributes] = ga4_attribute if start
  @secondary = local_assigns[:secondary]
  @secondary_quiet = local_assigns[:secondary_quiet]
  @secondary_solid = local_assigns[:secondary_solid]
  @destructive = local_assigns[:destructive]
  @name = local_assigns[:name]
  @value = local_assigns[:value]
  @classes = local_assigns[:classes]
  @aria_label = local_assigns[:aria_label]
  @info_text_id = "info-text-id-#{SecureRandom.hex(4)}"
  @button_id = "button-id-#{SecureRandom.hex(4)}"
  @aria_controls = local_assigns[:aria_controls]
  @aria_describedby = local_assigns[:aria_describedby]
end

Instance Attribute Details

#aria_controlsObject (readonly)

Returns the value of attribute aria_controls.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def aria_controls
  @aria_controls
end

#aria_describedbyObject (readonly)

Returns the value of attribute aria_describedby.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def aria_describedby
  @aria_describedby
end

#aria_labelObject (readonly)

Returns the value of attribute aria_label.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def aria_label
  @aria_label
end

#classesObject (readonly)

Returns the value of attribute classes.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def classes
  @classes
end

#data_attributesObject (readonly)

Returns the value of attribute data_attributes.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def data_attributes
  @data_attributes
end

#destructiveObject (readonly)

Returns the value of attribute destructive.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def destructive
  @destructive
end

#hrefObject (readonly)

Returns the value of attribute href.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def href
  @href
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def id
  @id
end

#info_textObject (readonly)

Returns the value of attribute info_text.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def info_text
  @info_text
end

#info_text_classesObject (readonly)

Returns the value of attribute info_text_classes.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def info_text_classes
  @info_text_classes
end

#inline_layoutObject (readonly)

Returns the value of attribute inline_layout.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def inline_layout
  @inline_layout
end

#margin_bottomObject (readonly)

Returns the value of attribute margin_bottom.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def margin_bottom
  @margin_bottom
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def name
  @name
end

#relObject (readonly)

Returns the value of attribute rel.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def rel
  @rel
end

#secondaryObject (readonly)

Returns the value of attribute secondary.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def secondary
  @secondary
end

#secondary_quietObject (readonly)

Returns the value of attribute secondary_quiet.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def secondary_quiet
  @secondary_quiet
end

#secondary_solidObject (readonly)

Returns the value of attribute secondary_solid.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def secondary_solid
  @secondary_solid
end

#startObject (readonly)

Returns the value of attribute start.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def start
  @start
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def target
  @target
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def text
  @text
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 6

def value
  @value
end

Instance Method Details

#aria_labelledbyObject



72
73
74
75
76
77
78
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 72

def aria_labelledby
  if info_text?
    text = "#{@button_id} "
    text << @info_text_id
    text
  end
end

#button_typeObject



106
107
108
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 106

def button_type
  type || "submit" unless link?
end

#html_optionsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 87

def html_options
  options = { class: css_classes }
  options[:role] = "button" if link?
  options[:type] = button_type
  options[:id] = @button_id if info_text?
  options[:aria] = { labelledby: aria_labelledby }
  options[:rel] = rel if rel
  options[:data] = data_attributes if data_attributes
  options[:title] = title if title
  options[:target] = target if target
  options[:name] = name if name.present? && value.present?
  options[:value] = value if name.present? && value.present?
  options[:aria] = { label: aria_label } if aria_label
  options[:aria][:controls] =  aria_controls if aria_controls
  options[:aria][:describedby] = aria_describedby if aria_describedby
  options[:draggable] = false if link?
  options
end

#info_text?Boolean



68
69
70
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 68

def info_text?
  info_text.present?
end

#info_text_optionsObject



80
81
82
83
84
85
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 80

def info_text_options
  options = { class: info_text_classes }
  options[:aria] = { hidden: true } if info_text?
  options[:id] = @info_text_id if info_text?
  options
end

#link?Boolean



64
65
66
# File 'lib/govuk_publishing_components/presenters/button_helper.rb', line 64

def link?
  href.present?
end