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

base_schema(assigns)

@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_label(assigns)

(optional)
@callback cancel_label(assigns :: map()) :: binary()

cancel button label

changeset(change, attrs, metadata)

(optional)
@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 the form target that triggered the changeset call. Default to nil if the call was not triggered by a form field.

confirm(assigns)

(optional)
@callback confirm(assigns :: map()) :: binary()

This text is being displayed in the confirm dialog.

There won't be any confirmation when this function is not defined.

confirm_label(assigns)

(optional)
@callback confirm_label(assigns :: map()) :: binary()

Confirm button label

fields()

(optional)
@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:

handle(socket, items, params)

@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.

icon(assigns, item)

@callback icon(assigns :: map(), item :: struct()) :: %Phoenix.LiveView.Rendered{
  caller: term(),
  dynamic: term(),
  fingerprint: term(),
  root: term(),
  static: term()
}

Action icon

label(assigns, item)

@callback label(assigns :: map(), item :: struct() | nil) :: binary()

Action label (Show label on hover)

Functions

__using__(_)

(macro)

Defines Backpex.ItemAction behaviour and provides default implementations.

default_actions()

Returns default item actions.

has_confirm_modal?(item_action)

Checks whether item action has confirmation modal.

has_form?(item_action)

Checks whether item action has form.