|
| 1 | +defmodule Backpex.Fields.Email do |
| 2 | + @config_schema [ |
| 3 | + placeholder: [ |
| 4 | + doc: "Placeholder value or function that receives the assigns.", |
| 5 | + type: {:or, [:string, {:fun, 1}]} |
| 6 | + ], |
| 7 | + debounce: [ |
| 8 | + doc: "Timeout value (in milliseconds), \"blur\" or function that receives the assigns.", |
| 9 | + type: {:or, [:pos_integer, :string, {:fun, 1}]} |
| 10 | + ], |
| 11 | + throttle: [ |
| 12 | + doc: "Timeout value (in milliseconds) or function that receives the assigns.", |
| 13 | + type: {:or, [:pos_integer, {:fun, 1}]} |
| 14 | + ], |
| 15 | + readonly: [ |
| 16 | + doc: "Sets the field to readonly. Also see the [panels](/guides/fields/readonly.md) guide.", |
| 17 | + type: {:or, [:boolean, {:fun, 1}]} |
| 18 | + ] |
| 19 | + ] |
| 20 | + |
| 21 | + @moduledoc """ |
| 22 | + A field for handling an email value. |
| 23 | +
|
| 24 | + ## Field-specific options |
| 25 | +
|
| 26 | + See `Backpex.Field` for general field options. |
| 27 | +
|
| 28 | + #{NimbleOptions.docs(@config_schema)} |
| 29 | + """ |
| 30 | + use Backpex.Field, config_schema: @config_schema |
| 31 | + |
| 32 | + @impl Backpex.Field |
| 33 | + def render_value(assigns) do |
| 34 | + ~H""" |
| 35 | + <p class={@live_action in [:index, :resource_action] && "truncate"}> |
| 36 | + {HTML.pretty_value(@value)} |
| 37 | + </p> |
| 38 | + """ |
| 39 | + end |
| 40 | + |
| 41 | + @impl Backpex.Field |
| 42 | + def render_form(assigns) do |
| 43 | + ~H""" |
| 44 | + <div> |
| 45 | + <Layout.field_container> |
| 46 | + <:label align={Backpex.Field.align_label(@field_options, assigns, :center)}> |
| 47 | + <Layout.input_label text={@field_options[:label]} /> |
| 48 | + </:label> |
| 49 | + <BackpexForm.input |
| 50 | + type="email" |
| 51 | + field={@form[@name]} |
| 52 | + placeholder={@field_options[:placeholder]} |
| 53 | + translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)} |
| 54 | + help_text={Backpex.Field.help_text(@field_options, assigns)} |
| 55 | + phx-debounce={Backpex.Field.debounce(@field_options, assigns)} |
| 56 | + phx-throttle={Backpex.Field.throttle(@field_options, assigns)} |
| 57 | + readonly={@readonly} |
| 58 | + disabled={@readonly} |
| 59 | + /> |
| 60 | + </Layout.field_container> |
| 61 | + </div> |
| 62 | + """ |
| 63 | + end |
| 64 | + |
| 65 | + @impl Backpex.Field |
| 66 | + def render_index_form(assigns) do |
| 67 | + form = to_form(%{"value" => assigns.value}, as: :index_form) |
| 68 | + |
| 69 | + assigns = |
| 70 | + assigns |
| 71 | + |> assign_new(:form, fn -> form end) |
| 72 | + |> assign_new(:valid, fn -> true end) |
| 73 | + |
| 74 | + ~H""" |
| 75 | + <div> |
| 76 | + <.form for={@form} class="relative" phx-change="update-field" phx-submit="update-field" phx-target={@myself}> |
| 77 | + <BackpexForm.input |
| 78 | + id={"index-form-input-#{@name}-#{LiveResource.primary_value(@item, @live_resource)}"} |
| 79 | + type="email" |
| 80 | + field={@form[:value]} |
| 81 | + placeholder={@field_options[:placeholder]} |
| 82 | + input_class={["input input-sm", @valid && "not-hover:input-ghost", !@valid && "input-error bg-error/10"]} |
| 83 | + phx-debounce="100" |
| 84 | + readonly={@readonly} |
| 85 | + hide_errors |
| 86 | + /> |
| 87 | + </.form> |
| 88 | + </div> |
| 89 | + """ |
| 90 | + end |
| 91 | + |
| 92 | + @impl Phoenix.LiveComponent |
| 93 | + def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do |
| 94 | + Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.name, value)) |
| 95 | + end |
| 96 | +end |
0 commit comments