Class: CCS::Components::GovUK::Field::Input::PasswordInput::ShowHideButton

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers, ActionView::Helpers::FormTagHelper
Defined in:
lib/ccs/components/govuk/field/input/password_input/show_hide_button.rb

Overview

GOV.UK Password input show/hide button

This is used to generate the password input show/hide button

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, context:, button: {}, after_input: nil, **options) ⇒ ShowHideButton

Returns a new instance of ShowHideButton.

Parameters:

  • attribute (String)

    the name of the field as it will appear in the input

  • context (ActionView::Base)

    the view context

  • button (Hash) (defaults to: {})

    options for the button

  • after_input (String) (defaults to: nil)

    Text or HTML that goes after the input

Options Hash (button:):

  • :classes (String)

    classes to add to the button



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ccs/components/govuk/field/input/password_input/show_hide_button.rb', line 38

def initialize(attribute:, context:, button: {}, after_input: nil, **options)
  button[:classes] = "govuk-button--secondary govuk-password-input__toggle govuk-js-password-input-toggle #{button[:classes]}".rstrip
  button[:attributes] = {
    type: :button,
    aria: {
      controls: options.dig(:attributes, :id) || field_id(options[:form]&.object_name, attribute),
      label: options[:show_password_aria_label_text] || 'Show password'
    },
    hidden: {
      value: true,
      optional: true
    }
  }

  @show_hide_button = Button.new(text: options[:show_password_text] || 'Show', context: context, classes: button[:classes], attributes: button[:attributes])
  @after_input = after_input
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Password input show/hide button

Returns:

  • (ActiveSupport::SafeBuffer)


60
61
62
63
64
65
# File 'lib/ccs/components/govuk/field/input/password_input/show_hide_button.rb', line 60

def render
  capture do
    concat(show_hide_button.render)
    concat(after_input) if after_input
  end
end