Skip to content

Commit

Permalink
Merge pull request #388 from DFE-Digital/infer-header-for-cells-in-thead
Browse files Browse the repository at this point in the history
Infer header status for cells based on their location
  • Loading branch information
peteryates authored Jan 8, 2023
2 parents dba59da + 8d1dbfb commit 9b026bc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
10 changes: 7 additions & 3 deletions app/components/govuk_component/table_component/cell_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class GovukComponent::TableComponent::CellComponent < GovukComponent::Base
"one-quarter" => "govuk-!-width-one-quarter",
}.freeze

def initialize(scope: nil, header: false, numeric: false, text: nil, width: nil, parent: nil, classes: [], html_attributes: {})
@header = header
def initialize(scope: nil, header: nil, numeric: false, text: nil, width: nil, parent: nil, classes: [], html_attributes: {})
@text = text
@numeric = numeric
@width = width
@scope = scope
@parent = parent
@header = (header.nil?) ? in_thead? : header

super(classes: classes, html_attributes: html_attributes)
end
Expand Down Expand Up @@ -59,7 +59,7 @@ def determine_scope
in { auto_table_scopes: true, parent: 'tbody' }
'row'
else
Rails.logger.warning("No scope pattern matched")
Rails.logger.warn("No scope pattern matched")

nil
end
Expand All @@ -76,4 +76,8 @@ def default_classes
def width_class
WIDTHS.fetch(width, nil)
end

def in_thead?
parent == "thead"
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class GovukComponent::TableComponent::HeadComponent < GovukComponent::Base
renders_many :rows, ->(cell_data: nil, header: true, classes: [], html_attributes: {}, &block) do
renders_many :rows, ->(cell_data: nil, classes: [], html_attributes: {}, &block) do
GovukComponent::TableComponent::RowComponent.from_head(
cell_data: cell_data,
header: header,
classes: classes,
html_attributes: html_attributes,
&block
Expand All @@ -26,7 +25,7 @@ def call
def build_rows_from_row_data(data)
return if data.blank?

data.each { |d| row(cell_data: d, header: true) }
data.each { |d| row(cell_data: d) }
end

def default_attributes
Expand Down
13 changes: 8 additions & 5 deletions app/components/govuk_component/table_component/row_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class GovukComponent::TableComponent::RowComponent < GovukComponent::Base
renders_many :cells, ->(scope: nil, header: false, text: nil, numeric: false, width: nil, classes: [], html_attributes: {}, &block) do
renders_many :cells, ->(scope: nil, header: nil, text: nil, numeric: false, width: nil, classes: [], html_attributes: {}, &block) do
GovukComponent::TableComponent::CellComponent.new(
scope: scope,
header: header,
Expand All @@ -13,10 +13,9 @@ class GovukComponent::TableComponent::RowComponent < GovukComponent::Base
)
end

attr_reader :header, :first_cell_is_header, :parent
attr_reader :first_cell_is_header, :parent

def initialize(cell_data: nil, first_cell_is_header: false, header: false, parent: nil, classes: [], html_attributes: {})
@header = header
def initialize(cell_data: nil, first_cell_is_header: false, parent: nil, classes: [], html_attributes: {})
@first_cell_is_header = first_cell_is_header
@parent = parent

Expand Down Expand Up @@ -52,10 +51,14 @@ def cell_attributes(count)
end

def cell_is_header?(count)
header || (first_cell_is_header && count.zero?)
in_thead? || (first_cell_is_header && count.zero?)
end

def default_attributes
{ class: %w(govuk-table__row) }
end

def in_thead?
parent == "thead"
end
end
14 changes: 7 additions & 7 deletions guide/lib/examples/table_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def table_normal
- table.head do |head|
- head.row do |row|
- row.cell(header: true, text: 'Name')
- row.cell(header: true, text: 'Types')
- row.cell(header: true, text: 'Pokédex number', numeric: true)
- row.cell(text: 'Name')
- row.cell(text: 'Types')
- row.cell(text: 'Pokédex number', numeric: true)
- table.body do |body|
- body.row do |row|
Expand All @@ -36,8 +36,8 @@ def table_with_header_column
- table.head do |head|
- head.row do |row|
- row.cell(header: true, text: 'Generation')
- row.cell(header: true, text: 'Years')
- row.cell(text: 'Generation')
- row.cell(text: 'Years')
- table.body do |body|
- body.row do |row|
Expand Down Expand Up @@ -85,8 +85,8 @@ def table_with_resized_columns
- table.head do |head|
- head.row do |row|
- row.cell(header: true, text: 'Name', width: 'one-third')
- row.cell(header: true, text: 'Description')
- row.cell(text: 'Name', width: 'one-third')
- row.cell(text: 'Description')
- table.body do |body|
- body.row do |row|
Expand Down
18 changes: 16 additions & 2 deletions spec/components/govuk_component/table_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
table.caption(text: "What a nice table")

table.head do |head|
head.row {}
head.row do |row|
row.cell(text: "A")
row.cell(text: "B")
end
end

table.body do |body|
body.row {}
body.row do |row|
row.cell(text: "C")
row.cell(text: "D")
end
end
end
end
Expand All @@ -36,10 +42,18 @@
expect(rendered_content).to have_tag("table > thead")
end

specify "the cells in thead should default to th" do
expect(rendered_content).to have_tag("table > thead > tr > th", count: 2)
end

specify "renders a tbody element" do
expect(rendered_content).to have_tag("table > tbody")
end

specify "the cells in tbody should default to td" do
expect(rendered_content).to have_tag("table > tbody > tr > td", count: 2)
end

context "when there is more than one tbody" do
let(:expected_count) { 2 }

Expand Down

0 comments on commit 9b026bc

Please sign in to comment.