govuk-components 2.0.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39ac79b41b17e5f17f5f6b80e195595ae1396f5d83a3a14c14564412831fdbc8
4
- data.tar.gz: 587e0b9a9688c4c239e1e8cbdf44111c88aa82cd1014197293b308d0b2409f36
3
+ metadata.gz: a251ce9b412d46a7fd70155a08ba3cc2f6e9830baca832622bdeb459936b791f
4
+ data.tar.gz: 85cccf721573ce9c734d7156de9da9c1ceefc22cac05888d8163d9441420be7b
5
5
  SHA512:
6
- metadata.gz: 826c5c80a39daefff656c96139c8f3c0ad6ceaffa7c8000a482f5b35a085b0f3251c7ded964c5bff0bc7a977287f77ed4ca2f5e4684d73e279d589de946e97a3
7
- data.tar.gz: cb1e686cf23ef788c4d45544bd1c4cc97cdb91a560a9ca7653d71b299dedfc735721b67b7d22062823f21283f56889e43e6a3b8e35eac29d8395cf9b1dfc169c
6
+ metadata.gz: c94357e58eee8a5ee0ae6e059f3177e340340d7af7cd19cfc3de607cc3b04e52d62e689a9291cc94ae568a1e1de809e224c566cb82a9b148986346773b45064b
7
+ data.tar.gz: 89494b6d782f7aaf2e4453fc317c917bb87e6dafabd21cd8b6d0d363ebba546ae2ffbdef43f129d8eaff875f0a3cd1b804a99f271f5a33779fca3ec8fddf2d0b
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/cbcbc140f300b920d833/maintainability)](https://codeclimate.com/github/DFE-Digital/govuk-components/maintainability)
5
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/cbcbc140f300b920d833/test_coverage)](https://codeclimate.com/github/DFE-Digital/govuk-components/test_coverage)
6
6
  [![GitHub license](https://img.shields.io/github/license/DFE-Digital/govuk-components)](https://github.com/DFE-Digital/govuk-components/blob/master/LICENSE)
7
- [![GOV.UK Design System Version](https://img.shields.io/badge/GOV.UK%20Design%20System-3.13.0-brightgreen)](https://design-system.service.gov.uk)
7
+ [![GOV.UK Design System Version](https://img.shields.io/badge/GOV.UK%20Design%20System-3.13.1-brightgreen)](https://design-system.service.gov.uk)
8
8
 
9
9
  This gem provides a suite of reusable components for the [GOV.UK Design System](https://design-system.service.gov.uk/). It is intended to provide a lightweight alternative to the [GOV.UK Publishing Components](https://github.com/alphagov/govuk_publishing_components) library and is built with Github's [ViewComponent](https://github.com/github/view_component) framework.
10
10
 
@@ -32,6 +32,7 @@ The provided components are:
32
32
  * [Start button](https://dfe-digital.github.io/govuk-components/#start-button)
33
33
  * [Summary list](https://dfe-digital.github.io/govuk-components/#summary-list)
34
34
  * [Tabs](https://dfe-digital.github.io/govuk-components/#tabs)
35
+ * [Tables](https://dfe-digital.github.io/govuk-components/#table)
35
36
  * [Tags](https://dfe-digital.github.io/govuk-components/#tags)
36
37
  * [Warning text](https://dfe-digital.github.io/govuk-components/#warning-text)
37
38
 
@@ -44,7 +45,7 @@ All of the components can be rendered in two ways:
44
45
  * directly using Rails' `#render` method:
45
46
 
46
47
  ```erb
47
- <%= render GovukComponent::GovukComponent::WarningTextComponent.new do %>
48
+ <%= render GovukComponent::WarningTextComponent.new do %>
48
49
  A serious warning
49
50
  <% end %>
50
51
  ```
@@ -33,7 +33,7 @@ private
33
33
  if link.present?
34
34
  list_item { link_to(text, link, class: "govuk-breadcrumbs__link") }
35
35
  else
36
- list_item(aria: { current: "page" }) { text }
36
+ list_item(aria: { current: "page" }) { text.to_s }
37
37
  end
38
38
  end
39
39
 
@@ -33,11 +33,7 @@
33
33
  <%= tag.ul(class: navigation_classes, id: "navigation", aria: { label: navigation_label }) do %>
34
34
  <% navigation_items.each do |item| %>
35
35
  <%= tag.li(class: item.classes.append(item.active_class), **item.html_attributes) do %>
36
- <% if item.link? %>
37
- <%= link_to(item.text, item.href, class: "govuk-header__link") %>
38
- <% else %>
39
- <%= item.text %>
40
- <% end %>
36
+ <%= item %>
41
37
  <% end %>
42
38
  <% end %>
43
39
  <% end %>
@@ -53,17 +53,20 @@ private
53
53
  end
54
54
 
55
55
  class NavigationItem < GovukComponent::Base
56
- attr_reader :text, :href, :active
56
+ attr_reader :text, :href, :options, :active
57
57
 
58
- def initialize(text:, href: nil, active: false, classes: [], html_attributes: {})
58
+ def initialize(text:, href: nil, options: {}, active: nil, classes: [], html_attributes: {})
59
59
  super(classes: classes, html_attributes: html_attributes)
60
60
 
61
- @text = text
62
- @href = href
63
- @active = active
61
+ @text = text
62
+ @href = href
63
+ @options = options
64
+ @active = active
64
65
  end
65
66
 
66
67
  def active?
68
+ return current_page?(href) if active.nil?
69
+
67
70
  active
68
71
  end
69
72
 
@@ -75,6 +78,14 @@ private
75
78
  href.present?
76
79
  end
77
80
 
81
+ def call
82
+ if link?
83
+ link_to(text, href, **options, class: "govuk-header__link")
84
+ else
85
+ text
86
+ end
87
+ end
88
+
78
89
  private
79
90
 
80
91
  def default_classes
@@ -0,0 +1,5 @@
1
+ <%= tag.tbody(class: classes, **html_attributes) do %>
2
+ <% rows.each do |row| %>
3
+ <%= row %>
4
+ <% end %>
5
+ <% end %>
@@ -0,0 +1,21 @@
1
+ class GovukComponent::TableComponent::BodyComponent < GovukComponent::Base
2
+ renders_many :rows, GovukComponent::TableComponent::RowComponent
3
+
4
+ def initialize(rows: nil, first_cell_is_header: false, classes: [], html_attributes: {})
5
+ super(classes: classes, html_attributes: html_attributes)
6
+
7
+ build_rows_from_row_data(rows, first_cell_is_header)
8
+ end
9
+
10
+ private
11
+
12
+ def build_rows_from_row_data(data, first_cell_is_header)
13
+ return if data.blank?
14
+
15
+ data.each { |d| row(cell_data: d, first_cell_is_header: first_cell_is_header) }
16
+ end
17
+
18
+ def default_classes
19
+ %w(govuk-table__body)
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ class GovukComponent::TableComponent::CaptionComponent < GovukComponent::Base
2
+ attr_reader :text, :size
3
+
4
+ SIZES = %w(s m l xl).freeze
5
+
6
+ def initialize(text: nil, id: nil, size: 'm', classes: [], html_attributes: {})
7
+ super(classes: classes, html_attributes: html_attributes)
8
+
9
+ @id = id
10
+ @text = text
11
+ @size = size
12
+ end
13
+
14
+ def call
15
+ tag.caption(caption_content, class: classes, **html_attributes)
16
+ end
17
+
18
+ def render?
19
+ caption_content.present?
20
+ end
21
+
22
+ private
23
+
24
+ def caption_content
25
+ @caption_content ||= (content || text)
26
+ end
27
+
28
+ def default_classes
29
+ %w(govuk-table__caption).tap do |c|
30
+ c << caption_size_class if @size
31
+ end
32
+ end
33
+
34
+ def caption_size_class
35
+ fail(ArgumentError, "bad size #{size}, must be in #{SIZES}") unless size.in?(SIZES)
36
+
37
+ %(govuk-table__caption--#{size})
38
+ end
39
+ end
@@ -0,0 +1 @@
1
+ <%= content_tag(cell_element, cell_content, class: classes, **html_attributes) %>
@@ -0,0 +1,55 @@
1
+ class GovukComponent::TableComponent::CellComponent < GovukComponent::Base
2
+ attr_reader :text, :header, :numeric, :width
3
+
4
+ alias_method :numeric?, :numeric
5
+
6
+ WIDTHS = {
7
+ "full" => "govuk-input govuk-!-width-full",
8
+ "three-quarters" => "govuk-input govuk-!-width-three-quarters",
9
+ "two-thirds" => "govuk-input govuk-!-width-two-thirds",
10
+ "one-half" => "govuk-input govuk-!-width-one-half",
11
+ "one-third" => "govuk-input govuk-!-width-one-third",
12
+ "one-quarter" => "govuk-input govuk-!-width-one-quarter",
13
+ }.freeze
14
+
15
+ def initialize(header: false, text: nil, numeric: false, width: nil, classes: [], html_attributes: {})
16
+ super(classes: classes, html_attributes: html_attributes)
17
+
18
+ @header = header
19
+ @text = text
20
+ @numeric = numeric
21
+ @width = width
22
+ end
23
+
24
+ private
25
+
26
+ def width?
27
+ width.present?
28
+ end
29
+
30
+ def cell_content
31
+ content || text
32
+ end
33
+
34
+ def cell_element
35
+ header ? :th : :td
36
+ end
37
+
38
+ def default_classes
39
+ if header
40
+ %w(govuk-table__header).tap do |c|
41
+ c << "govuk-table__header--numeric" if numeric?
42
+ c << width_class if width?
43
+ end
44
+ else
45
+ %w(govuk-table__cell).tap do |c|
46
+ c << "govuk-table__cell--numeric" if numeric?
47
+ c << width_class if width?
48
+ end
49
+ end
50
+ end
51
+
52
+ def width_class
53
+ WIDTHS.fetch(width)
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ <%= tag.thead(class: classes, **html_attributes) do %>
2
+ <% rows.each do |row| %>
3
+ <%= row %>
4
+ <% end %>
5
+ <% end %>
@@ -0,0 +1,23 @@
1
+ class GovukComponent::TableComponent::HeadComponent < GovukComponent::Base
2
+ renders_many :rows, GovukComponent::TableComponent::RowComponent
3
+
4
+ attr_reader :row_data
5
+
6
+ def initialize(rows: nil, classes: [], html_attributes: {})
7
+ super(classes: classes, html_attributes: html_attributes)
8
+
9
+ build_rows_from_row_data(rows)
10
+ end
11
+
12
+ private
13
+
14
+ def build_rows_from_row_data(data)
15
+ return if data.blank?
16
+
17
+ data.each { |d| row(cell_data: d, header: true) }
18
+ end
19
+
20
+ def default_classes
21
+ %w(govuk-table__head)
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ <%= tag.tr(class: classes, **html_attributes) do %>
2
+ <% cells.each do |cell| %>
3
+ <%= cell %>
4
+ <% end %>
5
+ <% end %>
@@ -0,0 +1,30 @@
1
+ class GovukComponent::TableComponent::RowComponent < GovukComponent::Base
2
+ renders_many :cells, GovukComponent::TableComponent::CellComponent
3
+
4
+ attr_reader :header, :first_cell_is_header
5
+
6
+ def initialize(cell_data: nil, first_cell_is_header: false, header: false, classes: [], html_attributes: {})
7
+ super(classes: classes, html_attributes: html_attributes)
8
+
9
+ @header = header
10
+ @first_cell_is_header = first_cell_is_header
11
+
12
+ build_cells_from_cell_data(cell_data)
13
+ end
14
+
15
+ private
16
+
17
+ def build_cells_from_cell_data(cell_data)
18
+ return if cell_data.blank?
19
+
20
+ cell_data.map.with_index { |cd, i| cell(header: cell_is_header?(i), text: cd) }
21
+ end
22
+
23
+ def cell_is_header?(count)
24
+ header || (first_cell_is_header && count.zero?)
25
+ end
26
+
27
+ def default_classes
28
+ %w(govuk-table__row)
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ <%= tag.table(id: id, class: classes, **html_attributes) do %>
2
+ <%= caption %>
3
+ <%= head %>
4
+ <% bodies.each do |body| %>
5
+ <%= body %>
6
+ <% end %>
7
+ <% end %>
@@ -0,0 +1,33 @@
1
+ class GovukComponent::TableComponent < GovukComponent::Base
2
+ renders_one :caption, GovukComponent::TableComponent::CaptionComponent
3
+ renders_one :head, GovukComponent::TableComponent::HeadComponent
4
+ renders_many :bodies, GovukComponent::TableComponent::BodyComponent
5
+
6
+ attr_accessor :id, :first_cell_is_header, :caption_text
7
+
8
+ def initialize(id: nil, rows: nil, head: nil, caption: nil, first_cell_is_header: false, classes: [], html_attributes: {})
9
+ super(classes: classes, html_attributes: html_attributes)
10
+
11
+ @id = id
12
+ @first_cell_is_header = first_cell_is_header
13
+ @caption_text = caption
14
+
15
+ # when no rows are passed in it's likely we're taking the slot approach
16
+ return unless rows.presence
17
+
18
+ # if no head is passed in,use the first row for headers
19
+ build(*(head ? [head, rows] : [rows[0], rows[1..]]), caption_text)
20
+ end
21
+
22
+ private
23
+
24
+ def build(head_data, body_data, caption_text)
25
+ caption(text: caption_text)
26
+ head(rows: [head_data])
27
+ body(rows: body_data, first_cell_is_header: first_cell_is_header)
28
+ end
29
+
30
+ def default_classes
31
+ %w(govuk-table)
32
+ end
33
+ end
@@ -13,6 +13,7 @@ module GovukComponentsHelper
13
13
  govuk_phase_banner: 'GovukComponent::PhaseBannerComponent',
14
14
  govuk_start_button: 'GovukComponent::StartButtonComponent',
15
15
  govuk_summary_list: 'GovukComponent::SummaryListComponent',
16
+ govuk_table: 'GovukComponent::TableComponent',
16
17
  govuk_tabs: 'GovukComponent::TabComponent',
17
18
  govuk_tag: 'GovukComponent::TagComponent',
18
19
  govuk_warning_text: 'GovukComponent::WarningTextComponent',
@@ -1,5 +1,5 @@
1
1
  module Govuk
2
2
  module Components
3
- VERSION = '2.0.0'.freeze
3
+ VERSION = '2.1.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk-components
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - DfE developers
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-18 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.36.0
47
+ version: 2.39.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.36.0
54
+ version: 2.39.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry-byebug
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -191,6 +191,17 @@ files:
191
191
  - app/components/govuk_component/summary_list_component/value_component.rb
192
192
  - app/components/govuk_component/tab_component.html.erb
193
193
  - app/components/govuk_component/tab_component.rb
194
+ - app/components/govuk_component/table_component.html.erb
195
+ - app/components/govuk_component/table_component.rb
196
+ - app/components/govuk_component/table_component/body_component.html.erb
197
+ - app/components/govuk_component/table_component/body_component.rb
198
+ - app/components/govuk_component/table_component/caption_component.rb
199
+ - app/components/govuk_component/table_component/cell_component.html.erb
200
+ - app/components/govuk_component/table_component/cell_component.rb
201
+ - app/components/govuk_component/table_component/head_component.html.erb
202
+ - app/components/govuk_component/table_component/head_component.rb
203
+ - app/components/govuk_component/table_component/row_component.html.erb
204
+ - app/components/govuk_component/table_component/row_component.rb
194
205
  - app/components/govuk_component/tag_component.rb
195
206
  - app/components/govuk_component/traits/custom_classes.rb
196
207
  - app/components/govuk_component/traits/custom_html_attributes.rb
@@ -209,7 +220,7 @@ homepage: https://github.com/DFE-Digital/govuk-components
209
220
  licenses:
210
221
  - MIT
211
222
  metadata: {}
212
- post_install_message:
223
+ post_install_message:
213
224
  rdoc_options: []
214
225
  require_paths:
215
226
  - lib
@@ -225,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
236
  version: '0'
226
237
  requirements: []
227
238
  rubygems_version: 3.1.6
228
- signing_key:
239
+ signing_key:
229
240
  specification_version: 4
230
241
  summary: Lightweight set of reusable GOV.UK Design System components
231
242
  test_files: []