ccs-frontend_helpers 0.1.0.rc.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -5
  3. data/CHANGELOG.md +2 -4
  4. data/Gemfile +1 -15
  5. data/Gemfile.lock +80 -81
  6. data/ccs-frontend_helpers.gemspec +10 -0
  7. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +79 -0
  8. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +119 -6
  9. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +183 -6
  10. data/lib/ccs/{components/ccs → frontend_helpers/ccs_frontend}/logo.rb +12 -10
  11. data/lib/ccs/frontend_helpers/ccs_frontend.rb +4 -2
  12. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +95 -8
  13. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +17 -6
  14. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +54 -6
  15. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +105 -6
  16. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +114 -6
  17. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +23 -7
  18. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +32 -11
  19. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +65 -13
  20. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +165 -0
  21. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +200 -0
  22. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +153 -0
  23. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +83 -0
  24. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +153 -0
  25. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +201 -0
  26. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +124 -0
  27. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +106 -0
  28. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +213 -0
  29. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +48 -7
  30. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +161 -6
  31. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +24 -13
  32. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +139 -6
  33. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +17 -8
  34. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +21 -7
  35. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +70 -7
  36. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +113 -7
  37. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +314 -6
  38. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +28 -7
  39. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +27 -8
  40. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +18 -6
  41. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +186 -7
  42. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +204 -6
  43. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +100 -11
  44. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +73 -6
  45. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +21 -7
  46. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +30 -7
  47. data/lib/ccs/frontend_helpers/govuk_frontend.rb +19 -17
  48. data/lib/ccs/frontend_helpers/shared_methods.rb +27 -0
  49. data/lib/ccs/frontend_helpers/version.rb +1 -1
  50. metadata +142 -112
  51. data/.ruby-version +0 -1
  52. data/lib/ccs/components/base.rb +0 -64
  53. data/lib/ccs/components/ccs/dashboard_section/panel.rb +0 -57
  54. data/lib/ccs/components/ccs/dashboard_section.rb +0 -71
  55. data/lib/ccs/components/ccs/footer/link.rb +0 -55
  56. data/lib/ccs/components/ccs/footer/meta.rb +0 -59
  57. data/lib/ccs/components/ccs/footer/navigation.rb +0 -60
  58. data/lib/ccs/components/ccs/footer.rb +0 -95
  59. data/lib/ccs/components/ccs/header/link.rb +0 -60
  60. data/lib/ccs/components/ccs/header/navigation.rb +0 -98
  61. data/lib/ccs/components/ccs/header/service_authentication.rb +0 -53
  62. data/lib/ccs/components/ccs/header.rb +0 -110
  63. data/lib/ccs/components/govuk/accordion/section/content.rb +0 -53
  64. data/lib/ccs/components/govuk/accordion/section/header.rb +0 -57
  65. data/lib/ccs/components/govuk/accordion/section.rb +0 -61
  66. data/lib/ccs/components/govuk/accordion.rb +0 -58
  67. data/lib/ccs/components/govuk/back_link.rb +0 -51
  68. data/lib/ccs/components/govuk/breadcrumbs/link.rb +0 -57
  69. data/lib/ccs/components/govuk/breadcrumbs.rb +0 -55
  70. data/lib/ccs/components/govuk/button.rb +0 -107
  71. data/lib/ccs/components/govuk/cookie_banner/action.rb +0 -60
  72. data/lib/ccs/components/govuk/cookie_banner/message.rb +0 -80
  73. data/lib/ccs/components/govuk/cookie_banner.rb +0 -55
  74. data/lib/ccs/components/govuk/details.rb +0 -53
  75. data/lib/ccs/components/govuk/error_message.rb +0 -60
  76. data/lib/ccs/components/govuk/error_summary/item.rb +0 -54
  77. data/lib/ccs/components/govuk/error_summary.rb +0 -70
  78. data/lib/ccs/components/govuk/field/input/character_count.rb +0 -129
  79. data/lib/ccs/components/govuk/field/input/file_upload.rb +0 -44
  80. data/lib/ccs/components/govuk/field/input/select.rb +0 -82
  81. data/lib/ccs/components/govuk/field/input/text_input/fix.rb +0 -55
  82. data/lib/ccs/components/govuk/field/input/text_input.rb +0 -97
  83. data/lib/ccs/components/govuk/field/input/textarea.rb +0 -59
  84. data/lib/ccs/components/govuk/field/input.rb +0 -62
  85. data/lib/ccs/components/govuk/field/inputs/checkboxes.rb +0 -69
  86. data/lib/ccs/components/govuk/field/inputs/date_input/item.rb +0 -65
  87. data/lib/ccs/components/govuk/field/inputs/date_input.rb +0 -89
  88. data/lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb +0 -47
  89. data/lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb +0 -43
  90. data/lib/ccs/components/govuk/field/inputs/item/checkbox.rb +0 -50
  91. data/lib/ccs/components/govuk/field/inputs/item/divider.rb +0 -50
  92. data/lib/ccs/components/govuk/field/inputs/item/radio/form.rb +0 -44
  93. data/lib/ccs/components/govuk/field/inputs/item/radio/tag.rb +0 -43
  94. data/lib/ccs/components/govuk/field/inputs/item/radio.rb +0 -50
  95. data/lib/ccs/components/govuk/field/inputs/item.rb +0 -111
  96. data/lib/ccs/components/govuk/field/inputs/radios.rb +0 -69
  97. data/lib/ccs/components/govuk/field/inputs.rb +0 -57
  98. data/lib/ccs/components/govuk/field.rb +0 -108
  99. data/lib/ccs/components/govuk/fieldset/legend.rb +0 -65
  100. data/lib/ccs/components/govuk/fieldset.rb +0 -54
  101. data/lib/ccs/components/govuk/footer/link.rb +0 -55
  102. data/lib/ccs/components/govuk/footer/meta.rb +0 -59
  103. data/lib/ccs/components/govuk/footer/navigation.rb +0 -60
  104. data/lib/ccs/components/govuk/footer.rb +0 -131
  105. data/lib/ccs/components/govuk/form_group.rb +0 -60
  106. data/lib/ccs/components/govuk/header/link.rb +0 -56
  107. data/lib/ccs/components/govuk/header/navigation.rb +0 -71
  108. data/lib/ccs/components/govuk/header.rb +0 -107
  109. data/lib/ccs/components/govuk/hint.rb +0 -49
  110. data/lib/ccs/components/govuk/inset_text.rb +0 -52
  111. data/lib/ccs/components/govuk/label.rb +0 -85
  112. data/lib/ccs/components/govuk/notification_banner.rb +0 -107
  113. data/lib/ccs/components/govuk/pagination/increment/next.rb +0 -44
  114. data/lib/ccs/components/govuk/pagination/increment/previous.rb +0 -43
  115. data/lib/ccs/components/govuk/pagination/increment.rb +0 -98
  116. data/lib/ccs/components/govuk/pagination/item/ellipsis.rb +0 -28
  117. data/lib/ccs/components/govuk/pagination/item/form.rb +0 -49
  118. data/lib/ccs/components/govuk/pagination/item/tag.rb +0 -47
  119. data/lib/ccs/components/govuk/pagination/item.rb +0 -64
  120. data/lib/ccs/components/govuk/pagination.rb +0 -96
  121. data/lib/ccs/components/govuk/panel.rb +0 -62
  122. data/lib/ccs/components/govuk/phase_banner.rb +0 -69
  123. data/lib/ccs/components/govuk/skip_link.rb +0 -51
  124. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list/item.rb +0 -53
  125. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list.rb +0 -50
  126. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/paragraph.rb +0 -45
  127. data/lib/ccs/components/govuk/step_by_step_navigation/section/content.rb +0 -63
  128. data/lib/ccs/components/govuk/step_by_step_navigation/section/heading.rb +0 -69
  129. data/lib/ccs/components/govuk/step_by_step_navigation/section.rb +0 -56
  130. data/lib/ccs/components/govuk/step_by_step_navigation.rb +0 -59
  131. data/lib/ccs/components/govuk/summary_list/action/link.rb +0 -59
  132. data/lib/ccs/components/govuk/summary_list/card/actions.rb +0 -59
  133. data/lib/ccs/components/govuk/summary_list/card/title.rb +0 -51
  134. data/lib/ccs/components/govuk/summary_list/card.rb +0 -63
  135. data/lib/ccs/components/govuk/summary_list/row/actions.rb +0 -59
  136. data/lib/ccs/components/govuk/summary_list/row/key.rb +0 -47
  137. data/lib/ccs/components/govuk/summary_list/row/value.rb +0 -47
  138. data/lib/ccs/components/govuk/summary_list/row.rb +0 -67
  139. data/lib/ccs/components/govuk/summary_list.rb +0 -74
  140. data/lib/ccs/components/govuk/table/body/data_cell.rb +0 -53
  141. data/lib/ccs/components/govuk/table/body/head_cell.rb +0 -52
  142. data/lib/ccs/components/govuk/table/header/head_cell.rb +0 -54
  143. data/lib/ccs/components/govuk/table.rb +0 -111
  144. data/lib/ccs/components/govuk/tabs/panel.rb +0 -58
  145. data/lib/ccs/components/govuk/tabs/tab.rb +0 -56
  146. data/lib/ccs/components/govuk/tabs.rb +0 -66
  147. data/lib/ccs/components/govuk/tag.rb +0 -51
  148. data/lib/ccs/components/govuk/warning_text.rb +0 -61
  149. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_section.rb +0 -27
  150. data/lib/ccs/frontend_helpers/govuk_frontend/character_count.rb +0 -28
  151. data/lib/ccs/frontend_helpers/govuk_frontend/checkboxes.rb +0 -28
  152. data/lib/ccs/frontend_helpers/govuk_frontend/date_input.rb +0 -28
  153. data/lib/ccs/frontend_helpers/govuk_frontend/file_upload.rb +0 -28
  154. data/lib/ccs/frontend_helpers/govuk_frontend/input.rb +0 -28
  155. data/lib/ccs/frontend_helpers/govuk_frontend/radios.rb +0 -28
  156. data/lib/ccs/frontend_helpers/govuk_frontend/select.rb +0 -28
  157. data/lib/ccs/frontend_helpers/govuk_frontend/textarea.rb +0 -28
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK Checkboxes
10
+ #
11
+ # This helper is used for generating the checkboxes component from the
12
+ # {https://design-system.service.gov.uk/components/checkboxes GDS - Components - Checkboxes}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
15
+
16
+ module Checkboxes
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK Checkboxes component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the raido buttons
22
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
23
+ # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
24
+ #
25
+ # @option govuk_checkboxes_options [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_checkboxes_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_checkboxes_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the checkbox tags and find the error message
29
+ # @option govuk_checkboxes_options [Hash] :form_group see {govuk_fields}
30
+ # @option govuk_checkboxes_options [Hash] :fieldset see {govuk_fields}
31
+ # @option govuk_checkboxes_options [Hash] :hint see {govuk_field}
32
+ # @option govuk_checkboxes_options [Hash] :checkboxes ({}) the options that will be used when rendering the checkbox buttons.
33
+ # See {_govuk_checkboxes_fields} for more details.
34
+ #
35
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
36
+ # which can then be rendered on the page
37
+
38
+ def govuk_checkboxes(attribute, items, **govuk_checkboxes_options)
39
+ govuk_fields(:checkboxes, attribute, **govuk_checkboxes_options) do |govuk_field_options|
40
+ if govuk_checkboxes_options[:model] || govuk_checkboxes_options[:form]
41
+ values = (govuk_checkboxes_options[:model] || govuk_checkboxes_options[:form].object).send(attribute) || []
42
+ items.each { |item| item[:checked] = values.include?(item[:value]) }
43
+ end
44
+
45
+ concat(
46
+ if govuk_checkboxes_options[:form]
47
+ govuk_checkbox_field_form(govuk_checkboxes_options[:form], attribute, items, **govuk_field_options)
48
+ else
49
+ govuk_checkbox_field_tag(attribute, items, **govuk_field_options)
50
+ end
51
+ )
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # Generates the checkboxes HTML for {govuk_checkboxes}
58
+ #
59
+ # @param attribute [String, Symbol] the attribute of the raido buttons
60
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
61
+ # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
62
+ #
63
+ # @option (see _govuk_checkboxes_fields)
64
+ #
65
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes}
66
+
67
+ def govuk_checkbox_field_tag(attribute, items, **govuk_checkboxes_options)
68
+ _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
69
+ govuk_checkbox_item_tag(attribute, checkbox_item)
70
+ end
71
+ end
72
+
73
+ # Generates the checkboxes HTML for {govuk_checkboxes} when there is a ActionView::Helpers::FormBuilder
74
+ #
75
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
76
+ # @param (see govuk_checkbox_field_tag)
77
+ #
78
+ # @option (see _govuk_checkboxes_fields)
79
+ #
80
+ # @return (see govuk_checkbox_field_tag)
81
+
82
+ def govuk_checkbox_field_form(form, attribute, items, **govuk_checkboxes_options)
83
+ _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
84
+ govuk_checkbox_item_form(form, attribute, checkbox_item)
85
+ end
86
+ end
87
+
88
+ # Wrapper method used by {govuk_checkbox_field_tag} and {govuk_checkbox_field_form} to generate the checkboxes HTML
89
+ #
90
+ # @param items [Array] array of checkbox items.
91
+ # Each item is a hash which can be:
92
+ # - +:divider+ text to separate checkbox items
93
+ # - checkbox item, see {_govuk_checkbox_item}
94
+ # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
95
+ #
96
+ # @option govuk_checkboxes_options [String] :classes additional CSS classes for the checkboxes HTML
97
+ # @option govuk_checkboxes_options [Hash] :attributes ({ module: 'govuk-checkboxes' }) any additional attributes that will added as part of the HTML
98
+ #
99
+ # @yield the checkbox item HTML generated by {govuk_checkbox_field_tag} or {govuk_checkbox_field_form}
100
+ #
101
+ # @yieldparam checkbox_item [Hash] the current checkbox item to be rendered
102
+ #
103
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkbox_field_tag} or {govuk_checkbox_field_form}
104
+
105
+ def _govuk_checkboxes_fields(items, **govuk_checkboxes_options)
106
+ initialise_attributes_and_set_classes(govuk_checkboxes_options, 'govuk-checkboxes')
107
+ set_data_module(govuk_checkboxes_options, 'govuk-checkboxes')
108
+
109
+ tag.div(**govuk_checkboxes_options[:attributes]) do
110
+ items.each do |checkbox_item|
111
+ concat(
112
+ if checkbox_item[:divider]
113
+ tag.div(checkbox_item[:divider], class: 'govuk-checkboxes__divider')
114
+ else
115
+ tag.div(class: 'govuk-checkboxes__item') do
116
+ yield(checkbox_item)
117
+ end
118
+ end
119
+ )
120
+ end
121
+ end
122
+ end
123
+
124
+ # Generates the HTML for a checkbox button for {govuk_checkbox_field_form}
125
+ #
126
+ # @param (see _govuk_checkbox_item)
127
+ #
128
+ # @option (see _govuk_checkbox_item)
129
+ #
130
+ # @return (see _govuk_checkbox_item)
131
+
132
+ def govuk_checkbox_item_tag(attribute, checkbox_item)
133
+ _govuk_checkbox_item(attribute, checkbox_item) do
134
+ checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
135
+
136
+ concat(check_box_tag("#{attribute}[]", checkbox_item[:value], checkbox_item[:checked], **checkbox_item[:attributes]))
137
+ concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label]))
138
+ end
139
+ end
140
+
141
+ # Generates the HTML for a checkbox button for {govuk_checkbox_field_tag}
142
+ #
143
+ # @param (see _govuk_checkbox_item)
144
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
145
+ #
146
+ # @option (see _govuk_checkbox_item)
147
+ #
148
+ # @return (see _govuk_checkbox_item)
149
+
150
+ def govuk_checkbox_item_form(form, attribute, checkbox_item)
151
+ _govuk_checkbox_item(attribute, checkbox_item) do
152
+ (checkbox_item[:label][:attributes] ||= {})[:value] = checkbox_item[:value]
153
+ checkbox_item[:label][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
154
+
155
+ concat(form.check_box(attribute, checkbox_item[:attributes].merge({ multiple: true, include_hidden: false }), checkbox_item[:value]))
156
+ concat(govuk_label(attribute, checkbox_item[:label][:text], form: form, **checkbox_item[:label]))
157
+ end
158
+ end
159
+
160
+ # rubocop:disable Metrics/AbcSize
161
+
162
+ # Wrapper method used by {govuk_checkbox_item_tag} and {govuk_checkbox_item_form} to generate the checkboxes HTML
163
+ # including the label and hint, if there is one.
164
+ #
165
+ # @param attribute [String, Symbol] the attribute of the raido buttons
166
+ # @param checkbox_item [Hash] the options for the checkbox item
167
+ #
168
+ # @option checkbox_item [String] :classes additional CSS classes for the checkbox button HTML
169
+ # @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button, see {govuk_label}
170
+ # @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the checkbox button, see {govuk_hint}.
171
+ # If no hint is given then no hint will be rendered
172
+ # @option checkbox_item [Hash] :conditional (nil) content that will appear when the checkbox input is checked.
173
+ # It can have the following options:
174
+ # - +[:content]+ the content that will be shown
175
+ # - +[:attributes][:id]+ the id of the conditional section
176
+ # If no conditional is given then no conditional content will be rendered
177
+ # @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
178
+ #
179
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons, label and hint
180
+ # which is used in {govuk_checkbox_item_tag} and {govuk_checkbox_item_form}
181
+
182
+ def _govuk_checkbox_item(attribute, checkbox_item)
183
+ (checkbox_item[:attributes] ||= {})[:class] = 'govuk-checkboxes__input'
184
+ checkbox_item[:label] ||= {}
185
+ checkbox_item[:label][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:classes]}".rstrip
186
+
187
+ set_item_options_for_hint('checkboxes', attribute, checkbox_item)
188
+ set_conditional_item_options('checkboxes', attribute, checkbox_item)
189
+
190
+ yield
191
+ concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint])) if checkbox_item[:hint]
192
+ concat(tag.div(checkbox_item[:conditional][:content], class: checkbox_item[:conditional][:attributes][:class], id: checkbox_item[:conditional][:attributes][:id])) if checkbox_item[:conditional]
193
+ end
194
+
195
+ # rubocop:enable Metrics/AbcSize
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+ require_relative 'input'
5
+
6
+ module CCS
7
+ module FrontendHelpers
8
+ module GovUKFrontend
9
+ module Field
10
+ # = GOV.UK Date Input
11
+ #
12
+ # This helper is used for generating the date input component from the
13
+ # {https://design-system.service.gov.uk/components/date-input GDS - Components - Date Input}
14
+ #
15
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
16
+
17
+ module DateInput
18
+ include Field
19
+ include Input
20
+
21
+ # Generates the HTML for the GOV.UK date input component
22
+ #
23
+ # @param attribute [String, Symbol] the attribute of the date input
24
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
25
+ #
26
+ # @option govuk_date_input_options [String] :error_message (nil) the error message to be displayed
27
+ # @option govuk_date_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
28
+ # @option govuk_date_input_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
29
+ # the date tag and find the error message
30
+ # @option govuk_date_input_options [Hash] :form_group see {govuk_fields}
31
+ # @option govuk_date_input_options [Hash] :fieldset see {govuk_fields}
32
+ # @option govuk_date_input_options [Hash] :hint see {govuk_field}
33
+ # @option govuk_date_input_options [Hash] :date_input_options ({}) the options that will be used when rendering the date input.
34
+ # See {_govuk_date_input_fields} for more details.
35
+ #
36
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Date Input
37
+ # which can then be rendered on the page
38
+
39
+ def govuk_date_input(attribute, **govuk_date_input_options)
40
+ set_govuk_date_input_fieldset_options(**govuk_date_input_options)
41
+
42
+ govuk_fields(:date_input, attribute, **govuk_date_input_options) do |govuk_field_options, error_message|
43
+ concat(_govuk_date_input_fields(attribute, error_message, **govuk_field_options) do |date_item_attribute, date_item_input_options|
44
+ govuk_input(
45
+ date_item_attribute,
46
+ model: govuk_date_input_options[:model],
47
+ form: govuk_date_input_options[:form],
48
+ **date_item_input_options
49
+ )
50
+ end)
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ # Sets the role for the fieldset to group
57
+ #
58
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
59
+
60
+ def set_govuk_date_input_fieldset_options(**govuk_date_input_options)
61
+ (govuk_date_input_options[:fieldset][:attributes] ||= {})[:role] = 'group'
62
+ end
63
+
64
+ # Wrapper method used by {govuk_date_input} to generate the date inputs HTML
65
+ #
66
+ # @param attribute [String, Symbol] the attribute of the date input
67
+ # @param any_errors [Boolean] flag to indicate if the inputs need the error class
68
+ # @param govuk_date_input_options [Hash] options that will be used in customising the HTML
69
+ #
70
+ # @option govuk_date_input_options [String] :classes additional CSS classes for the date inputs HTML
71
+ # @option govuk_date_input_options[Array] :date_items an array of the date items that will be rendered.
72
+ # Each date item hash must contain the +:name+ to add as suffix to attribute.
73
+ # Anything else in the hash will be used as options in a {govuk_input}.
74
+ # @option govuk_date_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
75
+ #
76
+ # @yield the date input item HTML generated by the parent method
77
+ #
78
+ # @yieldparam date_item_attribute [String] the attribute for individual date input
79
+ # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
80
+ #
81
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input}
82
+
83
+ def _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options)
84
+ initialise_attributes_and_set_classes(govuk_date_input_options, 'govuk-date-input')
85
+
86
+ date_items = govuk_date_input_options[:date_items] || govuk_default_date_items
87
+
88
+ tag.div(**govuk_date_input_options[:attributes]) do
89
+ enumerate_and_set_date_input_options(date_items, any_errors) do |date_item_input_options|
90
+ concat(tag.div(class: 'govuk-date-input__item') do
91
+ concat(yield("#{attribute}_#{date_item_input_options[:name]}", date_item_input_options))
92
+ end)
93
+ end
94
+ end
95
+ end
96
+
97
+ # rubocop:disable Metrics/CyclomaticComplexity
98
+
99
+ # Enumerates and inititalises the attributes of each date input item and is used by {_govuk_date_input_fields}
100
+ #
101
+ # @param date_items [Array] an array of the date item hashes that will be inititalised
102
+ # @param any_errors [Boolean] flag to indicate if the inputs need the error class
103
+ #
104
+ # @yield the date input item HTML generated by teach date input item
105
+ #
106
+ # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
107
+
108
+ def enumerate_and_set_date_input_options(date_items, any_errors)
109
+ date_items.each do |date_item_input_options|
110
+ (date_item_input_options[:input] ||= {})[:attributes] ||= {}
111
+
112
+ date_item_input_options[:input][:classes] = "govuk-date-input__input #{date_item_input_options[:input][:classes]} #{'govuk-input--error' if any_errors}".rstrip
113
+
114
+ date_item_input_options[:input][:attributes][:inputmode] ||= 'numeric'
115
+
116
+ date_item_input_options[:label] ||= {}
117
+ date_item_input_options[:label][:text] ||= date_item_input_options[:name].capitalize
118
+ date_item_input_options[:label][:classes] = 'govuk-date-input__label'
119
+
120
+ yield(date_item_input_options)
121
+ end
122
+ end
123
+ # rubocop:enable Metrics/CyclomaticComplexity
124
+
125
+ # The default date items used in {_govuk_date_input_fields} if no date items are provided
126
+
127
+ def govuk_default_date_items
128
+ [
129
+ {
130
+ name: 'day',
131
+ input: {
132
+ classes: 'govuk-input--width-2'
133
+ }
134
+ },
135
+ {
136
+ name: 'month',
137
+ input: {
138
+ classes: 'govuk-input--width-2'
139
+ }
140
+ },
141
+ {
142
+ name: 'year',
143
+ input: {
144
+ classes: 'govuk-input--width-4'
145
+ }
146
+ }
147
+ ]
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK File Upload
10
+ #
11
+ # This helper is used for generating the file upload component from the
12
+ # {https://design-system.service.gov.uk/components/file-upload GDS - Components - File Upload}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
15
+
16
+ module FileUpload
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK file upload component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the file upload
22
+ # @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
23
+ #
24
+ # @option govuk_file_upload_options [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_file_upload_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_file_upload_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the file upload input and find the error message
28
+ # @option govuk_file_upload_options [Hash] :form_group see {govuk_field}
29
+ # @option govuk_file_upload_options [Hash] :label see {govuk_field}
30
+ # @option govuk_file_upload_options [Hash] :hint see {govuk_field}
31
+ # @option govuk_file_upload_options [Hash] :file_upload ({}) the options that will be used when rendering the file upload.
32
+ # See {govuk_file_upload_tag} for more details.
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK File Upload
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_file_upload(attribute, **govuk_file_upload_options)
38
+ govuk_field(:file_upload, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
39
+ initialise_attributes_and_set_classes(govuk_field_options, "govuk-file-upload #{'govuk-file-upload--error' if error_message}".rstrip)
40
+
41
+ concat(
42
+ if govuk_file_upload_options[:form]
43
+ govuk_file_upload_form(govuk_file_upload_options[:form], attribute, **govuk_field_options)
44
+ else
45
+ govuk_file_upload_tag(attribute, **govuk_field_options)
46
+ end
47
+ )
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ # Generates the file upload HTML for {govuk_file_upload}
54
+ #
55
+ # @param attribute [String, Symbol] the attribute of the file upload
56
+ # @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
57
+ #
58
+ # @option govuk_file_upload_options [String] :classes additional CSS classes for the file upload HTML
59
+ # @option govuk_file_upload_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
60
+ #
61
+ # @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload}
62
+
63
+ def govuk_file_upload_tag(attribute, **govuk_file_upload_options)
64
+ file_field_tag(attribute, **govuk_file_upload_options[:attributes])
65
+ end
66
+
67
+ # Generates the file upload HTML for {govuk_file_upload} when there is a ActionView::Helpers::FormBuilder
68
+ #
69
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
70
+ # @param (see govuk_file_upload_tag)
71
+ #
72
+ # @option (see govuk_file_upload_tag)
73
+ #
74
+ # @return (see govuk_file_upload_tag)
75
+
76
+ def govuk_file_upload_form(form, attribute, **govuk_file_upload_options)
77
+ form.file_field(attribute, **govuk_file_upload_options[:attributes])
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK Input
10
+ #
11
+ # This helper is used for generating the input component from the
12
+ # {https://design-system.service.gov.uk/components/text-input GDS - Components - Text Input}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
15
+
16
+ module Input
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK input component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the input
22
+ # @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
23
+ #
24
+ # @option govuk_input_options [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_label_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the input tag and find the error message
28
+ # @option govuk_input_options [Hash] :form_group see {govuk_field}
29
+ # @option govuk_input_options [Hash] :label see {govuk_field}
30
+ # @option govuk_input_options [Hash] :hint see {govuk_field}
31
+ # @option govuk_input_options [Hash] :input ({}) the options that will be used when rendering the input.
32
+ # See {_govuk_input_field} for more details.
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_input(attribute, **govuk_input_options)
38
+ govuk_field(:input, attribute, **govuk_input_options) do |govuk_field_options, error_message|
39
+ concat(_govuk_input_field(error_message, **govuk_field_options) do
40
+ field_type = get_field_type(govuk_field_options[:field_type])
41
+ govuk_field_options[:value] = govuk_input_options[:model].send(attribute) if govuk_input_options[:model]
42
+
43
+ if govuk_input_options[:form]
44
+ govuk_input_form(govuk_input_options[:form], field_type, attribute, **govuk_field_options)
45
+ else
46
+ govuk_input_tag(field_type, attribute, **govuk_field_options)
47
+ end
48
+ end)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ # Generates the input HTML for {govuk_input}
55
+ #
56
+ # @param field_type [String] the inout field type
57
+ # @param attribute [String, Symbol] the attribute of the input
58
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
59
+ #
60
+ # @option (see _govuk_input_field)
61
+ #
62
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input}
63
+
64
+ def govuk_input_tag(field_type, attribute, **govuk_text_input_options)
65
+ send("#{field_type}_tag", attribute, govuk_text_input_options[:value], **govuk_text_input_options[:attributes])
66
+ end
67
+
68
+ # Generates the input HTML for {govuk_input} when there is a ActionView::Helpers::FormBuilder
69
+ #
70
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input tag
71
+ # @param (see govuk_input_tag)
72
+ #
73
+ # @option (see _govuk_input_field)
74
+ #
75
+ # @return (see govuk_input_tag)
76
+
77
+ def govuk_input_form(form, field_type, attribute, **govuk_text_input_options)
78
+ form.send(field_type, attribute, **govuk_text_input_options[:attributes])
79
+ end
80
+
81
+ # Wrapper method used by {govuk_input} to generate the Input HTML
82
+ #
83
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
84
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
85
+ #
86
+ # @option govuk_text_input_options [String] :classes additional CSS classes for the input HTML
87
+ # @option govuk_text_input_options [String] :value (nil) the value of the input
88
+ # @option govuk_text_input_options [Symbol] :field_type the type of the input field, see {get_field_type} for options
89
+ # @option govuk_text_input_options [Hash] :prefix (nil) optional prefix for the input field. See {govuk_fix} for more details.
90
+ # @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
91
+ # @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
92
+ #
93
+ # @yield the input HTML generated by {govuk_input_tag} or {govuk_input_form}
94
+ #
95
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used by {govuk_input}
96
+
97
+ def _govuk_input_field(error_message, **govuk_text_input_options)
98
+ initialise_attributes_and_set_classes(govuk_text_input_options, "govuk-input #{'govuk-input--error' if error_message}".rstrip)
99
+
100
+ input_html = yield
101
+
102
+ prefix = govuk_text_input_options[:prefix]
103
+ suffix = govuk_text_input_options[:suffix]
104
+
105
+ if prefix || suffix
106
+ tag.div(class: 'govuk-input__wrapper') do
107
+ concat(govuk_fix('pre', prefix)) if prefix
108
+ concat(input_html)
109
+ concat(govuk_fix('suf', suffix)) if suffix
110
+ end
111
+ else
112
+ input_html
113
+ end
114
+ end
115
+
116
+ # Generates the prefix and suffix HTML for {_govuk_input_field}
117
+ #
118
+ # @param fix [String] either +"pre"+ or +"suf"+
119
+ # @param govuk_fix_options [Hash] options that will be used in customising the HTML
120
+ #
121
+ # @option govuk_fix_options [String] :classes additional CSS classes for the input HTML
122
+ # @option govuk_text_input_options [Hash] :attributes ({ aria: { hidden: true } }) any additional attributes that will added as part of the HTML
123
+ #
124
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {_govuk_input_field}
125
+
126
+ def govuk_fix(fix, govuk_fix_options)
127
+ initialise_attributes_and_set_classes(govuk_fix_options, "govuk-input__#{fix}fix")
128
+
129
+ (govuk_fix_options[:attributes][:aria] ||= {})[:hidden] = true
130
+
131
+ tag.div(govuk_fix_options[:text], **govuk_fix_options[:attributes])
132
+ end
133
+
134
+ # Returns the field type used to create the rails input tag
135
+ #
136
+ # @param field_type [Symbol] the type of the field, defaults to text
137
+ # Allowed values are:
138
+ # - +:email+
139
+ # - +:password+
140
+
141
+ def get_field_type(field_type)
142
+ case field_type
143
+ when :email, :password
144
+ :"#{field_type}_field"
145
+ else
146
+ :text_field
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end