Class: CCS::Components::GovUK::Button

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/button.rb

Overview

GOV.UK Button

This is used to generate the button component from the GDS - Components - Button

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the button

{ class: 'govuk-button', data: { module: 'govuk-button' } }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text:, **options) ⇒ Button

Returns a new instance of Button.

Parameters:

  • text (String)

    the text that will be shown in the button

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the button HTML

  • :is_start_button (Boolean)

    indicates if it is a start button

  • :href (String)

    the URI that will be used in anchor tag

  • :form (ActionView::Helpers::FormBuilder)

    the form builder used to create the submit button

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ccs/components/govuk/button.rb', line 32

def initialize(text:, **options)
  super(**options)

  if @options[:attributes][:disabled]
    @options[:attributes][:aria] ||= {}
    @options[:attributes][:aria][:disabled] = true
  end
  @options[:attributes][:class] << ' govuk-button--start' if @options[:is_start_button]

  @text = text
  @render_method = :"render_#{if @options[:href]
                                :link
                              elsif @options[:form]
                                :submit
                              else
                                :button
                              end}"
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Back link component

Returns:

  • (ActiveSupport::SafeBuffer)


55
56
57
# File 'lib/ccs/components/govuk/button.rb', line 55

def render
  send(@render_method)
end