Skip to content

Commit

Permalink
refactor sort to optional slot
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhitworth committed May 26, 2024
1 parent 9dc3c9f commit 8c7ddf7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
23 changes: 19 additions & 4 deletions lib/phoenix_better_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule PhoenixBetterTable do
* `:filter_control` - an optional slot that takes a single argument, a tuple of `{active?, id, myself}`. The interactive element should
set `phx-click="filter_toggle"`, `phx-value-header={id}`, and `phx-target={myself}` for the event to be routed correctly.
* `:sort_control` - an optional slot that takes a single argument, a tuple of `{direction, id, myself}`. See above.
"""

use Phoenix.LiveComponent
Expand Down Expand Up @@ -91,6 +92,7 @@ defmodule PhoenixBetterTable do
|> assign_new(:engine_module, fn -> engine_module() end)
|> assign_new(:sort, fn -> nil end)
|> assign_new(:filter, fn -> %{} end)
|> assign_new(:sort_control, fn -> nil end)
|> assign_new(:filter_control, fn -> nil end)
|> assign_new(:class, fn -> "" end)
|> assign_new(:body_class, fn -> "" end)
Expand Down Expand Up @@ -154,10 +156,23 @@ defmodule PhoenixBetterTable do

defp column_filter(_), do: &to_string/1

defp sort_arrow(nil, _column), do: "—"
defp sort_arrow({column, order}, column) when order == :asc, do: "▲"
defp sort_arrow({column, order}, column) when order == :desc, do: "▼"
defp sort_arrow(_, _), do: "—"
defp column_sort_order({column, order}, column), do: order
defp column_sort_order(_, _), do: nil

attr(:direction, :atom, required: true)
attr(:rest, :global)

defp sort_control(assigns) do
~H"""
<a style="color: #000000" href="#" {@rest}>
<%= case @direction do
nil -> "—"
:asc -> "▲"
:desc -> "▼"
end %>
</a>
"""
end

attr(:active?, :boolean, required: true)
attr(:rest, :global)
Expand Down
10 changes: 7 additions & 3 deletions lib/phoenix_better_table.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
<tr>
<th :for={header <- @meta.headers} data-test-table-header={header.id}>
<%= Map.get(header, :label, header.id) %>
<a :if={Map.get(header, :sort, true)} style="color: #000000" href="#" phx-click="sort" phx-value-header={header.id} phx-target={@myself}>
<%= sort_arrow(@sort, header.id) %>
</a>
<%= if Map.get(header, :sort, true) do %>
<%= if @sort_control do %>
<%= render_slot(@sort_control, {column_sort_order(@sort, header.id), header.id, @myself}) %>
<% else %>
<.sort_control direction={column_sort_order(@sort, header.id)} phx-click="sort" phx-value-header={header.id} phx-target={@myself} />
<% end %>
<% end %>
&nbsp;
<%= if @filter_control do %>
<%= render_slot(@filter_control, {Map.has_key?(@filter, header.id), header.id, @myself}) %>
Expand Down
24 changes: 24 additions & 0 deletions test/phoenix_better_table_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,30 @@ defmodule PhoenixBetterTableTest do
""")
end

test "custom sort control can be passed as slot" do
{:ok, view, _html} =
live_isolated_component(PhoenixBetterTable,
assigns: %{
meta: %{headers: [%{id: :name}]},
rows: [%{name: "John"}, %{name: "Jane"}]
},
slots: %{
sort_control:
slot(let: {direction, id, myself}) do
~H[<span phx-click="sort" phx-value-header={id} phx-target={myself}>Sort (<%= direction %>)</span>]
end
}
)

html = view |> element("span[phx-click='sort']") |> render_click()

assert_table_matches(html, """
name
Jane
John
""")
end

test "custom filter control can be passed as a slot" do
{:ok, view, _html} =
live_isolated_component(PhoenixBetterTable,
Expand Down

0 comments on commit 8c7ddf7

Please sign in to comment.