View Source Backpex.ItemAction behaviour (Backpex v0.9.1)
Behaviour implemented by all item actions.
Summary
Callbacks
The base item / schema to use for the changeset. The result will be passed as the first parameter to changeset/3
each time it is called.
cancel button label
The changeset to be used in the resource action. It may be used to validate form inputs.
This text is being displayed in the confirm dialog.
Confirm button label
A list of fields to be displayed in the item action. See Backpex.Field
. In addition you have to provide
a type
for each field in order to support changeset generation.
Performs the action. It takes the socket, the list of affected items, and the casted and validated data (received from Ecto.Changeset.apply_action/2
).
Action icon
Action label (Show label on hover)
Functions
Defines Backpex.ItemAction
behaviour and provides default implementations.
Returns default item actions.
Checks whether item action has confirmation modal.
Checks whether item action has form.
Callbacks
@callback base_schema(assigns :: map()) :: Ecto.Schema.t() | Ecto.Changeset.t() | {Ecto.Changeset.data(), Ecto.Changeset.types()}
The base item / schema to use for the changeset. The result will be passed as the first parameter to changeset/3
each time it is called.
This function is optional and can be used to use changesets with schemas in item actions. If this function is not provided,
a schemaless changeset will be created with the provided types from fields/0
.
cancel button label
@callback changeset( change :: Ecto.Schema.t() | Ecto.Changeset.t() | {Ecto.Changeset.data(), Ecto.Changeset.types()}, attrs :: map(), metadata :: keyword() ) :: Ecto.Changeset.t()
The changeset to be used in the resource action. It may be used to validate form inputs.
Additional metadata is passed as a keyword list via the third parameter.
The list of metadata:
:assigns
- the assigns:target
- the name of theform
target that triggered the changeset call. Default tonil
if the call was not triggered by a form field.
This text is being displayed in the confirm dialog.
There won't be any confirmation when this function is not defined.
Confirm button label
@callback fields() :: list()
A list of fields to be displayed in the item action. See Backpex.Field
. In addition you have to provide
a type
for each field in order to support changeset generation.
The following fields are currently not supported:
@callback handle( socket :: Phoenix.LiveView.Socket.t(), items :: [map()], params :: map() | struct() ) :: {:ok, Phoenix.LiveView.Socket.t()} | {:error, Ecto.Changeset.t()}
Performs the action. It takes the socket, the list of affected items, and the casted and validated data (received from Ecto.Changeset.apply_action/2
).
You must return either {:ok, socket}
or {:error, changeset}
.
If {:ok, socket}
is returned, the action is considered successful by Backpex and the action modal is closed. However, you can add an error flash message to the socket to indicate that something has gone wrong.
If {:error, changeset}
is returned, the changeset is used to update the form to display the errors. Note that Backpex already validates the form for you.
Therefore it is only necessary in rare cases to perform additional validation and return a changeset from handle/3
.
For example, if you are building a duplicate action and can only check for a unique constraint when inserting the duplicate element.
You are only allowed to return {:error, changeset}
if the action has a form. Otherwise Backpex will throw an ArgumentError.
@callback icon(assigns :: map(), item :: struct()) :: %Phoenix.LiveView.Rendered{ caller: term(), dynamic: term(), fingerprint: term(), root: term(), static: term() }
Action icon
Action label (Show label on hover)
Functions
Defines Backpex.ItemAction
behaviour and provides default implementations.
Returns default item actions.
Checks whether item action has confirmation modal.
Checks whether item action has form.