ccs-frontend_helpers 0.1.0 → 0.1.1.rc.1

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 +11 -2
  3. data/.ruby-version +1 -0
  4. data/CHANGELOG.md +4 -1
  5. data/Gemfile +15 -1
  6. data/Gemfile.lock +153 -114
  7. data/ccs-frontend_helpers.gemspec +1 -11
  8. data/lib/ccs/components/base.rb +64 -0
  9. data/lib/ccs/components/ccs/dashboard_section/panel.rb +57 -0
  10. data/lib/ccs/components/ccs/dashboard_section.rb +71 -0
  11. data/lib/ccs/components/ccs/footer/link.rb +55 -0
  12. data/lib/ccs/components/ccs/footer/meta.rb +59 -0
  13. data/lib/ccs/components/ccs/footer/navigation.rb +60 -0
  14. data/lib/ccs/components/ccs/footer.rb +95 -0
  15. data/lib/ccs/components/ccs/header/link.rb +60 -0
  16. data/lib/ccs/components/ccs/header/navigation.rb +98 -0
  17. data/lib/ccs/components/ccs/header/service_authentication.rb +53 -0
  18. data/lib/ccs/components/ccs/header.rb +110 -0
  19. data/lib/ccs/{frontend_helpers/ccs_frontend → components/ccs}/logo.rb +10 -12
  20. data/lib/ccs/components/govuk/accordion/section/content.rb +53 -0
  21. data/lib/ccs/components/govuk/accordion/section/header.rb +57 -0
  22. data/lib/ccs/components/govuk/accordion/section.rb +61 -0
  23. data/lib/ccs/components/govuk/accordion.rb +58 -0
  24. data/lib/ccs/components/govuk/back_link.rb +51 -0
  25. data/lib/ccs/components/govuk/breadcrumbs/link.rb +57 -0
  26. data/lib/ccs/components/govuk/breadcrumbs.rb +55 -0
  27. data/lib/ccs/components/govuk/button.rb +107 -0
  28. data/lib/ccs/components/govuk/cookie_banner/action.rb +60 -0
  29. data/lib/ccs/components/govuk/cookie_banner/message.rb +80 -0
  30. data/lib/ccs/components/govuk/cookie_banner.rb +55 -0
  31. data/lib/ccs/components/govuk/details.rb +53 -0
  32. data/lib/ccs/components/govuk/error_message.rb +60 -0
  33. data/lib/ccs/components/govuk/error_summary/item.rb +54 -0
  34. data/lib/ccs/components/govuk/error_summary.rb +70 -0
  35. data/lib/ccs/components/govuk/field/input/character_count.rb +129 -0
  36. data/lib/ccs/components/govuk/field/input/file_upload.rb +44 -0
  37. data/lib/ccs/components/govuk/field/input/select.rb +82 -0
  38. data/lib/ccs/components/govuk/field/input/text_input/fix.rb +55 -0
  39. data/lib/ccs/components/govuk/field/input/text_input.rb +97 -0
  40. data/lib/ccs/components/govuk/field/input/textarea.rb +59 -0
  41. data/lib/ccs/components/govuk/field/input.rb +62 -0
  42. data/lib/ccs/components/govuk/field/inputs/checkboxes.rb +69 -0
  43. data/lib/ccs/components/govuk/field/inputs/date_input/item.rb +65 -0
  44. data/lib/ccs/components/govuk/field/inputs/date_input.rb +89 -0
  45. data/lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb +47 -0
  46. data/lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb +43 -0
  47. data/lib/ccs/components/govuk/field/inputs/item/checkbox.rb +50 -0
  48. data/lib/ccs/components/govuk/field/inputs/item/divider.rb +50 -0
  49. data/lib/ccs/components/govuk/field/inputs/item/radio/form.rb +44 -0
  50. data/lib/ccs/components/govuk/field/inputs/item/radio/tag.rb +43 -0
  51. data/lib/ccs/components/govuk/field/inputs/item/radio.rb +50 -0
  52. data/lib/ccs/components/govuk/field/inputs/item.rb +111 -0
  53. data/lib/ccs/components/govuk/field/inputs/radios.rb +69 -0
  54. data/lib/ccs/components/govuk/field/inputs.rb +57 -0
  55. data/lib/ccs/components/govuk/field.rb +108 -0
  56. data/lib/ccs/components/govuk/fieldset/legend.rb +65 -0
  57. data/lib/ccs/components/govuk/fieldset.rb +54 -0
  58. data/lib/ccs/components/govuk/footer/link.rb +55 -0
  59. data/lib/ccs/components/govuk/footer/meta.rb +59 -0
  60. data/lib/ccs/components/govuk/footer/navigation.rb +60 -0
  61. data/lib/ccs/components/govuk/footer.rb +131 -0
  62. data/lib/ccs/components/govuk/form_group.rb +60 -0
  63. data/lib/ccs/components/govuk/header/link.rb +56 -0
  64. data/lib/ccs/components/govuk/header/navigation.rb +71 -0
  65. data/lib/ccs/components/govuk/header.rb +107 -0
  66. data/lib/ccs/components/govuk/hint.rb +49 -0
  67. data/lib/ccs/components/govuk/inset_text.rb +52 -0
  68. data/lib/ccs/components/govuk/label.rb +85 -0
  69. data/lib/ccs/components/govuk/notification_banner.rb +107 -0
  70. data/lib/ccs/components/govuk/pagination/increment/next.rb +44 -0
  71. data/lib/ccs/components/govuk/pagination/increment/previous.rb +43 -0
  72. data/lib/ccs/components/govuk/pagination/increment.rb +98 -0
  73. data/lib/ccs/components/govuk/pagination/item/ellipsis.rb +28 -0
  74. data/lib/ccs/components/govuk/pagination/item/form.rb +49 -0
  75. data/lib/ccs/components/govuk/pagination/item/tag.rb +47 -0
  76. data/lib/ccs/components/govuk/pagination/item.rb +64 -0
  77. data/lib/ccs/components/govuk/pagination.rb +96 -0
  78. data/lib/ccs/components/govuk/panel.rb +62 -0
  79. data/lib/ccs/components/govuk/phase_banner.rb +69 -0
  80. data/lib/ccs/components/govuk/skip_link.rb +51 -0
  81. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list/item.rb +53 -0
  82. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list.rb +50 -0
  83. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/paragraph.rb +45 -0
  84. data/lib/ccs/components/govuk/step_by_step_navigation/section/content.rb +63 -0
  85. data/lib/ccs/components/govuk/step_by_step_navigation/section/heading.rb +69 -0
  86. data/lib/ccs/components/govuk/step_by_step_navigation/section.rb +56 -0
  87. data/lib/ccs/components/govuk/step_by_step_navigation.rb +59 -0
  88. data/lib/ccs/components/govuk/summary_list/action/link.rb +59 -0
  89. data/lib/ccs/components/govuk/summary_list/card/actions.rb +59 -0
  90. data/lib/ccs/components/govuk/summary_list/card/title.rb +51 -0
  91. data/lib/ccs/components/govuk/summary_list/card.rb +63 -0
  92. data/lib/ccs/components/govuk/summary_list/row/actions.rb +59 -0
  93. data/lib/ccs/components/govuk/summary_list/row/key.rb +47 -0
  94. data/lib/ccs/components/govuk/summary_list/row/value.rb +47 -0
  95. data/lib/ccs/components/govuk/summary_list/row.rb +67 -0
  96. data/lib/ccs/components/govuk/summary_list.rb +74 -0
  97. data/lib/ccs/components/govuk/table/body/data_cell.rb +53 -0
  98. data/lib/ccs/components/govuk/table/body/head_cell.rb +52 -0
  99. data/lib/ccs/components/govuk/table/header/head_cell.rb +54 -0
  100. data/lib/ccs/components/govuk/table.rb +111 -0
  101. data/lib/ccs/components/govuk/tabs/panel.rb +58 -0
  102. data/lib/ccs/components/govuk/tabs/tab.rb +56 -0
  103. data/lib/ccs/components/govuk/tabs.rb +66 -0
  104. data/lib/ccs/components/govuk/tag.rb +51 -0
  105. data/lib/ccs/components/govuk/warning_text.rb +61 -0
  106. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_section.rb +27 -0
  107. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +6 -119
  108. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +6 -183
  109. data/lib/ccs/frontend_helpers/ccs_frontend.rb +2 -4
  110. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +8 -95
  111. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +6 -17
  112. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +6 -54
  113. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +6 -105
  114. data/lib/ccs/frontend_helpers/govuk_frontend/character_count.rb +28 -0
  115. data/lib/ccs/frontend_helpers/govuk_frontend/checkboxes.rb +28 -0
  116. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +6 -114
  117. data/lib/ccs/frontend_helpers/govuk_frontend/date_input.rb +28 -0
  118. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +7 -23
  119. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +11 -32
  120. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +13 -65
  121. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +7 -48
  122. data/lib/ccs/frontend_helpers/govuk_frontend/file_upload.rb +28 -0
  123. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +6 -161
  124. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +13 -24
  125. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +6 -139
  126. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +8 -17
  127. data/lib/ccs/frontend_helpers/govuk_frontend/input.rb +28 -0
  128. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +7 -21
  129. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +7 -70
  130. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +7 -113
  131. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +6 -314
  132. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +7 -28
  133. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +8 -27
  134. data/lib/ccs/frontend_helpers/govuk_frontend/radios.rb +28 -0
  135. data/lib/ccs/frontend_helpers/govuk_frontend/select.rb +28 -0
  136. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +6 -18
  137. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +7 -186
  138. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +6 -204
  139. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +11 -100
  140. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +6 -73
  141. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +7 -21
  142. data/lib/ccs/frontend_helpers/govuk_frontend/textarea.rb +28 -0
  143. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +7 -30
  144. data/lib/ccs/frontend_helpers/govuk_frontend.rb +17 -19
  145. data/lib/ccs/frontend_helpers/version.rb +1 -1
  146. metadata +117 -147
  147. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +0 -79
  148. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +0 -165
  149. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +0 -200
  150. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +0 -153
  151. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +0 -83
  152. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +0 -153
  153. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +0 -201
  154. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +0 -124
  155. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +0 -106
  156. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +0 -213
  157. data/lib/ccs/frontend_helpers/shared_methods.rb +0 -27
@@ -0,0 +1,55 @@
1
+ require_relative '../base'
2
+ require_relative 'cookie_banner/message'
3
+
4
+ module CCS
5
+ module Components
6
+ module GovUK
7
+ # = GOV.UK Cookie Banner
8
+ #
9
+ # This is used to generate the cookie banner component from the
10
+ # {https://design-system.service.gov.uk/components/cookie-banner GDS - Components - Cookie banner}
11
+ #
12
+ # @!attribute [r] messages
13
+ # @return [Array<Message>] An array of the initialised cookie banner messages
14
+
15
+ class CookieBanner < Base
16
+ private
17
+
18
+ attr_reader :messages
19
+
20
+ public
21
+
22
+ # @param messages [Array<Hash>] An array of messages for the cookie banner.
23
+ # See {Components::GovUK::CookieBanner::Message#initialize Message#initialize} for details of the items in the array.
24
+ # @param options [Hash] options that will be used in customising the HTML
25
+ #
26
+ # @option options [String] :classes additional CSS classes for the cookie banner HTML
27
+ # @option options [Hash] :attributes any additional attributes that will added as part of the HTML
28
+
29
+ def initialize(messages:, **options)
30
+ super(**options)
31
+
32
+ (@options[:attributes][:data] ||= {})[:nosnippet] = 'true'
33
+ @options[:attributes][:role] = 'region'
34
+ (@options[:attributes][:aria] ||= {})[:label] ||= 'Cookie banner'
35
+
36
+ @messages = messages.map { |message| Message.new(context: @context, **message) }
37
+ end
38
+
39
+ # Generates the HTML for the GOV.UK Cookie banner component
40
+ #
41
+ # @return [ActiveSupport::SafeBuffer]
42
+
43
+ def render
44
+ tag.div(**options[:attributes]) do
45
+ messages.each { |message| concat(message.render) }
46
+ end
47
+ end
48
+
49
+ # The default attributes for the cookie banner
50
+
51
+ DEFAULT_ATTRIBUTES = { class: 'govuk-cookie-banner' }.freeze
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../base'
2
+
3
+ module CCS
4
+ module Components
5
+ module GovUK
6
+ # = GOV.UK Details
7
+ #
8
+ # This is used to generate the details component from the
9
+ # {https://design-system.service.gov.uk/components/details GDS - Components - Details}
10
+ #
11
+ # @!attribute [r] summary_text
12
+ # @return [String] Summary text for the details element
13
+
14
+ class Details < Base
15
+ private
16
+
17
+ attr_reader :summary_text
18
+
19
+ public
20
+
21
+ # @param summary_text [String] the summary text for the details element
22
+ # @param options [Hash] options that will be used in customising the HTML
23
+ #
24
+ # @option options [String] :classes additional CSS classes for the details HTML
25
+ # @option options [Hash] :attributes ({ data: { module: 'govuk-details' } }) any additional attributes that will added as part of the HTML
26
+
27
+ def initialize(summary_text:, **options)
28
+ super(**options)
29
+
30
+ @summary_text = summary_text
31
+ end
32
+
33
+ # Generates the HTML for the GOV.UK Details component
34
+ #
35
+ # @yield HTML that will be contained within the 'govuk-details__text' div
36
+ #
37
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Details
38
+ # which can then be rendered on the page
39
+
40
+ def render(&block)
41
+ tag.details(**options[:attributes]) do
42
+ concat(tag.summary(tag.span(summary_text, class: 'govuk-details__summary-text'), class: 'govuk-details__summary'))
43
+ concat(tag.div(class: 'govuk-details__text', &block))
44
+ end
45
+ end
46
+
47
+ # The default attributes for the details
48
+
49
+ DEFAULT_ATTRIBUTES = { class: 'govuk-details', data: { module: 'govuk-details' } }.freeze
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,60 @@
1
+ require_relative '../base'
2
+
3
+ module CCS
4
+ module Components
5
+ module GovUK
6
+ # = GOV.UK Error Message
7
+ #
8
+ # This is used to generate the error message component from the
9
+ # {https://design-system.service.gov.uk/components/error-message GDS - Components - Error message}
10
+ #
11
+ # @!attribute [r] message
12
+ # @return [String] Text for the error message
13
+ # @!attribute [r] attribute
14
+ # @return [String,Symbol] The attribute that has an error
15
+ # @!attribute [r] visually_hidden_text
16
+ # @return [String] The visually hidden text before the error message
17
+
18
+ class ErrorMessage < Base
19
+ private
20
+
21
+ attr_reader :message, :attribute, :visually_hidden_text
22
+
23
+ public
24
+
25
+ # @param message [String] the message to be displayed
26
+ # @param attribute [String, Symbol] the attribute that has an error
27
+ # @param options [Hash] options that will be used in customising the HTML
28
+ #
29
+ # @option options [String] :classes additional CSS classes for the error message HTML
30
+ # @option options [String] :visually_hidden_text ('Error') visualy hidden text before the error message
31
+ # @option options [Hash] :attributes ({}) any additional attributes that will be added as part of the HTML
32
+
33
+ def initialize(message:, attribute:, **options)
34
+ super(**options)
35
+
36
+ @options[:attributes][:id] ||= "#{attribute}-error"
37
+
38
+ @message = message
39
+ @attribute = attribute
40
+ @visually_hidden_text = @options[:visually_hidden_text] || 'Error'
41
+ end
42
+
43
+ # Generates the HTML for the GOV.UK Error message component
44
+ #
45
+ # @return [ActiveSupport::SafeBuffer]
46
+
47
+ def render
48
+ tag.p(**options[:attributes]) do
49
+ concat(tag.span("#{visually_hidden_text}: ", class: 'govuk-visually-hidden')) if visually_hidden_text.present?
50
+ concat(message)
51
+ end
52
+ end
53
+
54
+ # The default attributes for the error message
55
+
56
+ DEFAULT_ATTRIBUTES = { class: 'govuk-error-message' }.freeze
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../../base'
2
+
3
+ module CCS
4
+ module Components
5
+ module GovUK
6
+ class ErrorSummary < Base
7
+ # = GOV.UK Error Summary Item
8
+ #
9
+ # The individual list item for the error summary
10
+ #
11
+ # @!attribute [r] text
12
+ # @return [String] Text for the error message item
13
+ # @!attribute [r] href
14
+ # @return [String] The href for the error message
15
+
16
+ class Item < Base
17
+ private
18
+
19
+ attr_reader :text, :href
20
+
21
+ public
22
+
23
+ # @param text [String] the text for the error link item
24
+ # @param href [String] the href for the error link item.
25
+ # If provided item will be a link
26
+ # @param options [Hash] options that will be used in customising the HTML
27
+ #
28
+ # @option options [Hash] :attributes any additional attributes that will added as part of the HTML.
29
+
30
+ def initialize(text:, href: nil, **options)
31
+ super(**options)
32
+
33
+ @text = text
34
+ @href = href
35
+ end
36
+
37
+ # Generates the HTML for the GOV.UK Error Summary Item
38
+ #
39
+ # @return [ActiveSupport::SafeBuffer]
40
+
41
+ def render
42
+ tag.li do
43
+ if href
44
+ link_to(text, href, **options[:attributes])
45
+ else
46
+ text
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,70 @@
1
+ require_relative '../base'
2
+ require_relative 'error_summary/item'
3
+
4
+ module CCS
5
+ module Components
6
+ module GovUK
7
+ # = GOV.UK Error Summary
8
+ #
9
+ # This is used to generate the error summary component from the
10
+ # {https://design-system.service.gov.uk/components/error-summary GDS - Components - Error summary}
11
+ #
12
+ # @!attribute [r] title
13
+ # @return [String] Heading for the error summary
14
+ # @!attribute [r] error_summary_items
15
+ # @return [Array<Item>] An array of the initialised error summary items
16
+ # @!attribute [r] description
17
+ # @return [String] Text to use for the description of the errors
18
+
19
+ class ErrorSummary < Base
20
+ private
21
+
22
+ attr_reader :title, :error_summary_items, :description
23
+
24
+ public
25
+
26
+ # @param title [String] text to use for the heading of the error summary block
27
+ # @param error_summary_items [Array<Hash>] the list of errors to include in the summary. See {Components::GovUK::ErrorSummary::Item#initialize Item#initialize}
28
+ # @param description [String] optional text to use for the description of the errors
29
+ # @param options [Hash] options that will be used in customising the HTML
30
+ #
31
+ # @option options [String] :classes additional CSS classes for the error summary HTML
32
+ # @option options [Hash] :attributes ({}) any additional attributes that will be added as part of the HTML
33
+
34
+ def initialize(title:, error_summary_items:, description: nil, **options)
35
+ super(**options)
36
+
37
+ @title = title
38
+ @error_summary_items = error_summary_items.map { |error_summary_item| Item.new(context: @context, **error_summary_item) }
39
+ @description = description
40
+ end
41
+
42
+ # rubocop:disable Metrics/AbcSize
43
+
44
+ # Generates the HTML for the GOV.UK Error summary component
45
+ #
46
+ # @return [ActiveSupport::SafeBuffer]
47
+
48
+ def render
49
+ tag.div(**options[:attributes]) do
50
+ tag.div(role: 'alert') do
51
+ concat(tag.h2(title, class: 'govuk-error-summary__title'))
52
+ concat(tag.div(class: 'govuk-error-summary__body') do
53
+ concat(tag.p(description)) if description
54
+ concat(tag.ul(class: 'govuk-list govuk-error-summary__list') do
55
+ error_summary_items.each { |error_summary_item| concat(error_summary_item.render) }
56
+ end)
57
+ end)
58
+ end
59
+ end
60
+ end
61
+
62
+ # rubocop:enable Metrics/AbcSize
63
+
64
+ # The default attributes for the error summary
65
+
66
+ DEFAULT_ATTRIBUTES = { class: 'govuk-error-summary', data: { module: 'govuk-error-summary' } }.freeze
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,129 @@
1
+ require_relative 'textarea'
2
+ require_relative '../../hint'
3
+
4
+ module CCS
5
+ module Components
6
+ module GovUK
7
+ class Field < Base
8
+ class Input < Field
9
+ # = GOV.UK Character count
10
+ #
11
+ # This is used for generating the character count component from the
12
+ # {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
13
+ #
14
+ # This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
15
+ #
16
+ # @!attribute [r] textarea
17
+ # @return [Textarea] The initialised textarea
18
+ # @!attribute [r] fallback_hint
19
+ # @return [Hint] The initialised character count fallback hint
20
+ # @!attribute [r] character_count_html_options
21
+ # @return [Hash] HTML options for the character count
22
+
23
+ class CharacterCount
24
+ include ActionView::Context
25
+ include ActionView::Helpers
26
+
27
+ private
28
+
29
+ attr_reader :textarea, :fallback_hint, :character_count_html_options
30
+
31
+ public
32
+
33
+ # @param (see CCS::Components::GovUK::Field::Input::Textarea#initialize)
34
+ # @param character_count_options [Hash] options for the character count
35
+ # @param context [ActionView::Base] the view context
36
+ #
37
+ # @option (see CCS::Components::GovUK::Field::Input::Textarea#initialize)
38
+ #
39
+ # @option (see initialise_character_count_html_options)
40
+
41
+ def initialize(attribute:, character_count_options:, context:, **options)
42
+ character_count_attribute = options[:form] ? "#{options[:form].object_name}_#{attribute}" : attribute
43
+
44
+ initialise_textarea(attribute, character_count_attribute, context, options)
45
+ initialise_character_count_html_options(character_count_options)
46
+ initialise_fallback_hint(character_count_attribute, context, character_count_options)
47
+ end
48
+
49
+ # Generates the HTML for the GOV.UK Character count component
50
+ #
51
+ # @return [ActiveSupport::SafeBuffer]
52
+
53
+ def render
54
+ tag.div(**character_count_html_options) do
55
+ concat(textarea.render)
56
+ concat(fallback_hint.render)
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ # Initialises the Textarea for the character count
63
+ #
64
+ # @param attribute [Symbol] the attribute of the field
65
+ # @param character_count_attribute [String] the name of the field as it will appear in the textarea
66
+ # @param context [ActionView::Base] the view context
67
+ # @param options [Hash] options that will be used for the textarea
68
+ #
69
+ # @option (see CCS::Components::GovUK::Field::Input::Textarea#initialize)
70
+
71
+ def initialise_textarea(attribute, character_count_attribute, context, options)
72
+ ((options[:attributes] ||= {})[:aria] ||= {})[:describedby] = [options.dig(:attributes, :aria, :describedby), "#{character_count_attribute}-info"].compact.join(' ')
73
+ options[:classes] = "#{options[:classes]} govuk-js-character-count".lstrip
74
+
75
+ @textarea = Textarea.new(attribute: attribute, context: context, **options)
76
+ end
77
+
78
+ # Initialises the charcter count options
79
+ #
80
+ # @param character_count_options [Hash] options for the charcter count
81
+ #
82
+ # @option character_count_options [String] :maxlength the maximum number of characters.
83
+ # If +maxwords+ is set, this is not required.
84
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
85
+ # @option character_count_options [String] :maxwords the maximum number of words.
86
+ # If maxlength is set, this is not required.
87
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
88
+ # @option character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
89
+ # If this attribute is set, the count message will be hidden by default.
90
+ # @option character_count_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text:
91
+ # - +:count_message+ replaced the default text for the count message.
92
+ # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
93
+ # - +classes+ additional CSS classes for the fallback hint HTML
94
+
95
+ def initialise_character_count_html_options(character_count_options)
96
+ character_count_html_options = { class: 'govuk-character-count', data: { module: 'govuk-character-count' } }
97
+
98
+ %i[maxlength threshold maxwords].each do |data_attribute|
99
+ character_count_html_options[:data][data_attribute] = character_count_options[data_attribute].to_s if character_count_options[data_attribute]
100
+ end
101
+
102
+ @character_count_html_options = character_count_html_options
103
+ end
104
+
105
+ # Initialises the charcter count fall back hint
106
+ #
107
+ # @param character_count_attribute [String] the name of the field as it will appear in the textarea
108
+ # @param context [ActionView::Base] the view context
109
+ # @param (see initialise_character_count_html_options)
110
+ #
111
+ # @option (see initialise_character_count_html_options)
112
+
113
+ def initialise_fallback_hint(character_count_attribute, context, character_count_options)
114
+ fallback_hint = character_count_options[:fallback_hint] || {}
115
+
116
+ fallback_hint_length = character_count_options[:maxwords] || character_count_options[:maxlength]
117
+ fallback_hint_default = "You can enter up to %<count>s #{character_count_options[:maxwords] ? 'words' : 'characters'}"
118
+
119
+ text = format(fallback_hint[:count_message] || fallback_hint_default, count: fallback_hint_length)
120
+ classes = "#{fallback_hint[:classes]} govuk-character-count__message".lstrip
121
+
122
+ @fallback_hint = Hint.new(text: text, classes: classes, attributes: { id: "#{character_count_attribute}-info" }, context: context)
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,44 @@
1
+ require_relative '../input'
2
+
3
+ module CCS
4
+ module Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Input < Field
8
+ # = GOV.UK File Upload
9
+ #
10
+ # This is used for generating the file upload component from the
11
+ # {https://design-system.service.gov.uk/components/file-upload GDS - Components - File Upload}
12
+
13
+ class FileUpload < Input
14
+ # @param (see CCS::Components::GovUK::Field::Input#initialize)
15
+ #
16
+ # @option (see CCS::Components::GovUK::Field::Input#initialize)
17
+
18
+ def initialize(attribute:, **options)
19
+ super(attribute: attribute, **options)
20
+ end
21
+
22
+ # Generates the HTML for the GOV.UK File Upload component
23
+ #
24
+ # @return [ActiveSupport::SafeBuffer]
25
+
26
+ def render
27
+ super() do
28
+ if options[:form]
29
+ options[:form].file_field(attribute, **options[:attributes])
30
+ else
31
+ context.file_field_tag(attribute, **options[:attributes])
32
+ end
33
+ end
34
+ end
35
+
36
+ # The default attributes for the file upload
37
+
38
+ DEFAULT_ATTRIBUTES = { class: 'govuk-file-upload' }.freeze
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,82 @@
1
+ require_relative '../input'
2
+
3
+ module CCS
4
+ module Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Input < Field
8
+ # = GOV.UK Select
9
+ #
10
+ # This is used for generating the select component from the
11
+ # {https://design-system.service.gov.uk/components/select GDS - Components - Select}
12
+ #
13
+ # @!attribute [r] items
14
+ # @return [Array<Hash>] The select items
15
+ # @!attribute [r] selected
16
+ # @return [String] The selected option
17
+
18
+ class Select < Input
19
+ private
20
+
21
+ attr_reader :items, :selected
22
+
23
+ public
24
+
25
+ # @param (see CCS::Components::GovUK::Field::Input#initialize)
26
+ # @param items [Array<Hash>] array of options for select.
27
+ # The options are:
28
+ # - +:text+ the text of the option item. If this is blank the value is used
29
+ # - +:value+ the value of the option item
30
+ # - +:attributes+ any additional attributes that will added as part of the option HTML
31
+ # @param selected [String] the selected option
32
+ #
33
+ # @option (see CCS::Components::GovUK::Field::Input#initialize)
34
+
35
+ def initialize(attribute:, items:, selected: nil, **options)
36
+ super(attribute: attribute, **options)
37
+
38
+ @items = items.map do |item|
39
+ [
40
+ item[:text] || item[:value],
41
+ item[:value],
42
+ (item[:attributes] || {})
43
+ ]
44
+ end
45
+ @selected = @options[:model] ? @options[:model].send(attribute) : selected
46
+ end
47
+
48
+ # Generates the HTML for the GOV.UK Select component
49
+ #
50
+ # @return [ActiveSupport::SafeBuffer]
51
+
52
+ def render
53
+ super() do
54
+ if options[:form]
55
+ options[:form].select(
56
+ attribute,
57
+ items,
58
+ {},
59
+ **options[:attributes]
60
+ )
61
+ else
62
+ context.select_tag(
63
+ attribute,
64
+ context.options_for_select(
65
+ items,
66
+ selected
67
+ ),
68
+ **options[:attributes]
69
+ )
70
+ end
71
+ end
72
+ end
73
+
74
+ # The default attributes for the select
75
+
76
+ DEFAULT_ATTRIBUTES = { class: 'govuk-select' }.freeze
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,55 @@
1
+ require_relative '../text_input'
2
+
3
+ module CCS
4
+ module Components
5
+ module GovUK
6
+ class Field < Base
7
+ class Input < Field
8
+ class TextInput < Input
9
+ # = GOV.UK Text Input prefix/suffix
10
+ #
11
+ # This is used to generate the prefix/suffix HTML for a text input
12
+ #
13
+ # @!attribute [r] text
14
+ # @return [String] Text for the prefix/suffix
15
+
16
+ class Fix < Base
17
+ private
18
+
19
+ attr_reader :text
20
+
21
+ public
22
+
23
+ # @param text [String] the text for the prefix/suffix
24
+ # @param fix [String] either +"pre"+ or +"suf"+
25
+ #
26
+ # @param options [Hash] options that will be used in customising the HTML
27
+ #
28
+ # @option options [String] :classes additional CSS classes for the prefix/suffix HTML
29
+ # @option options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
30
+
31
+ def initialize(text:, fix:, **options)
32
+ super(**options)
33
+
34
+ @options[:attributes][:class] = "govuk-input__#{fix}fix #{@options[:attributes][:class]}".rstrip
35
+ (@options[:attributes][:aria] ||= {})[:hidden] = true
36
+
37
+ @text = text
38
+ end
39
+
40
+ # Generates the HTML for the GOV.UK Text Input prefix/suffix
41
+ #
42
+ # @yield (see CCS::Components::GovUK::Field#render)
43
+ #
44
+ # @return [ActiveSupport::SafeBuffer]
45
+
46
+ def render
47
+ tag.div(text, **options[:attributes])
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end