ccs-frontend_helpers 0.1.0.rc.1 → 0.1.0.rc.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -1
  3. data/.ruby-version +1 -0
  4. data/CHANGELOG.md +4 -1
  5. data/Gemfile.lock +71 -71
  6. data/lib/ccs/components/base.rb +64 -0
  7. data/lib/ccs/components/ccs/dashboard_section/panel.rb +55 -0
  8. data/lib/ccs/components/ccs/dashboard_section.rb +71 -0
  9. data/lib/ccs/components/ccs/footer/link.rb +53 -0
  10. data/lib/ccs/components/ccs/footer/meta.rb +58 -0
  11. data/lib/ccs/components/ccs/footer/navigation.rb +59 -0
  12. data/lib/ccs/components/ccs/footer.rb +93 -0
  13. data/lib/ccs/components/ccs/header/link.rb +58 -0
  14. data/lib/ccs/components/ccs/header/navigation.rb +97 -0
  15. data/lib/ccs/components/ccs/header/service_authentication.rb +52 -0
  16. data/lib/ccs/components/ccs/header.rb +108 -0
  17. data/lib/ccs/components/ccs/logo.rb +45 -0
  18. data/lib/ccs/components/govuk/accordion/section/content.rb +53 -0
  19. data/lib/ccs/components/govuk/accordion/section/header.rb +57 -0
  20. data/lib/ccs/components/govuk/accordion/section.rb +60 -0
  21. data/lib/ccs/components/govuk/accordion.rb +56 -0
  22. data/lib/ccs/components/govuk/back_link.rb +49 -0
  23. data/lib/ccs/components/govuk/breadcrumbs/link.rb +55 -0
  24. data/lib/ccs/components/govuk/breadcrumbs.rb +53 -0
  25. data/lib/ccs/components/govuk/button.rb +105 -0
  26. data/lib/ccs/components/govuk/cookie_banner/action.rb +58 -0
  27. data/lib/ccs/components/govuk/cookie_banner/message.rb +78 -0
  28. data/lib/ccs/components/govuk/cookie_banner.rb +53 -0
  29. data/lib/ccs/components/govuk/details.rb +51 -0
  30. data/lib/ccs/components/govuk/error_message.rb +58 -0
  31. data/lib/ccs/components/govuk/error_summary/item.rb +52 -0
  32. data/lib/ccs/components/govuk/error_summary.rb +68 -0
  33. data/lib/ccs/components/govuk/field/input/character_count.rb +127 -0
  34. data/lib/ccs/components/govuk/field/input/file_upload.rb +42 -0
  35. data/lib/ccs/components/govuk/field/input/select.rb +80 -0
  36. data/lib/ccs/components/govuk/field/input/text_input/fix.rb +53 -0
  37. data/lib/ccs/components/govuk/field/input/text_input.rb +95 -0
  38. data/lib/ccs/components/govuk/field/input/textarea.rb +57 -0
  39. data/lib/ccs/components/govuk/field/input.rb +60 -0
  40. data/lib/ccs/components/govuk/field/inputs/checkboxes.rb +67 -0
  41. data/lib/ccs/components/govuk/field/inputs/date_input/item.rb +63 -0
  42. data/lib/ccs/components/govuk/field/inputs/date_input.rb +87 -0
  43. data/lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb +45 -0
  44. data/lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb +41 -0
  45. data/lib/ccs/components/govuk/field/inputs/item/checkbox.rb +48 -0
  46. data/lib/ccs/components/govuk/field/inputs/item/divider.rb +48 -0
  47. data/lib/ccs/components/govuk/field/inputs/item/radio/form.rb +42 -0
  48. data/lib/ccs/components/govuk/field/inputs/item/radio/tag.rb +41 -0
  49. data/lib/ccs/components/govuk/field/inputs/item/radio.rb +48 -0
  50. data/lib/ccs/components/govuk/field/inputs/item.rb +109 -0
  51. data/lib/ccs/components/govuk/field/inputs/radios.rb +67 -0
  52. data/lib/ccs/components/govuk/field/inputs.rb +55 -0
  53. data/lib/ccs/components/govuk/field.rb +106 -0
  54. data/lib/ccs/components/govuk/fieldset/legend.rb +63 -0
  55. data/lib/ccs/components/govuk/fieldset.rb +52 -0
  56. data/lib/ccs/components/govuk/footer/link.rb +53 -0
  57. data/lib/ccs/components/govuk/footer/meta.rb +58 -0
  58. data/lib/ccs/components/govuk/footer/navigation.rb +59 -0
  59. data/lib/ccs/components/govuk/footer.rb +129 -0
  60. data/lib/ccs/components/govuk/form_group.rb +58 -0
  61. data/lib/ccs/components/govuk/header/link.rb +54 -0
  62. data/lib/ccs/components/govuk/header/navigation.rb +70 -0
  63. data/lib/ccs/components/govuk/header.rb +105 -0
  64. data/lib/ccs/components/govuk/hint.rb +47 -0
  65. data/lib/ccs/components/govuk/inset_text.rb +50 -0
  66. data/lib/ccs/components/govuk/label.rb +83 -0
  67. data/lib/ccs/components/govuk/notification_banner.rb +105 -0
  68. data/lib/ccs/components/govuk/pagination/increment/next.rb +42 -0
  69. data/lib/ccs/components/govuk/pagination/increment/previous.rb +41 -0
  70. data/lib/ccs/components/govuk/pagination/increment.rb +96 -0
  71. data/lib/ccs/components/govuk/pagination/item/ellipsis.rb +26 -0
  72. data/lib/ccs/components/govuk/pagination/item/form.rb +47 -0
  73. data/lib/ccs/components/govuk/pagination/item/tag.rb +45 -0
  74. data/lib/ccs/components/govuk/pagination/item.rb +62 -0
  75. data/lib/ccs/components/govuk/pagination.rb +94 -0
  76. data/lib/ccs/components/govuk/panel.rb +60 -0
  77. data/lib/ccs/components/govuk/phase_banner.rb +67 -0
  78. data/lib/ccs/components/govuk/skip_link.rb +49 -0
  79. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list/item.rb +51 -0
  80. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list.rb +48 -0
  81. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/paragraph.rb +43 -0
  82. data/lib/ccs/components/govuk/step_by_step_navigation/section/content.rb +61 -0
  83. data/lib/ccs/components/govuk/step_by_step_navigation/section/heading.rb +67 -0
  84. data/lib/ccs/components/govuk/step_by_step_navigation/section.rb +54 -0
  85. data/lib/ccs/components/govuk/step_by_step_navigation.rb +57 -0
  86. data/lib/ccs/components/govuk/summary_list/action/link.rb +57 -0
  87. data/lib/ccs/components/govuk/summary_list/card/actions.rb +57 -0
  88. data/lib/ccs/components/govuk/summary_list/card/title.rb +49 -0
  89. data/lib/ccs/components/govuk/summary_list/card.rb +61 -0
  90. data/lib/ccs/components/govuk/summary_list/row/actions.rb +57 -0
  91. data/lib/ccs/components/govuk/summary_list/row/key.rb +45 -0
  92. data/lib/ccs/components/govuk/summary_list/row/value.rb +45 -0
  93. data/lib/ccs/components/govuk/summary_list/row.rb +63 -0
  94. data/lib/ccs/components/govuk/summary_list.rb +72 -0
  95. data/lib/ccs/components/govuk/table/body/data_cell.rb +51 -0
  96. data/lib/ccs/components/govuk/table/body/head_cell.rb +50 -0
  97. data/lib/ccs/components/govuk/table/header/head_cell.rb +52 -0
  98. data/lib/ccs/components/govuk/table.rb +109 -0
  99. data/lib/ccs/components/govuk/tabs/panel.rb +56 -0
  100. data/lib/ccs/components/govuk/tabs/tab.rb +54 -0
  101. data/lib/ccs/components/govuk/tabs.rb +64 -0
  102. data/lib/ccs/components/govuk/tag.rb +49 -0
  103. data/lib/ccs/components/govuk/warning_text.rb +59 -0
  104. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_section.rb +25 -0
  105. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +16 -131
  106. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +16 -195
  107. data/lib/ccs/frontend_helpers/ccs_frontend.rb +2 -4
  108. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +16 -105
  109. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +16 -29
  110. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +16 -66
  111. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +16 -117
  112. data/lib/ccs/frontend_helpers/govuk_frontend/character_count.rb +26 -0
  113. data/lib/ccs/frontend_helpers/govuk_frontend/checkboxes.rb +26 -0
  114. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +16 -126
  115. data/lib/ccs/frontend_helpers/govuk_frontend/date_input.rb +26 -0
  116. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +18 -36
  117. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +31 -54
  118. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +32 -86
  119. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +18 -61
  120. data/lib/ccs/frontend_helpers/govuk_frontend/file_upload.rb +26 -0
  121. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +16 -173
  122. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +26 -39
  123. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +16 -151
  124. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +17 -28
  125. data/lib/ccs/frontend_helpers/govuk_frontend/input.rb +26 -0
  126. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +18 -34
  127. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +17 -82
  128. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +18 -126
  129. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +16 -326
  130. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +18 -41
  131. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +18 -39
  132. data/lib/ccs/frontend_helpers/govuk_frontend/radios.rb +26 -0
  133. data/lib/ccs/frontend_helpers/govuk_frontend/select.rb +26 -0
  134. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +16 -30
  135. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +23 -204
  136. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +16 -216
  137. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +16 -114
  138. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +16 -85
  139. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +16 -32
  140. data/lib/ccs/frontend_helpers/govuk_frontend/textarea.rb +26 -0
  141. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +18 -43
  142. data/lib/ccs/frontend_helpers/govuk_frontend.rb +17 -19
  143. data/lib/ccs/frontend_helpers/version.rb +1 -1
  144. metadata +110 -14
  145. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +0 -79
  146. data/lib/ccs/frontend_helpers/ccs_frontend/logo.rb +0 -49
  147. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +0 -165
  148. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +0 -200
  149. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +0 -153
  150. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +0 -83
  151. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +0 -153
  152. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +0 -201
  153. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +0 -124
  154. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +0 -106
  155. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +0 -213
  156. data/lib/ccs/frontend_helpers/shared_methods.rb +0 -27
@@ -0,0 +1,63 @@
1
+ require_relative '../date_input'
2
+ require_relative '../../input/text_input'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Inputs < Field
8
+ class DateInput < Inputs
9
+ # = GOV.UK Date Inputs item
10
+ #
11
+ # This is used to generate an individual date input item
12
+ #
13
+ # @!attribute [r] input
14
+ # @return [TextInput] The initialised text input for the date item
15
+
16
+ class Item
17
+ include ActionView::Context
18
+ include ActionView::Helpers
19
+
20
+ private
21
+
22
+ attr_reader :input
23
+
24
+ public
25
+
26
+ # rubocop:disable Metrics/ParameterLists
27
+
28
+ # @param attribute [String,Symbol] the attribute for the date inputs
29
+ # @param name [String] the name of the date input item
30
+ # @param input [Hash] options for the input of the date input item, see {CCS::Components::GovUK::Field::Input::TextInput#initialize TextInput#initialize} for more details.
31
+ # @param label [Hash] options for the label of the date input item, see {CCS::Components::GovUK::Label#initialize Label#initialize} for more details.
32
+ # @param context [ActionView::Base] the view context
33
+ #
34
+ # @option options [Hash] error_message used as a flag to add the error classes to the item
35
+ # @option options [ActiveModel] :model (nil) optional model used to create the field
36
+ # @option options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create the field
37
+
38
+ def initialize(attribute:, name:, context:, input: {}, label: {}, **options)
39
+ input[:classes] = "govuk-date-input__input #{input[:classes]}".rstrip
40
+ input[:classes] << ' govuk-input--error' if options[:error_message]
41
+ (input[:attributes] ||= {})[:inputmode] ||= 'numeric'
42
+
43
+ label[:text] ||= name.capitalize
44
+ label[:classes] = 'govuk-date-input__label'
45
+
46
+ @input = Input::TextInput.new(attribute: "#{attribute}_#{name}", label: label, model: options[:model], form: options[:form], context: context, **input)
47
+ end
48
+
49
+ # rubocop:enable Metrics/ParameterLists
50
+
51
+ # Generates the HTML for the date input item
52
+ #
53
+ # @return [ActiveSupport::SafeBuffer]
54
+
55
+ def render
56
+ tag.div(input.render, class: 'govuk-date-input__item')
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,87 @@
1
+ require_relative '../input'
2
+ require_relative 'date_input/item'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Inputs < Field
8
+ # = GOV.UK Date Input
9
+ #
10
+ # This is used for generating the date input component from the
11
+ # {https://design-system.service.gov.uk/components/date-input GDS - Components - Date Input}
12
+ #
13
+ # @!attribute [r] date_input_items
14
+ # @return [Array<DateInput::Item>] An array of the initialised date input items
15
+
16
+ class DateInput < Inputs
17
+ private
18
+
19
+ attr_reader :date_input_items
20
+
21
+ public
22
+
23
+ # @param (see CCS::Components::GovUK::Field::Inputs#initialize)
24
+ # @param date_items [Array<Hash>] an array of options for the date items.
25
+ # See {Components::GovUK::Field::Inputs::DateInput::Item#initialize Item#initialize} for details of the items in the array.
26
+ #
27
+ # @option (see CCS::Components::GovUK::Field::Inputs#initialize)
28
+
29
+ def initialize(attribute:, date_items: default_date_items, **options)
30
+ (options[:fieldset][:attributes] ||= {})[:role] = 'group'
31
+
32
+ super(attribute: attribute, **options)
33
+
34
+ # date_items ||= default_date_items
35
+
36
+ @date_input_items = date_items.map { |date_input_item| Item.new(attribute: attribute, error_message: @error_message, model: @options[:model], form: @options[:form], context: @context, **date_input_item) }
37
+ end
38
+
39
+ # Generates the HTML for the GOV.UK date input component
40
+ #
41
+ # @return [ActiveSupport::SafeBuffer]
42
+
43
+ def render
44
+ super() do
45
+ tag.div(**options[:attributes]) do
46
+ date_input_items.each { |date_input_item| concat(date_input_item.render) }
47
+ end
48
+ end
49
+ end
50
+
51
+ # The default attributes for the date input
52
+
53
+ DEFAULT_ATTRIBUTES = { class: 'govuk-date-input' }.freeze
54
+
55
+ private
56
+
57
+ # The default date items used if no date items are provided
58
+ #
59
+ # @return [Array<Hash>]
60
+
61
+ def default_date_items
62
+ [
63
+ {
64
+ name: 'day',
65
+ input: {
66
+ classes: 'govuk-input--width-2'
67
+ }
68
+ },
69
+ {
70
+ name: 'month',
71
+ input: {
72
+ classes: 'govuk-input--width-2'
73
+ }
74
+ },
75
+ {
76
+ name: 'year',
77
+ input: {
78
+ classes: 'govuk-input--width-4'
79
+ }
80
+ }
81
+ ]
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,45 @@
1
+ require_relative '../checkbox'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ class Checkbox < Item
9
+ # = GOV.UK Field Inputs Checkbox form tag
10
+ #
11
+ # This is used to generate an individual checkbox item using +form.check_box+
12
+
13
+ class Form < Checkbox
14
+ # @param (see CCS::Components::GovUK::Field::Items::Item::Checkbox#initialize)
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Items::Item::Checkbox#initialize)
17
+
18
+ def initialize(attribute:, label:, **options)
19
+ super(attribute: attribute, label: label, **options)
20
+
21
+ (label[:attributes] ||= {})[:value] = @value
22
+ label[:attributes][:for] = @options[:attributes][:id] if @options[:attributes][:id]
23
+
24
+ @options[:attributes][:multiple] = true
25
+ @options[:attributes][:include_hidden] = false
26
+
27
+ @label = Label.new(attribute: attribute, form: @options[:form], context: @context, **label)
28
+ end
29
+
30
+ # Generates the HTML for the checkbox input
31
+ #
32
+ # @return [ActiveSupport::SafeBuffer]
33
+
34
+ def render
35
+ super() do
36
+ @options[:form].check_box(@attribute, @options[:attributes], @value)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../checkbox'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ class Checkbox < Item
9
+ # = GOV.UK Field Inputs Checkbox tag
10
+ #
11
+ # This is used to generate an individual checkbox item using +check_box_tag+
12
+
13
+ class Tag < Checkbox
14
+ # @param (see CCS::Components::GovUK::Field::Items::Item::Checkbox#initialize)
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Items::Item::Checkbox#initialize)
17
+
18
+ def initialize(attribute:, label:, **options)
19
+ super(attribute: attribute, label: label, **options)
20
+
21
+ @options[:attributes][:id] ||= "#{sanitize_to_id(@attribute)}_#{sanitize_to_id(@value)}"
22
+
23
+ @label = Label.new(attribute: @options[:attributes][:id], context: @context, **label)
24
+ end
25
+
26
+ # Generates the HTML for the checkbox input
27
+ #
28
+ # @return [ActiveSupport::SafeBuffer]
29
+
30
+ def render
31
+ super() do
32
+ context.check_box_tag("#{@attribute}[]", @value, @options[:checked], **@options[:attributes])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,48 @@
1
+ require_relative '../item'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ # = GOV.UK Field Inputs Checkbox
9
+ #
10
+ # This is used to generate an individual checkbox item
11
+
12
+ class Checkbox < Item
13
+ # @param (see CCS::Components::GovUK::Field::Items::Item#initialize)
14
+ # label [Hash] attributes for the checkbox label, see {CCS::Components::GovUK::Label#initialize Label#initialize} for more details.
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Items::Item#initialize))
17
+
18
+ def initialize(attribute:, label:, **options)
19
+ super(attribute: attribute, **options)
20
+
21
+ label[:classes] = "govuk-checkboxes__label #{label[:classes]}".rstrip
22
+ end
23
+
24
+ # Generates the HTML to wrap arround a checkbox input
25
+ #
26
+ # @yield the checkbox item input HTML
27
+ #
28
+ # @return [ActiveSupport::SafeBuffer]
29
+
30
+ def render(&block)
31
+ tag.div(class: 'govuk-checkboxes__item') do
32
+ super(&block)
33
+ end
34
+ end
35
+
36
+ # The default attributes for the checkbox
37
+
38
+ DEFAULT_ATTRIBUTES = { class: 'govuk-checkboxes__input' }.freeze
39
+
40
+ # The type of the input item
41
+
42
+ ITEM_TYPE = 'checkboxes'.freeze
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ require 'action_view'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ # = GOV.UK Field Inputs item divider
9
+ #
10
+ # The divider for input items e.g. {CCS::Components::GovUK::Field::Inputs::Checkboxes Checkboxes} or {CCS::Components::GovUK::Field::Inputs::Radios Radios}
11
+ #
12
+ # @!attribute [r] text
13
+ # @return [String] Text for the divider
14
+ # @!attribute [r] type
15
+ # @return [String] The type of the item
16
+
17
+ class Divider
18
+ include ActionView::Context
19
+ include ActionView::Helpers
20
+
21
+ private
22
+
23
+ attr_reader :text, :type
24
+
25
+ public
26
+
27
+ # @param divider [String] the text for the divider
28
+ # @param type [String] the type of the item divider.
29
+ # One of +'checkboxes'+ or +'radios'+
30
+
31
+ def initialize(divider:, type:)
32
+ @text = divider
33
+ @type = type
34
+ end
35
+
36
+ # Generates the HTML for an item divider
37
+ #
38
+ # @return [ActiveSupport::SafeBuffer]
39
+
40
+ def render
41
+ tag.div(text, class: "govuk-#{type}__divider")
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ require_relative '../radio'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ class Radio < Item
9
+ # = GOV.UK Field Inputs Radio form tag
10
+ #
11
+ # This is used to generate an individual radio item using +form.check_box+
12
+
13
+ class Form < Radio
14
+ # @param (see CCS::Components::GovUK::Field::Items::Item::Radio#initialize)
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Items::Item::Radio#initialize)
17
+
18
+ def initialize(attribute:, label:, **options)
19
+ super(attribute: attribute, label: label, **options)
20
+
21
+ (label[:attributes] ||= {})[:value] = @value
22
+ label[:attributes][:for] = @options[:attributes][:id] if @options[:attributes][:id]
23
+
24
+ @label = Label.new(attribute: attribute, form: @options[:form], context: @context, **label)
25
+ end
26
+
27
+ # Generates the HTML for the radio input
28
+ #
29
+ # @return [ActiveSupport::SafeBuffer]
30
+
31
+ def render
32
+ super() do
33
+ @options[:form].radio_button(@attribute, @value, **@options[:attributes])
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../radio'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ class Radio < Item
9
+ # = GOV.UK Field Inputs Radio tag
10
+ #
11
+ # This is used to generate an individual radio item using +radio_button_tag+
12
+
13
+ class Tag < Radio
14
+ # @param (see CCS::Components::GovUK::Field::Items::Item::Radio#initialize)
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Items::Item::Radio#initialize)
17
+
18
+ def initialize(attribute:, label:, **options)
19
+ super(attribute: attribute, label: label, **options)
20
+
21
+ @options[:attributes][:id] ||= "#{sanitize_to_id(@attribute)}_#{sanitize_to_id(@value)}"
22
+
23
+ @label = Label.new(attribute: @options[:attributes][:id], context: @context, **label)
24
+ end
25
+
26
+ # Generates the HTML for the radio input
27
+ #
28
+ # @return [ActiveSupport::SafeBuffer]
29
+
30
+ def render
31
+ super() do
32
+ context.radio_button_tag(@attribute, @value, @options[:checked], **@options[:attributes])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,48 @@
1
+ require_relative '../item'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Inputs < Field
7
+ class Item < Base
8
+ # = GOV.UK Field Inputs Radio
9
+ #
10
+ # This is used to generate an individual radio item
11
+
12
+ class Radio < Item
13
+ # @param (see CCS::Components::GovUK::Field::Items::Item#initialize)
14
+ # label [Hash] attributes for the radio label, see {CCS::Components::GovUK::Label#initialize Label#initialize} for more details.
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Items::Item#initialize))
17
+
18
+ def initialize(attribute:, label:, **options)
19
+ super(attribute: attribute, **options)
20
+
21
+ label[:classes] = "govuk-radios__label #{label[:classes]}".rstrip
22
+ end
23
+
24
+ # Generates the HTML to wrap arround a radio input
25
+ #
26
+ # @yield the radio item input HTML
27
+ #
28
+ # @return [ActiveSupport::SafeBuffer]
29
+
30
+ def render(&block)
31
+ tag.div(class: 'govuk-radios__item') do
32
+ super(&block)
33
+ end
34
+ end
35
+
36
+ # The default attributes for the radio
37
+
38
+ DEFAULT_ATTRIBUTES = { class: 'govuk-radios__input' }.freeze
39
+
40
+ # The type of the input item
41
+
42
+ ITEM_TYPE = 'radios'.freeze
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,109 @@
1
+ require_relative '../inputs'
2
+ require_relative '../../label'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Inputs < Field
8
+ # = GOV.UK Field Inputs Item
9
+ #
10
+ # This class is used to provided the shared HTML for a
11
+ # {Components::GovUK::Field::Inputs::Item::Checkbox checkbox item} or {Components::GovUK::Field::Inputs::Item::Radio radio item}.
12
+ # It wraps these items with the following:
13
+ # - Item label
14
+ # - Item hint
15
+ # - Item conditional content
16
+ #
17
+ # @!attribute [r] attribute
18
+ # @return [String,Symbol] The attribute of the item
19
+ # @!attribute [r] value
20
+ # @return [String] The value of the item
21
+ # @!attribute [r] label
22
+ # @return [Label] The initialised item label
23
+ # @!attribute [r] hint
24
+ # @return [Hint] The initialised item hint
25
+ # @!attribute [r] conditional_content
26
+ # @return [ActiveSupport::SafeBuffer] The conditional HTML
27
+
28
+ class Item < Base
29
+ private
30
+
31
+ attr_reader :attribute, :value, :label, :hint, :conditional_content
32
+
33
+ public
34
+
35
+ # @param attribute [String,Symbol] the attribute of the item
36
+ # @param value [String] the value of the item
37
+ # @param hint [Hash] options for an item hint see {CCS::Components::GovUK::Hint#initialize Hint#initialize} for more details.
38
+ # If no hint is given then no hint will be rendered
39
+ # @param conditional [Hash] content that will appear if the item is checked.
40
+ # If no conditional is given then no conditional content will be rendered
41
+ #
42
+ # @option options [String] :classes additional CSS classes for the item HTML
43
+ # @option options [Boolean] :checked flag to indicate if the item is checked
44
+ # @option options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create item HTML
45
+ # @option options [Hash] :attributes ({}) any additional attributes that will be added as part of the item HTML
46
+ #
47
+ # @option conditional [ActiveSupport::SafeBuffer] content the HTML content
48
+ # @option conditional [Hash] attributes[:id] the id of the conditional section
49
+
50
+ def initialize(attribute:, value:, hint: nil, conditional: nil, **options)
51
+ super(**options)
52
+
53
+ initialise_item_hint(attribute, value, hint) if hint
54
+ initialize_item_conditional(attribute, value, conditional) if conditional
55
+ @attribute = attribute
56
+ @value = value
57
+ end
58
+
59
+ # Generates the HTML to wrap arround a input
60
+ #
61
+ # @yield the item input HTML
62
+ #
63
+ # @return [ActiveSupport::SafeBuffer]
64
+
65
+ def render
66
+ concat(yield)
67
+ concat(label.render)
68
+ concat(hint.render) if hint
69
+ concat(conditional_content) if conditional_content
70
+ end
71
+
72
+ private
73
+
74
+ # Initialises the item hint and adds the aira-describedby attribute for the item
75
+ #
76
+ # @param attribute [String, Symbol] the attribute of the item
77
+ # @param value [String] the value of the item
78
+ # @param hint [Hash] the options for the hint
79
+
80
+ def initialise_item_hint(attribute, value, hint)
81
+ hint[:attributes] ||= {}
82
+ hint[:classes] = "govuk-#{self.class::ITEM_TYPE}__hint #{hint[:classes]}".rstrip
83
+ hint[:attributes][:id] ||= "#{attribute}_#{value}-item-hint"
84
+
85
+ (@options[:attributes][:aria] ||= {})[:describedby] = hint[:attributes][:id]
86
+
87
+ @hint = Hint.new(context: @context, **hint)
88
+ end
89
+
90
+ # Generate the item conditional HTML and adds the data-aira-controls attribute for the item
91
+ #
92
+ # @param attribute [String, Symbol] the attribute of the item
93
+ # @param value [String] the value of the item
94
+ # @param conditional [Hash] the options for the conditional
95
+
96
+ def initialize_item_conditional(attribute, value, conditional)
97
+ conditional[:attributes] ||= {}
98
+ conditional[:attributes][:class] = "govuk-#{self.class::ITEM_TYPE}__conditional #{"govuk-#{self.class::ITEM_TYPE}__conditional--hidden" unless @options[:checked]}".rstrip
99
+ conditional[:attributes][:id] ||= sanitize_to_id("#{attribute}_#{value}_conditional")
100
+
101
+ (@options[:attributes][:data] ||= {})[:'aria-controls'] = conditional[:attributes][:id]
102
+
103
+ @conditional_content = tag.div(conditional[:content], class: conditional[:attributes][:class], id: conditional[:attributes][:id])
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,67 @@
1
+ require_relative '../input'
2
+ require_relative 'item/divider'
3
+ require_relative 'item/radio/form'
4
+ require_relative 'item/radio/tag'
5
+
6
+ module CCS::Components
7
+ module GovUK
8
+ class Field < Base
9
+ class Inputs < Field
10
+ # = GOV.UK Radios
11
+ #
12
+ # This is used for generating the radios component from the
13
+ # {https://design-system.service.gov.uk/components/radios GDS - Components - Radios}
14
+ #
15
+ # @!attribute [r] radio_items
16
+ # @return [Array<Item::Divider|Item::Radio|Item::Radio>] An array of the initialised radio items
17
+
18
+ class Radios < Inputs
19
+ private
20
+
21
+ attr_reader :radio_items
22
+
23
+ public
24
+
25
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
26
+
27
+ # @param (see CCS::Components::GovUK::Field::Inputs#initialize)
28
+ # @param radio_items [Array<Hash>] an array of options for the radios.
29
+ # See {Components::GovUK::Field::Inputs::Item::Radio#initialize Radio#initialize} for details of the items in the array.
30
+ #
31
+ # @option (see CCS::Components::GovUK::Field::Inputs#initialize)
32
+
33
+ def initialize(attribute:, radio_items:, **options)
34
+ super(attribute: attribute, **options)
35
+
36
+ if @options[:model] || @options[:form]
37
+ values = (@options[:model] || @options[:form].object).send(attribute) || []
38
+ radio_items.each { |radio_item| radio_item[:checked] = values.include?(radio_item[:value]) }
39
+ end
40
+
41
+ radio_item_class = @options[:form] ? Item::Radio::Form : Inputs::Item::Radio::Tag
42
+
43
+ @radio_items = radio_items.map { |radio_item| radio_item[:divider] ? Item::Divider.new(divider: radio_item[:divider], type: 'radios') : radio_item_class.new(attribute: attribute, form: @options[:form], context: @context, **radio_item) }
44
+ end
45
+
46
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
47
+
48
+ # Generates the HTML for the GOV.UK Radios component
49
+ #
50
+ # @return [ActiveSupport::SafeBuffer]
51
+
52
+ def render
53
+ super() do
54
+ tag.div(**options[:attributes]) do
55
+ radio_items.each { |radio_item| concat(radio_item.render) }
56
+ end
57
+ end
58
+ end
59
+
60
+ # The default attributes for the radios
61
+
62
+ DEFAULT_ATTRIBUTES = { class: 'govuk-radios', data: { module: 'govuk-radios' } }.freeze
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end