defmodule SigmaKit.Components.Table do use Phoenix.LiveComponent import SigmaKit.Components.Icons, only: [icon: 1] @doc """ Renders a table with generic styling. ## Examples <.table id="users" rows={@users}> <:col :let={user} label="id">{user.id} <:col :let={user} label="username">{user.username} """ attr :id, :string, required: true attr :rows, :list, required: true attr :row_id, :any, default: nil, doc: "the function for generating the row id" attr :row_click, :any, default: nil, doc: "the function for handling phx-click on each row" attr :alternate, :boolean, default: false attr :paginate, :boolean, default: false attr :page_number, :integer attr :total_pages, :integer attr :page_size, :integer attr :total_entries, :integer attr :row_item, :any, default: &Function.identity/1, doc: "the function for mapping each row before calling the :col and :action slots" slot :col, required: true do attr :label, :string end slot :action do attr :label, :string attr :icon, :string attr :event, :string end def table(%{rows: %Scrivener.Page{}} = assigns) do assigns |> assign(rows: assigns.rows.entries) |> assign(page_number: assigns.rows.page_number) |> assign(total_pages: assigns.rows.total_pages) |> assign(total_entries: assigns.rows.total_entries) |> assign(page_size: assigns.rows.page_size) |> assign(paginate: true) |> table() end def table(assigns) do assigns = with %{rows: %Phoenix.LiveView.LiveStream{}} <- assigns do assign(assigns, row_id: assigns.row_id || fn {id, _item} -> id end) end ~H"""
| {col[:label]} | |
|---|---|
|
{render_slot(col, @row_item.(row))}
|