Last active
February 3, 2022 16:49
-
-
Save boardfish/5eafa75c28fdc2e248b8a0c6e9133964 to your computer and use it in GitHub Desktop.
Example of a basic table component
This file contains hidden or 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
<p style="color: green"><%= notice %></p> | |
<h1>Products</h1> | |
<div id="products"> | |
<%= render TableComponent.new do |c| %> | |
<% @products.each do |product| %> | |
<% c.row(product, RowComponent) %> | |
<% end %> | |
<% end %> | |
</div> | |
<%= link_to "New product", new_product_path %> |
This file contains hidden or 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
<tr> | |
<% @object.attributes.each do |key, value| %> | |
<td id="<%= "#{helpers.dom_id(@object)}_#{key}" %>"><%= value %></td> | |
<% end %> | |
</tr> |
This file contains hidden or 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
# frozen_string_literal: true | |
class RowComponent < ApplicationComponent | |
def initialize(object) | |
@object = object | |
end | |
end |
This file contains hidden or 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
<table> | |
<thead> | |
<% column_names.each do |column_name| %> | |
<th><%= column_name %></th> | |
<% end %> | |
</thead> | |
<tbody> | |
<% rows.each do |row| %> | |
<%= row %> | |
<% end %> | |
</tbody> | |
</table> |
This file contains hidden or 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
# frozen_string_literal: true | |
class TableComponent < ApplicationComponent | |
renders_many :rows, ->(object, component_class, **params) { component_class.new(object, **params) } | |
def column_names | |
Product.new.attributes.keys | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment