Skip to content
Merged
81 changes: 71 additions & 10 deletions CHANGELOG.md

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions app/components/table_components/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class TableComponents::Table < ViewComponent::Base
<div class="overflow-x-scroll bg-white">
<div id="<%= @options[:turbo_id] %>" class="grid" style="<%= grid_columns %>">
<%= render TableComponents::Column.with_collection(@row_component::columns(**@column_arguments), order_scope_name: @order_scope_name) %>
<% if @options[:turbo_id] %> <div id="turbo-separator" class='h-px' style="grid-column: 1/-1;"></div> <% end %>
<% if @options[:turbo_id] %> <div id="turbo-separator" class='h-px col-span-full'></div> <% end %>
<%= render @row_component.with_collection(@rows, pagy: @pagy, **@row_arguments) %>
<% if @rows.empty? && empty_message %>
<div class="py-8 text-center text-gray-500 col-span-full"><%= empty_message %></div>
<% end %>
</div>
</div>
ERB

# rubocop:disable Metrics/ParameterLists
def initialize(row_component:, rows:, column_arguments: {}, row_arguments: {}, order_scope_name: :table_order, pagy: nil, options: {})
@row_component = row_component
Expand All @@ -35,4 +37,10 @@ def initialize(row_component:, rows:, column_arguments: {}, row_arguments: {}, o
def grid_columns
"grid-template-columns: repeat(#{@row_component.columns(**@column_arguments).count}, #{@options[:wrap] ? 'minmax(max-content, auto)' : 'auto'})"
end

def empty_message
return if @options[:empty_message] == false

@options[:empty_message] || I18n.t(:no_results_found)
end
end
2 changes: 1 addition & 1 deletion app/views/chapters/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<div id="new_chapter"></div>

<%= render TableComponents::Table.new(pagy: @pagy, rows: @chapters, row_component: TableComponents::ChapterRow) do |t|
<%= render TableComponents::Table.new(pagy: @pagy, rows: @chapters, row_component: TableComponents::ChapterRow, options: { empty_message: t(:no_chapters_found) }) do |t|
t.with_left do %>
<div class="flex items-center justify-between">
<div class="flex-1">
Expand Down
2 changes: 1 addition & 1 deletion app/views/groups/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
].compact) %>

<div id="new_group"></div>
<%= render TableComponents::Table.new(pagy: @pagy, rows: @groups, row_component: TableComponents::GroupRow) do |t|
<%= render TableComponents::Table.new(pagy: @pagy, rows: @groups, row_component: TableComponents::GroupRow, options: { empty_message: t(:no_groups_found) }) do |t|
t.with_left do %>
<div class="flex items-center justify-between">
<div class="flex-1">
Expand Down
2 changes: 1 addition & 1 deletion app/views/organizations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<div id="new_organization"></div>

<%= render TableComponents::Table.new(pagy: @pagy, rows: @organizations, row_component: TableComponents::OrganizationRow) do |t|
<%= render TableComponents::Table.new(pagy: @pagy, rows: @organizations, row_component: TableComponents::OrganizationRow, options: { empty_message: t(:no_organizations_found) }) do |t|
t.with_left do %>
<div class="flex items-center justify-between">
<div class="flex-1">
Expand Down
2 changes: 1 addition & 1 deletion app/views/students/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(CommonComponents::ButtonComponent.new(label: t(:add_student), href: new_student_path) if policy(Student).new?)
].compact) %>

<%= render TableComponents::Table.new(pagy: @pagy, rows: @student_rows, row_component: TableComponents::StudentRow) do |t|
<%= render TableComponents::Table.new(pagy: @pagy, rows: @student_rows, row_component: TableComponents::StudentRow, options: { empty_message: t(:no_students_found) }) do |t|
t.with_left do %>
<div class="flex items-center justify-between">
<div class="flex-1">
Expand Down
6 changes: 5 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ en:
health_insurance: Health Insurance
health_issues: Health Issues
hiv_tested: HIV Tested
no_students_found: No Students found
no_groups_found: No Groups found
no_chapters_found: No Chapters found
no_organizations_found: No Organizations found
no_results_found: No results found

confirm: Confirm
import: Import
Expand Down Expand Up @@ -451,7 +456,6 @@ en:
one: MLID
other: MLID


errors:
messages:
extension_whitelist_error: File is not an image
Expand Down
51 changes: 51 additions & 0 deletions spec/components/table_components/table_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'rails_helper'

# Minimal stub to test TableComponents::Table in isolation without policy concerns
class StubTableRow < ViewComponent::Base
with_collection_parameter :item

def self.columns(**_)
[{ column_name: 'Name' }]
end

def initialize(item:, **_)
@item = item
super()
end

erb_template '<div class="stub-row"><%= @item %></div>'
end

RSpec.describe TableComponents::Table, type: :component do
let(:row_component) { StubTableRow }

context 'when rows is empty' do
it 'renders the default empty message when no empty_message option is provided' do
render_inline(described_class.new(rows: [], row_component: row_component))

expect(page).to have_text(I18n.t(:no_results_found))
end

it 'renders the empty message when the empty_message option is provided' do
render_inline(described_class.new(rows: [], row_component: row_component, options: { empty_message: 'No items found' }))

expect(page).to have_text('No items found')
end

it 'does not render an empty message when empty_message is false' do
render_inline(described_class.new(rows: [], row_component: row_component, options: { empty_message: false }))

expect(page).not_to have_text(I18n.t(:no_results_found))
expect(page).not_to have_text('No items found')
end
end

context 'when rows is not empty' do
it 'does not render the empty message' do
render_inline(described_class.new(rows: ['Alice'], row_component: row_component, options: { empty_message: 'No items found' }))

expect(page).not_to have_text('No items found')
expect(page).to have_css('.stub-row', text: 'Alice')
end
end
end
Loading