defmodule Phoenix.UI.Components.Table do @moduledoc """ Provides a table components. """ import Phoenix.UI.Components.Paper use Phoenix.UI, :component @doc """ Renders table component. ## Examples ``` <.table> <.tr> <.th>Month <.th>Savings <.tr> <.td>January <.td>$100 ``` """ @spec table(Socket.assigns()) :: Rendered.t() def table(assigns) do assigns |> build_paper_assigns() |> generate_markup() end @doc """ Renders body in table. ## Examples ``` <.table> <.tbody> <.tr> <.td>Cell A <.td>Cell B ``` """ @spec tbody(Socket.assigns()) :: Rendered.t() def tbody(assigns) do ~H""" <%= render_slot(@inner_block) %> """ end @doc """ Renders standard data cell in table. ## Examples ``` <.table> <.tr> <.td>Cell A <.td>Cell B ``` """ @spec td(Socket.assigns()) :: Rendered.t() def td(assigns) do ~H""" <%= render_slot(@inner_block) %> """ end @doc """ Renders footer in table. ## Examples ``` <.table> <.tfoot> <.tr> <.th>Sum <.th>$180 ``` """ @spec tfoot(Socket.assigns()) :: Rendered.t() def tfoot(assigns) do ~H""" <%= render_slot(@inner_block) %> """ end @doc """ Renders header cell in table. ## Examples ``` <.table> <.thead> <.tr> <.th>Month <.th>Savings ``` """ @spec th(Socket.assigns()) :: Rendered.t() def th(raw_assigns) do attrs = Map.drop(raw_assigns, [:__changed__, :inner_block]) assigns = Map.put(raw_assigns, :attrs, attrs) ~H""" <%= render_slot(@inner_block) %> """ end @doc """ Renders header in table. ## Examples ``` <.table> <.thead> ... ``` """ @spec thead(Socket.assigns()) :: Rendered.t() def thead(assigns) do ~H""" <%= render_slot(@inner_block) %> """ end @doc """ Renders row in table. ## Examples ``` <.table> <.tr> ... ``` """ @spec tr(Socket.assigns()) :: Rendered.t() def tr(assigns) do ~H""" <%= render_slot(@inner_block) %> """ end ############################################################################################# ### Markup ############################################################################################# defp generate_markup(assigns) do ~H""" <.paper {@paper} extend_class="overflow-x-auto" variant="elevated"> <%= render_slot(@inner_block) %>
""" end ############################################################################################# ### Paper Assigns ############################################################################################# defp build_paper_assigns(assigns) do paper = Map.drop(assigns, [ :__changed__, :inner_block ]) assign(assigns, :paper, paper) end end