Class: DesignSystem::Nhsuk::FormBuilder

Inherits:
Govuk::FormBuilder show all
Defined in:
lib/design_system/nhsuk/form_builder.rb

Overview

The NHS version of the form builder

Instance Method Summary collapse

Methods inherited from Govuk::FormBuilder

#ds_check_box, #ds_check_boxes_fieldset, #ds_collection_check_boxes, #ds_collection_radio_buttons, #ds_collection_select, #ds_date_field, #ds_email_field, #ds_error_summary, #ds_field_set_tag, #ds_file_field, #ds_label, #ds_number_field, #ds_phone_field, #ds_radio_button, #ds_radio_buttons_fieldset, #ds_select, #ds_submit, #ds_text_area, #ds_text_field, #ds_url_field, #initialize

Methods inherited from Generic::FormBuilder

brand

Methods included from Helpers::CssHelper

#css_class_options_merge

Constructor Details

This class inherits a constructor from DesignSystem::Govuk::FormBuilder

Instance Method Details

#ds_hidden_field(method, options = {}) ⇒ Object

Same interface as ActionView::Helpers::FormHelper.hidden_field, but with label automatically added and takes a show_text option



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/design_system/nhsuk/form_builder.rb', line 7

def ds_hidden_field(method, options = {})
  @brand = config.brand

  options[:class] = Array(options[:class])
  options[:class] << "#{@brand}-u-visually-hidden"

  label_hash = options.delete(:label) || {}
  label = ds_label(method, label_hash)
  show_text = options.delete(:show_text)

  (:div, class: "#{@brand}-form-group") do
    components = []
    components << label if label
    components << hidden_field(method, **options)
    components << (:span, show_text, class: "#{@brand}-body-m") if show_text

    safe_join(components)
  end
end

#ds_password_field(method, options = {}) ⇒ Object



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
# File 'lib/design_system/nhsuk/form_builder.rb', line 27

def ds_password_field(method, options = {})
  @brand = config.brand
  options[:id] = govuk_field_id(method, link_errors: true)
  password_field_options = css_class_options_merge(options,
                                                   ["#{@brand}-input",
                                                    has_errors?(method) ? "#{@brand}-input--error" : nil].compact)

  hint = options.delete(:hint)
  password_field_options[:'aria-describedby'] = field_id("#{method}-hint") if hint
  password_field_options[:autocomplete] = 'current-password'
  password_field_options['data-ds--show-password-target'] = 'password'
  password_field_options.delete('text')

  form_group_classes = ["#{@brand}-form-group"]
  form_group_classes << "#{@brand}-form-group--error" if has_errors?(method)

  (:div,
              class: form_group_classes.join(' '),
              data: {
                controller: 'ds--show-password',
                'ds--show-password-show-text-value': 'Show password',
                'ds--show-password-hide-text-value': 'Hide password'
              }) do
    ds_label(method, {}) +
      optional_hint(method, hint) +
      password_field(method, password_field_options) + '&nbsp;'.html_safe +
      show_password_button
  end
end

#show_password_buttonObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/design_system/nhsuk/form_builder.rb', line 57

def show_password_button
  button_options = {
    type: 'button',
    class: "#{@brand}-button #{@brand}-button--secondary",
    style: 'padding: 8px 10px 7px; margin-bottom: 0px !important',
    aria: { label: 'Show password' },
    data: {
      'module' => "#{@brand}-button",
      'action' => 'click->ds--show-password#toggle',
      'ds--show-password-target' => 'button'
    }
  }
  (:button, 'Show password', button_options)
end