defmodule Flop.Phoenix do @moduledoc """ Phoenix components for pagination, sortable tables and filter forms with [Flop](https://hex.pm/packages/flop). ## Introduction Please refer to the [Readme](README.md) for an introduction. ## Customization To customize the components, it is recommended to define wrapper components in your `CoreComponents` module that pass attributes that are constant for your application and add additional markup as necessary. For example, to customize the `pagination` component, define your own `pagination` component: ```heex defmodule MyAppWeb.CoreComponents do use Phoenix.Component attr :meta, Flop.Meta, required: true attr :path, :any, default: nil attr :on_paginate, JS, default: nil attr :target, :string, default: nil def pagination(assigns) do ~H\""" \""" end defp next_icon do assigns = %{} ~H\""" \""" end defp previous_icon do assigns = %{} ~H\""" \""" end end ``` The given `opts` attributes are deep-merged into the default options. Refer to `t:pagination_option/0` and `t:table_option/0` for a list of available options and defaults. ## Hiding default parameters Default values for page size and ordering are omitted from the query parameters. If you pass the `:for` assign, the Flop.Phoenix function will pick up the default values from the schema module deriving `Flop.Schema`. ## Links Links are generated with `Phoenix.Components.link/1`. This will lead to `` tags with `data-phx-link` and `data-phx-link-state` attributes, which will be ignored outside of LiveViews and LiveComponents. When used within a LiveView or LiveComponent, you will need to handle the new params in the `c:Phoenix.LiveView.handle_params/3` callback of your LiveView module. ## Using JS commands You can pass a `Phoenix.LiveView.JS` command as `on_paginate` and `on_sort` attributes. If used with the `path` attribute, the URL will be patched _and_ the given JS command will be executed. If used without the `path` attribute, you will need to include a `push` command to trigger an event when a pagination or sort link is clicked. You can set a different target setting the `target` attribute. The value will be used as the `phx-target` attribute. ```heex ``` You will need to handle the event in the `c:Phoenix.LiveView.handle_event/3` or `c:Phoenix.LiveComponent.handle_event/3` callback of your LiveView or LiveComponent module. def handle_event("paginate-pets", %{"page" => page}, socket) do flop = Flop.set_page(socket.assigns.meta.flop, page) with {:ok, {pets, meta}} <- Pets.list_pets(flop) do {:noreply, assign(socket, pets: pets, meta: meta)} end end def handle_event("sort-pets", %{"order" => order}, socket) do flop = Flop.push_order(socket.assigns.meta.flop, order) with {:ok, {pets, meta}} <- Pets.list_pets(flop) do {:noreply, assign(socket, pets: pets, meta: meta)} end end """ use Phoenix.Component import Phoenix.HTML.Form, only: [ input_id: 2, input_name: 2 ] alias Flop.Meta alias Flop.Phoenix.Misc alias Flop.Phoenix.Pagination alias Flop.Phoenix.Table alias Phoenix.HTML.Form alias Phoenix.LiveView.JS alias Plug.Conn.Query @typedoc """ Defines the available options for `Flop.Phoenix.pagination/1`. - `:current_link_attrs` - The attributes for the link to the current page. Default: `#{inspect(Pagination.default_opts()[:current_link_attrs])}`. - `:disabled_class` - The class which is added to disabled links. Default: `#{inspect(Pagination.default_opts()[:disabled_class])}`. - `:ellipsis_attrs` - The attributes for the `` that wraps the ellipsis. Default: `#{inspect(Pagination.default_opts()[:ellipsis_attrs])}`. - `:ellipsis_content` - The content for the ellipsis element. Default: `#{inspect(Pagination.default_opts()[:ellipsis_content])}`. - `:next_link_attrs` - The attributes for the link to the next page. Default: `#{inspect(Pagination.default_opts()[:next_link_attrs])}`. - `:next_link_content` - The content for the link to the next page. Default: `#{inspect(Pagination.default_opts()[:next_link_content])}`. - `:pagination_link_aria_label` - 1-arity function that takes a page number and returns an aria label for the corresponding page link. Default: `&"Go to page \#{&1}"`. - `:pagination_link_attrs` - The attributes for the pagination links. Default: `#{inspect(Pagination.default_opts()[:pagination_link_attrs])}`. - `:pagination_list_attrs` - The attributes for the pagination list. Default: `#{inspect(Pagination.default_opts()[:pagination_list_attrs])}`. - `:pagination_list_item_attrs` - The attributes for the pagination list items. Default: `#{inspect(Pagination.default_opts()[:pagination_list_item_attrs])}`. - `:previous_link_attrs` - The attributes for the link to the previous page. Default: `#{inspect(Pagination.default_opts()[:previous_link_attrs])}`. - `:previous_link_content` - The content for the link to the previous page. Default: `#{inspect(Pagination.default_opts()[:previous_link_content])}`. - `:wrapper_attrs` - The attributes for the `