View Source Backpex.Field behaviour (Backpex v0.8.2)

Behaviour implemented by all fields.

A field defines how a column is rendered on index, show and edit views. In the resource configuration file you can configure a list of fields. You may create your own field by implementing this behaviour. A field has to be a LiveComponent.

Example

def fields do
  [
    rating: %{
      module: Backpex.Fields.Text,
      label: "Rating"
    }
  ]
end

Summary

Callbacks

This function will be called in the FormComponent and may be used to assign uploads.

Determines whether the field is an association or not.

This function is called before the changeset function is called. This allows fields to modify the changeset. The Backpex.Fields.HasMany uses this callback to put the linked associations into the changeset.

The field to be displayed on index views. In most cases this is the name / key configured in the corresponding field definition. In fields with associations this value often differs from the name / key. The function will receive the field definition.

Will be used on edit views to render a form for the value of the provided item. This has to be a heex template.

Used to render the readonly version of the field.

Used to render form on index to support index editable.

Will be used on index and show views to render a value from the provided item. This has to be a heex template.

The schema to be used in queries. In most cases this is the schema defined in the resource configuration. In fields with associations this is the schema of the corresponding relation. The function will receive the field definition and the schema defined in the resource configuration.

Defines the search condition. Defaults to an ilike condition with text comparison. The function has to return a query wrapped into a Ecto.Query.dynamic/2 which is then passed into a Ecto.Query.where/3.

Functions

Defines Backpex.Field behaviour and provides default implementations.

Returns a map of types from a list of fields used for the Ecto changeset.

Defines debounce timeout value.

Checks whether index editable is enabled or not.

Defines placeholder value.

Determines whether the field should be rendered as readonly version.

Defines throttle timeout value.

Callbacks

Link to this callback

assign_uploads(field, socket)

View Source
@callback assign_uploads(field :: tuple(), socket :: Phoenix.LiveView.Socket.t()) ::
  Phoenix.LiveView.Socket.t()

This function will be called in the FormComponent and may be used to assign uploads.

@callback association?(field :: tuple()) :: boolean()

Determines whether the field is an association or not.

Link to this callback

before_changeset(changeset, attrs, metadata, repo, field, assigns)

View Source
@callback before_changeset(
  changeset :: Phoenix.LiveView.Socket.t(),
  attrs :: map(),
  metadata :: keyword(),
  repo :: module(),
  field :: tuple(),
  assigns :: map()
) :: Ecto.Changeset.t()

This function is called before the changeset function is called. This allows fields to modify the changeset. The Backpex.Fields.HasMany uses this callback to put the linked associations into the changeset.

@callback display_field(field :: tuple()) :: atom()

The field to be displayed on index views. In most cases this is the name / key configured in the corresponding field definition. In fields with associations this value often differs from the name / key. The function will receive the field definition.

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

Will be used on edit views to render a form for the value of the provided item. This has to be a heex template.

Link to this callback

render_form_readonly(assigns)

View Source (optional)
@callback render_form_readonly(assigns :: map()) :: %Phoenix.LiveView.Rendered{
  caller: term(),
  dynamic: term(),
  fingerprint: term(),
  root: term(),
  static: term()
}

Used to render the readonly version of the field.

Link to this callback

render_index_form(assigns)

View Source (optional)
@callback render_index_form(assigns :: map()) :: %Phoenix.LiveView.Rendered{
  caller: term(),
  dynamic: term(),
  fingerprint: term(),
  root: term(),
  static: term()
}

Used to render form on index to support index editable.

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

Will be used on index and show views to render a value from the provided item. This has to be a heex template.

@callback schema(field :: tuple(), schema :: atom()) :: atom()

The schema to be used in queries. In most cases this is the schema defined in the resource configuration. In fields with associations this is the schema of the corresponding relation. The function will receive the field definition and the schema defined in the resource configuration.

Link to this callback

search_condition(schema_name, field_name, search_string)

View Source
@callback search_condition(
  schema_name :: binary(),
  field_name :: binary(),
  search_string :: binary()
) :: Ecto.Query.dynamic_expr()

Defines the search condition. Defaults to an ilike condition with text comparison. The function has to return a query wrapped into a Ecto.Query.dynamic/2 which is then passed into a Ecto.Query.where/3.

Example

Imagine the underlying database type of the field is an integer. Before text comparison in an ilike condition you have to cast the integer to text.

The function could return the following query to make the field searchable.

dynamic(
  [{^schema_name, schema_name}],
  ilike(fragment("CAST(? AS TEXT)", schema_name |> field(^field_name)), ^search_string)
)

Functions

Defines Backpex.Field behaviour and provides default implementations.

Link to this function

align_label(field_options, assigns, default \\ :center)

View Source

Gets alignment option for label.

Returns a map of types from a list of fields used for the Ecto changeset.

Defines debounce timeout value.

Link to this function

handle_index_editable(socket, value, change)

View Source

Handles index editable.

Link to this function

index_editable_enabled?(field_options, assigns, default \\ false)

View Source

Checks whether index editable is enabled or not.

Link to this function

placeholder(arg1, assigns)

View Source

Defines placeholder value.

Link to this function

readonly?(arg1, assigns)

View Source

Determines whether the field should be rendered as readonly version.

Defines throttle timeout value.