Class: CCS::Components::GovUK::Table
- Defined in:
- lib/ccs/components/govuk/table.rb,
lib/ccs/components/govuk/table/body/data_cell.rb,
lib/ccs/components/govuk/table/body/head_cell.rb,
lib/ccs/components/govuk/table/header/head_cell.rb
Overview
GOV.UK Table
This is used to generate the table component from the GDS - Components - Table
Defined Under Namespace
Constant Summary collapse
- DEFAULT_ATTRIBUTES =
The default attributes for the table
{ class: 'govuk-table' }.freeze
Instance Method Summary collapse
-
#initialize(rows:, head_cells: nil, caption: nil, first_cell_is_header: nil, **options) ⇒ Table
constructor
A new instance of Table.
-
#render ⇒ ActiveSupport::SafeBuffer
Generates the HTML for the GOV.UK table component.
Constructor Details
#initialize(rows:, head_cells: nil, caption: nil, first_cell_is_header: nil, **options) ⇒ Table
Returns a new instance of Table.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ccs/components/govuk/table.rb', line 43 def initialize(rows:, head_cells: nil, caption: nil, first_cell_is_header: nil, **) super(**) @rows = rows.map do |row_cells| row_cells.map.with_index do |row_cell, index| if first_cell_is_header && index.zero? Body::HeadCell.new(context: @context, **row_cell) else Body::DataCell.new(context: @context, **row_cell) end end end @head_cells = head_cells&.map { |head_cell| Header::HeadCell.new(context: @context, **head_cell) } @caption = end |
Instance Method Details
#render ⇒ ActiveSupport::SafeBuffer
Generates the HTML for the GOV.UK table component
63 64 65 66 67 68 69 70 71 |
# File 'lib/ccs/components/govuk/table.rb', line 63 def render tag.table(**[:attributes]) do concat() if concat(table_head) if head_cells concat(tag.tbody(class: 'govuk-table__body') do rows.each { |row_cells| concat(table_row(row_cells)) } end) end end |