diff --git a/app/assets/stylesheets/components/table.scss b/app/assets/stylesheets/components/table.scss new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/components/table_component.rb b/app/components/table_component.rb new file mode 100644 index 0000000000..823e23fa71 --- /dev/null +++ b/app/components/table_component.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class TableComponent < ViewComponent::Base + + renders_one :header, TableRowComponent + renders_many :rows, TableRowComponent + + def initialize(id: '', stripped: false) + super + @id = id + @stripped = stripped + end + + def stripped_class + @stripped ? 'table-content-stripped' : '' + end +end diff --git a/app/components/table_component/table_component.html.haml b/app/components/table_component/table_component.html.haml new file mode 100644 index 0000000000..354e87fe99 --- /dev/null +++ b/app/components/table_component/table_component.html.haml @@ -0,0 +1,6 @@ +%table.table-content{id: @id, class: stripped_class} + %thead + = header + %tbody + - rows.each do |row| + = row diff --git a/app/components/table_row_component.rb b/app/components/table_row_component.rb new file mode 100644 index 0000000000..4e92d3a445 --- /dev/null +++ b/app/components/table_row_component.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class TableRowComponent < ViewComponent::Base + + renders_many :ths + renders_many :tds +end diff --git a/app/components/table_row_component/table_row_component.html.haml b/app/components/table_row_component/table_row_component.html.haml new file mode 100644 index 0000000000..79f66ac2e9 --- /dev/null +++ b/app/components/table_row_component/table_row_component.html.haml @@ -0,0 +1,6 @@ +%tr + - ths.each do |th| + %th= th + + - tds.each do |td| + %td= td \ No newline at end of file diff --git a/test/components/previews/table_component_preview.rb b/test/components/previews/table_component_preview.rb new file mode 100644 index 0000000000..4ac538b637 --- /dev/null +++ b/test/components/previews/table_component_preview.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +class TableComponentPreview < ViewComponent::Preview + + include ActionView::Helpers::UrlHelper + + def default + render TableComponent.new do |t| + table_content(t) + end + end + + def stripped + render TableComponent.new(stripped: true) do |t| + table_content(t) + end + end + + private + + def table_content(t) + headers = 5.times.map { |i| "header #{i}" } + rows = 6.times.map { |row| 5.times.map { |i| "line #{row} :#{i} " } } + + t.header do |h| + headers.each do |header| + h.th { header } + end + h.th { 'Action' } + end + + rows.each do |row| + t.row do |r| + row.each do |col| + r.td { col } + end + + r.td do + link_to('Edit', '', class: 'mr-3') + link_to('Delete', '') + end + end + end + + end +end