diff --git a/lib/backpex/item_actions/item_action.ex b/lib/backpex/item_actions/item_action.ex index 2add1ef7..f1a74715 100644 --- a/lib/backpex/item_actions/item_action.ex +++ b/lib/backpex/item_actions/item_action.ex @@ -33,6 +33,14 @@ defmodule Backpex.ItemAction do | Ecto.Changeset.t() | {Ecto.Changeset.data(), Ecto.Changeset.types()} + @doc """ + The initial params for the changeset. The result is passed as the second parameter to `c:changeset/3` the first time it is called. + + This function is optional and can be used to set an initial change, e.g. to pre-populate the form with values. + If this function is not provided, an empty map will be used. + """ + @callback init_params(assigns :: map()) :: map() + @doc """ The changeset to be used in the resource action. It may be used to validate form inputs. @@ -117,6 +125,9 @@ defmodule Backpex.ItemAction do {%{}, types} end + + @impl Backpex.ItemAction + def init_params(_assigns), do: %{} end end end diff --git a/lib/backpex/live_resource.ex b/lib/backpex/live_resource.ex index dcca8cab..d6649610 100644 --- a/lib/backpex/live_resource.ex +++ b/lib/backpex/live_resource.ex @@ -1174,12 +1174,13 @@ defmodule Backpex.LiveResource do defp open_action_confirm_modal(socket, action, key) do base_item = action.module.base_item(socket.assigns) + init_params = action.module.init_params(socket.assigns) changeset_function = &action.module.changeset/3 metadata = Resource.build_changeset_metadata(socket.assigns) - changeset = changeset_function.(base_item, %{}, metadata) + changeset = changeset_function.(base_item, init_params, metadata) socket = socket