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,68 @@
1
+ require_relative '../base'
2
+ require_relative 'error_summary/item'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ # = GOV.UK Error Summary
7
+ #
8
+ # This is used to generate the error summary component from the
9
+ # {https://design-system.service.gov.uk/components/error-summary GDS - Components - Error summary}
10
+ #
11
+ # @!attribute [r] title
12
+ # @return [String] Heading for the error summary
13
+ # @!attribute [r] error_summary_items
14
+ # @return [Array<Item>] An array of the initialised error summary items
15
+ # @!attribute [r] description
16
+ # @return [String] Text to use for the description of the errors
17
+
18
+ class ErrorSummary < Base
19
+ private
20
+
21
+ attr_reader :title, :error_summary_items, :description
22
+
23
+ public
24
+
25
+ # @param title [String] text to use for the heading of the error summary block
26
+ # @param error_summary_items [Array<Hash>] the list of errors to include in the summary. See {Components::GovUK::ErrorSummary::Item#initialize Item#initialize}
27
+ # @param description [String] optional text to use for the description of the errors
28
+ # @param options [Hash] options that will be used in customising the HTML
29
+ #
30
+ # @option options [String] :classes additional CSS classes for the error summary HTML
31
+ # @option options [Hash] :attributes ({}) any additional attributes that will be added as part of the HTML
32
+
33
+ def initialize(title:, error_summary_items:, description: nil, **options)
34
+ super(**options)
35
+
36
+ @title = title
37
+ @error_summary_items = error_summary_items.map { |error_summary_item| Item.new(context: @context, **error_summary_item) }
38
+ @description = description
39
+ end
40
+
41
+ # rubocop:disable Metrics/AbcSize
42
+
43
+ # Generates the HTML for the GOV.UK Error summary component
44
+ #
45
+ # @return [ActiveSupport::SafeBuffer]
46
+
47
+ def render
48
+ tag.div(**options[:attributes]) do
49
+ tag.div(role: 'alert') do
50
+ concat(tag.h2(title, class: 'govuk-error-summary__title'))
51
+ concat(tag.div(class: 'govuk-error-summary__body') do
52
+ concat(tag.p(description)) if description
53
+ concat(tag.ul(class: 'govuk-list govuk-error-summary__list') do
54
+ error_summary_items.each { |error_summary_item| concat(error_summary_item.render) }
55
+ end)
56
+ end)
57
+ end
58
+ end
59
+ end
60
+
61
+ # rubocop:enable Metrics/AbcSize
62
+
63
+ # The default attributes for the error summary
64
+
65
+ DEFAULT_ATTRIBUTES = { class: 'govuk-error-summary', data: { module: 'govuk-error-summary' } }.freeze
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,127 @@
1
+ require_relative 'textarea'
2
+ require_relative '../../hint'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Input < Field
8
+ # = GOV.UK Character count
9
+ #
10
+ # This is used for generating the character count component from the
11
+ # {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
12
+ #
13
+ # This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
14
+ #
15
+ # @!attribute [r] textarea
16
+ # @return [Textarea] The initialised textarea
17
+ # @!attribute [r] fallback_hint
18
+ # @return [Hint] The initialised character count fallback hint
19
+ # @!attribute [r] character_count_html_options
20
+ # @return [Hash] HTML options for the character count
21
+
22
+ class CharacterCount
23
+ include ActionView::Context
24
+ include ActionView::Helpers
25
+
26
+ private
27
+
28
+ attr_reader :textarea, :fallback_hint, :character_count_html_options
29
+
30
+ public
31
+
32
+ # @param (see CCS::Components::GovUK::Field::Input::Textarea#initialize)
33
+ # @param character_count_options [Hash] options for the character count
34
+ # @param context [ActionView::Base] the view context
35
+ #
36
+ # @option (see CCS::Components::GovUK::Field::Input::Textarea#initialize)
37
+ #
38
+ # @option (see initialise_character_count_html_options)
39
+
40
+ def initialize(attribute:, character_count_options:, context:, **options)
41
+ character_count_attribute = options[:form] ? "#{options[:form].object_name}_#{attribute}" : attribute
42
+
43
+ initialise_textarea(attribute, character_count_attribute, context, options)
44
+ initialise_character_count_html_options(character_count_options)
45
+ initialise_fallback_hint(character_count_attribute, context, character_count_options)
46
+ end
47
+
48
+ # Generates the HTML for the GOV.UK Character count component
49
+ #
50
+ # @return [ActiveSupport::SafeBuffer]
51
+
52
+ def render
53
+ tag.div(**character_count_html_options) do
54
+ concat(textarea.render)
55
+ concat(fallback_hint.render)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ # Initialises the Textarea for the character count
62
+ #
63
+ # @param attribute [Symbol] the attribute of the field
64
+ # @param character_count_attribute [String] the name of the field as it will appear in the textarea
65
+ # @param context [ActionView::Base] the view context
66
+ # @param options [Hash] options that will be used for the textarea
67
+ #
68
+ # @option (see CCS::Components::GovUK::Field::Input::Textarea#initialize)
69
+
70
+ def initialise_textarea(attribute, character_count_attribute, context, options)
71
+ ((options[:attributes] ||= {})[:aria] ||= {})[:describedby] = [options.dig(:attributes, :aria, :describedby), "#{character_count_attribute}-info"].compact.join(' ')
72
+ options[:classes] = "#{options[:classes]} govuk-js-character-count".lstrip
73
+
74
+ @textarea = Textarea.new(attribute: attribute, context: context, **options)
75
+ end
76
+
77
+ # Initialises the charcter count options
78
+ #
79
+ # @param character_count_options [Hash] options for the charcter count
80
+ #
81
+ # @option character_count_options [String] :maxlength the maximum number of characters.
82
+ # If +maxwords+ is set, this is not required.
83
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
84
+ # @option character_count_options [String] :maxwords the maximum number of words.
85
+ # If maxlength is set, this is not required.
86
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
87
+ # @option character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
88
+ # If this attribute is set, the count message will be hidden by default.
89
+ # @option character_count_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text:
90
+ # - +:count_message+ replaced the default text for the count message.
91
+ # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
92
+ # - +classes+ additional CSS classes for the fallback hint HTML
93
+
94
+ def initialise_character_count_html_options(character_count_options)
95
+ character_count_html_options = { class: 'govuk-character-count', data: { module: 'govuk-character-count' } }
96
+
97
+ %i[maxlength threshold maxwords].each do |data_attribute|
98
+ character_count_html_options[:data][data_attribute] = character_count_options[data_attribute].to_s if character_count_options[data_attribute]
99
+ end
100
+
101
+ @character_count_html_options = character_count_html_options
102
+ end
103
+
104
+ # Initialises the charcter count fall back hint
105
+ #
106
+ # @param character_count_attribute [String] the name of the field as it will appear in the textarea
107
+ # @param context [ActionView::Base] the view context
108
+ # @param (see initialise_character_count_html_options)
109
+ #
110
+ # @option (see initialise_character_count_html_options)
111
+
112
+ def initialise_fallback_hint(character_count_attribute, context, character_count_options)
113
+ fallback_hint = character_count_options[:fallback_hint] || {}
114
+
115
+ fallback_hint_length = character_count_options[:maxwords] || character_count_options[:maxlength]
116
+ fallback_hint_default = "You can enter up to %<count>s #{character_count_options[:maxwords] ? 'words' : 'characters'}"
117
+
118
+ text = format(fallback_hint[:count_message] || fallback_hint_default, count: fallback_hint_length)
119
+ classes = "#{fallback_hint[:classes]} govuk-character-count__message".lstrip
120
+
121
+ @fallback_hint = Hint.new(text: text, classes: classes, attributes: { id: "#{character_count_attribute}-info" }, context: context)
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,42 @@
1
+ require_relative '../input'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Input < Field
7
+ # = GOV.UK File Upload
8
+ #
9
+ # This is used for generating the file upload component from the
10
+ # {https://design-system.service.gov.uk/components/file-upload GDS - Components - File Upload}
11
+
12
+ class FileUpload < Input
13
+ # @param (see CCS::Components::GovUK::Field::Input#initialize)
14
+ #
15
+ # @option (see CCS::Components::GovUK::Field::Input#initialize)
16
+
17
+ def initialize(attribute:, **options)
18
+ super(attribute: attribute, **options)
19
+ end
20
+
21
+ # Generates the HTML for the GOV.UK File Upload component
22
+ #
23
+ # @return [ActiveSupport::SafeBuffer]
24
+
25
+ def render
26
+ super() do
27
+ if options[:form]
28
+ options[:form].file_field(attribute, **options[:attributes])
29
+ else
30
+ context.file_field_tag(attribute, **options[:attributes])
31
+ end
32
+ end
33
+ end
34
+
35
+ # The default attributes for the file upload
36
+
37
+ DEFAULT_ATTRIBUTES = { class: 'govuk-file-upload' }.freeze
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,80 @@
1
+ require_relative '../input'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Input < Field
7
+ # = GOV.UK Select
8
+ #
9
+ # This is used for generating the select component from the
10
+ # {https://design-system.service.gov.uk/components/select GDS - Components - Select}
11
+ #
12
+ # @!attribute [r] items
13
+ # @return [Array<Hash>] The select items
14
+ # @!attribute [r] selected
15
+ # @return [String] The selected option
16
+
17
+ class Select < Input
18
+ private
19
+
20
+ attr_reader :items, :selected
21
+
22
+ public
23
+
24
+ # @param (see CCS::Components::GovUK::Field::Input#initialize)
25
+ # @param items [Array<Hash>] array of options for select.
26
+ # The options are:
27
+ # - +:text+ the text of the option item. If this is blank the value is used
28
+ # - +:value+ the value of the option item
29
+ # - +:attributes+ any additional attributes that will added as part of the option HTML
30
+ # @param selected [String] the selected option
31
+ #
32
+ # @option (see CCS::Components::GovUK::Field::Input#initialize)
33
+
34
+ def initialize(attribute:, items:, selected: nil, **options)
35
+ super(attribute: attribute, **options)
36
+
37
+ @items = items.map do |item|
38
+ [
39
+ item[:text] || item[:value],
40
+ item[:value],
41
+ (item[:attributes] || {})
42
+ ]
43
+ end
44
+ @selected = @options[:model] ? @options[:model].send(attribute) : selected
45
+ end
46
+
47
+ # Generates the HTML for the GOV.UK Select component
48
+ #
49
+ # @return [ActiveSupport::SafeBuffer]
50
+
51
+ def render
52
+ super() do
53
+ if options[:form]
54
+ options[:form].select(
55
+ attribute,
56
+ items,
57
+ {},
58
+ **options[:attributes]
59
+ )
60
+ else
61
+ context.select_tag(
62
+ attribute,
63
+ context.options_for_select(
64
+ items,
65
+ selected
66
+ ),
67
+ **options[:attributes]
68
+ )
69
+ end
70
+ end
71
+ end
72
+
73
+ # The default attributes for the select
74
+
75
+ DEFAULT_ATTRIBUTES = { class: 'govuk-select' }.freeze
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../text_input'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Input < Field
7
+ class TextInput < Input
8
+ # = GOV.UK Text Input prefix/suffix
9
+ #
10
+ # This is used to generate the prefix/suffix HTML for a text input
11
+ #
12
+ # @!attribute [r] text
13
+ # @return [String] Text for the prefix/suffix
14
+
15
+ class Fix < Base
16
+ private
17
+
18
+ attr_reader :text
19
+
20
+ public
21
+
22
+ # @param text [String] the text for the prefix/suffix
23
+ # @param fix [String] either +"pre"+ or +"suf"+
24
+ #
25
+ # @param options [Hash] options that will be used in customising the HTML
26
+ #
27
+ # @option options [String] :classes additional CSS classes for the prefix/suffix HTML
28
+ # @option options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
29
+
30
+ def initialize(text:, fix:, **options)
31
+ super(**options)
32
+
33
+ @options[:attributes][:class] = "govuk-input__#{fix}fix #{@options[:attributes][:class]}".rstrip
34
+ (@options[:attributes][:aria] ||= {})[:hidden] = true
35
+
36
+ @text = text
37
+ end
38
+
39
+ # Generates the HTML for the GOV.UK Text Input prefix/suffix
40
+ #
41
+ # @yield (see CCS::Components::GovUK::Field#render)
42
+ #
43
+ # @return [ActiveSupport::SafeBuffer]
44
+
45
+ def render
46
+ tag.div(text, **options[:attributes])
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,95 @@
1
+ require_relative '../input'
2
+ require_relative 'text_input/fix'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Input < Field
8
+ # = GOV.UK Input
9
+ #
10
+ # This is used for generating the input component from the
11
+ # {https://design-system.service.gov.uk/components/text-input GDS - Components - Text Input}
12
+ #
13
+ # @!attribute [r] field_type
14
+ # @return [Symbol,String] The input field type
15
+ # @!attribute [r] value
16
+ # @return [String] The value of the text input
17
+ # @!attribute [r] prefix
18
+ # @return [Fix] The initialised prefix
19
+ # @!attribute [r] suffix
20
+ # @return [Fix] The initialised suffix
21
+
22
+ class TextInput < Input
23
+ private
24
+
25
+ attr_reader :field_type, :value, :prefix, :suffix
26
+
27
+ public
28
+
29
+ # rubocop:disable Metrics/ParameterLists
30
+
31
+ # @param (see CCS::Components::GovUK::Field::Input#initialize)
32
+ # @param field_type [Symbol,String] (:text) the input field type
33
+ # @param value [String] the value if the input
34
+ # @param prefix [Hash] optional prefix for the input field,
35
+ # see {CCS::Components::GovUK::Field::Input::TextInput::Fix#initialize Fix#initialize} for more details.
36
+ # @param suffix [Hash] optional suffix for the input field,
37
+ # see {CCS::Components::GovUK::Field::Input::TextInput::Fix#initialize Fix#initialize} for more details.
38
+ #
39
+ # @option (see CCS::Components::GovUK::Field::Input#initialize)
40
+
41
+ def initialize(attribute:, field_type: :text, value: nil, prefix: nil, suffix: nil, **options)
42
+ super(attribute: attribute, **options)
43
+
44
+ @field_type = :"#{field_type}_field"
45
+ @value = @options[:model] ? @options[:model].send(attribute) : value
46
+ @prefix = Fix.new(fix: 'pre', context: @context, **prefix) if prefix
47
+ @suffix = Fix.new(fix: 'suf', context: @context, **suffix) if suffix
48
+ end
49
+
50
+ # rubocop:enable Metrics/ParameterLists
51
+
52
+ # Generates the HTML for the GOV.UK Text Input component
53
+ #
54
+ # @return [ActiveSupport::SafeBuffer]
55
+
56
+ def render
57
+ super() do
58
+ text_input_wrapper do
59
+ if options[:form]
60
+ options[:form].send(field_type, attribute, **options[:attributes])
61
+ else
62
+ context.send("#{field_type}_tag", attribute, value, **options[:attributes])
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ # The default attributes for the text input
69
+
70
+ DEFAULT_ATTRIBUTES = { class: 'govuk-input' }.freeze
71
+
72
+ private
73
+
74
+ # Wrapper method used by {render} to wrap the text input with a prefix or suffix if they exist
75
+ #
76
+ # @yield the text input HTML
77
+ #
78
+ # @return [ActiveSupport::SafeBuffer]
79
+
80
+ def text_input_wrapper
81
+ if prefix || suffix
82
+ tag.div(class: 'govuk-input__wrapper') do
83
+ concat(prefix.render) if prefix
84
+ concat(yield)
85
+ concat(suffix.render) if suffix
86
+ end
87
+ else
88
+ yield
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '../input'
2
+
3
+ module CCS::Components
4
+ module GovUK
5
+ class Field < Base
6
+ class Input < Field
7
+ # = GOV.UK Textarea
8
+ #
9
+ # This helper is used for generating the textarea component from the
10
+ # {https://design-system.service.gov.uk/components/textarea GDS - Components - Textarea}
11
+ #
12
+ # @!attribute [r] content
13
+ # @return [String] The content of the textarea
14
+
15
+ class Textarea < Input
16
+ private
17
+
18
+ attr_reader :content
19
+
20
+ public
21
+
22
+ # @param (see CCS::Components::GovUK::Field::Input#initialize)
23
+ # @param content [String] the content of the textarea
24
+ # @param rows [Integer] the number of rows for the text area
25
+ #
26
+ # @option (see CCS::Components::GovUK::Field::Input#initialize)
27
+
28
+ def initialize(attribute:, content: nil, rows: 5, **options)
29
+ super(attribute: attribute, **options)
30
+
31
+ @options[:attributes][:rows] ||= rows
32
+
33
+ @content = @options[:model] ? @options[:model].send(attribute) : content
34
+ end
35
+
36
+ # Generates the HTML for the GOV.UK Textarea component
37
+ #
38
+ # @return [ActiveSupport::SafeBuffer]
39
+
40
+ def render
41
+ super() do
42
+ if options[:form]
43
+ options[:form].text_area(attribute, **options[:attributes])
44
+ else
45
+ context.text_area_tag(attribute, content, **options[:attributes])
46
+ end
47
+ end
48
+ end
49
+
50
+ # The default attributes for the textarea
51
+
52
+ DEFAULT_ATTRIBUTES = { class: 'govuk-textarea' }.freeze
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,60 @@
1
+ require_relative '../field'
2
+ require_relative '../label'
3
+
4
+ module CCS::Components
5
+ module GovUK
6
+ class Field < Base
7
+ # = GOV.UK Field Input
8
+ #
9
+ # This class is used to create a form for an individual field, e.g. text input or select.
10
+ # It will wrap the input in the form group and:
11
+ # - display the label
12
+ # - display the hint (if there is one)
13
+ # - find and display the error message (if there is one)
14
+ #
15
+ # @!attribute [r] label
16
+ # @return [Label] The initialised label
17
+
18
+ class Input < Field
19
+ private
20
+
21
+ attr_reader :label
22
+
23
+ public
24
+
25
+ # @param (see CCS::Components::GovUK::Field#initialize)
26
+ # @param label [Hash] attributes for the label, see {CCS::Components::GovUK::Label#initialize Label#initialize} for more details.
27
+ #
28
+ # @option (see CCS::Components::GovUK::Field#initialize)
29
+
30
+ def initialize(attribute:, label:, **options)
31
+ super(attribute: attribute, **options)
32
+
33
+ set_described_by(@options, @attribute, @error_message, options[:hint])
34
+
35
+ @options[:attributes][:class] << " #{self.class::DEFAULT_ATTRIBUTES[:class]}--error" if @error_message
36
+
37
+ field_id = @options.dig(:attributes, :id)
38
+ (label[:attributes] ||= {})[:for] = field_id if field_id
39
+
40
+ @label = Label.new(attribute: attribute, form: @options[:form], context: @context, **label)
41
+ end
42
+
43
+ # Generates the HTML to wrap arround a GDS form component
44
+ #
45
+ # @yield (see CCS::Components::GovUK::Field#render)
46
+ #
47
+ # @return [ActiveSupport::SafeBuffer]
48
+
49
+ def render
50
+ super() do |display_error_message|
51
+ concat(label.render)
52
+ concat(hint.render) if hint
53
+ concat(display_error_message)
54
+ concat(yield)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,67 @@
1
+ require_relative '../input'
2
+ require_relative 'item/divider'
3
+ require_relative 'item/checkbox/form'
4
+ require_relative 'item/checkbox/tag'
5
+
6
+ module CCS::Components
7
+ module GovUK
8
+ class Field < Base
9
+ class Inputs < Field
10
+ # = GOV.UK Checkboxes
11
+ #
12
+ # This is used for generating the checkboxes component from the
13
+ # {https://design-system.service.gov.uk/components/checkboxes GDS - Components - Checkboxes}
14
+ #
15
+ # @!attribute [r] checkbox_items
16
+ # @return [Array<Item::Divider|Item::Checkbox|Item::Checkbox>] An array of the initialised checkbox items
17
+
18
+ class Checkboxes < Inputs
19
+ private
20
+
21
+ attr_reader :checkbox_items
22
+
23
+ public
24
+
25
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
26
+
27
+ # @param (see CCS::Components::GovUK::Field::Inputs#initialize)
28
+ # @param checkbox_items [Array<Hash>] an array of options for the checkboxes.
29
+ # See {Components::GovUK::Field::Inputs::Item::Checkbox#initialize Checkbox#initialize} for details of the items in the array.
30
+ #
31
+ # @option (see CCS::Components::GovUK::Field::Inputs#initialize)
32
+
33
+ def initialize(attribute:, checkbox_items:, **options)
34
+ super(attribute: attribute, **options)
35
+
36
+ if @options[:model] || @options[:form]
37
+ values = (@options[:model] || @options[:form].object).send(attribute) || []
38
+ checkbox_items.each { |checkbox_item| checkbox_item[:checked] = values.include?(checkbox_item[:value]) }
39
+ end
40
+
41
+ checkbox_item_class = @options[:form] ? Item::Checkbox::Form : Inputs::Item::Checkbox::Tag
42
+
43
+ @checkbox_items = checkbox_items.map { |checkbox_item| checkbox_item[:divider] ? Item::Divider.new(divider: checkbox_item[:divider], type: 'checkboxes') : checkbox_item_class.new(attribute: attribute, form: @options[:form], context: @context, **checkbox_item) }
44
+ end
45
+
46
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
47
+
48
+ # Generates the HTML for the GOV.UK Checkboxes component
49
+ #
50
+ # @return [ActiveSupport::SafeBuffer]
51
+
52
+ def render
53
+ super() do
54
+ tag.div(**options[:attributes]) do
55
+ checkbox_items.each { |checkbox_item| concat(checkbox_item.render) }
56
+ end
57
+ end
58
+ end
59
+
60
+ # The default attributes for the checkboxes
61
+
62
+ DEFAULT_ATTRIBUTES = { class: 'govuk-checkboxes', data: { module: 'govuk-checkboxes' } }.freeze
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end