Last active
January 25, 2024 06:57
-
-
Save joeldrapper/cc82d815357d1b1a5b1d7f53ac8cce82 to your computer and use it in GitHub Desktop.
Table component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render TableComponent.new(@posts) do |t| | |
t.column("Title", &:title) | |
t.column("Author") { |post| post.author.name } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TableComponent < Phlex::HTML | |
Column = Data.define(:name, :content) | |
include Phlex::DeferredRender | |
def initialize(collection) | |
@collection = collection | |
@columns = [] | |
end | |
def template | |
table do | |
thead do | |
tr do | |
@columns.each do |column| | |
th { column.name } | |
end | |
end | |
end | |
tbody do | |
@collection.each do |item| | |
tr do | |
@columns.each do |column| | |
td do | |
column.content.call(item) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
def column(name, &content) | |
@columns << Column.new(name:, content:) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment