From 1f017ced4a706e2e1ef84ae1c923c4c4f62b2e84 Mon Sep 17 00:00:00 2001 From: shibao Date: Wed, 9 Nov 2022 21:03:46 -0500 Subject: [PATCH] add inital_key and initial_sort_mode to table component --- lib/cannery_web/components/table_component.ex | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/cannery_web/components/table_component.ex b/lib/cannery_web/components/table_component.ex index 94c8a35..44bd8f9 100644 --- a/lib/cannery_web/components/table_component.ex +++ b/lib/cannery_web/components/table_component.ex @@ -36,18 +36,38 @@ defmodule CanneryWeb.Components.TableComponent do list(%{ (key :: atom()) => any() | {custom_sort_value :: String.t(), value :: any()} }), + optional(:inital_key) => atom(), + optional(:initial_sort_mode) => atom(), optional(any()) => any() }, Socket.t() ) :: {:ok, Socket.t()} def update(%{columns: columns, rows: rows} = assigns, socket) do - initial_key = columns |> List.first() |> Map.get(:key) - rows = rows |> Enum.sort_by(fn row -> row |> Map.get(initial_key) end, :asc) + initial_key = + if assigns |> Map.has_key?(:initial_key) do + assigns.initial_key + else + columns |> List.first() |> Map.get(:key) + end + + initial_sort_mode = + if assigns |> Map.has_key?(:initial_sort_mode) do + assigns.initial_sort_mode + else + :asc + end + + rows = rows |> Enum.sort_by(fn row -> row |> Map.get(initial_key) end, initial_sort_mode) socket = socket |> assign(assigns) - |> assign(columns: columns, rows: rows, last_sort_key: initial_key, sort_mode: :asc) + |> assign( + columns: columns, + rows: rows, + last_sort_key: initial_key, + sort_mode: initial_sort_mode + ) {:ok, socket} end