Skip to content

Commit

Permalink
feat: allow to set a empty state for index table
Browse files Browse the repository at this point in the history
  • Loading branch information
sifthedog committed Oct 23, 2024
1 parent b7bd93e commit 812e09e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
36 changes: 20 additions & 16 deletions app/components/polaris/index_table_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<%= render Polaris::BaseComponent.new(**system_arguments) do %>
<div class="Polaris-IndexTable-ScrollContainer">
<table class="Polaris-IndexTable__Table">
<thead>
<tr>
<% columns.each_with_index do |column, index| %>
<th class="Polaris-IndexTable__TableHeading">
<%= column.title %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% @data.each do |row| %>
<%= render Polaris::BaseComponent.new(**row_arguments(row)) do %>
<% if @data.empty? %>
<%= empty_state %>
<% else %>
<div class="Polaris-IndexTable-ScrollContainer">
<table class="Polaris-IndexTable__Table">
<thead>
<tr>
<% columns.each_with_index do |column, index| %>
<%= render_cell(flush: column.flush, **column.system_arguments) do %>
<%= column.call(row) %>
<th class="Polaris-IndexTable__TableHeading">
<%= column.title %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% @data.each do |row| %>
<%= render Polaris::BaseComponent.new(**row_arguments(row)) do %>
<% columns.each_with_index do |column, index| %>
<%= render_cell(flush: column.flush, **column.system_arguments) do %>
<%= column.call(row) %>
<% end %>
<% end %>
<% end %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions app/components/polaris/index_table_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class IndexTableComponent < Polaris::Component
renders_many :columns, ->(title, **system_arguments, &block) do
IndexTable::ColumnComponent.new(title, **system_arguments, &block)
end
renders_one :empty_state

def initialize(data, **system_arguments)
@data = data
Expand Down
3 changes: 3 additions & 0 deletions demo/app/previews/index_table_component_preview.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class IndexTableComponentPreview < ViewComponent::Preview
def without_checkboxes
end

def empty_state
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% data = [] %>
<%= polaris_index_table(data) do |table| %>
<% table.with_column("Name") do |row| %>
<%= polaris_text_strong { row[:name] } %>
<% end %>

<% table.with_empty_state do %>
<%= polaris_card do %>
<%= polaris_empty_state(
heading: "Manage your inventory transfers",
image: "https://cdn.shopify.com/s/files/1/0262/4071/2726/files/emptystate-files.png",
) do |state| %>
<% state.with_primary_action { "Add transfer" } %>
<% state.with_secondary_action(url: "#") { "Learn more" } %>
<p>Track and receive your incoming inventory from suppliers.</p>
<% end %>
<% end %>
<% end %>
<% end %>
10 changes: 10 additions & 0 deletions test/components/polaris/index_table_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ def test_default_table
end
end
end

def test_empty_state
render_inline(Polaris::IndexTableComponent.new([])) do |table|
table.with_empty_state do |row|
"Empty state"
end
end

assert_selector ".Polaris-IndexTable", text: "Empty state"
end
end

0 comments on commit 812e09e

Please sign in to comment.