Skip to content

Commit 83afde9

Browse files
authored
Merge pull request #1472 from nolman/email-field
Add `Backpex.Fields.Email`
2 parents 77e498f + 81a5c89 commit 83afde9

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

guides/fields/index-edit.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ The example above will enable index editable for the `name` text field.
2626
- `Backpex.Fields.BelongsTo`
2727
- `Backpex.Fields.Date`
2828
- `Backpex.Fields.DateTime`
29+
- `Backpex.Fields.Email`
2930
- `Backpex.Fields.Number`
3031
- `Backpex.Fields.Select`
3132
- `Backpex.Fields.Text`
3233

3334
## Custom index editable implementation
3435

35-
You can add index editable support to your custom fields by defining the [render_index_form/1](Backpex.Field.html#c:render_index_form/1) function and enabling index editable for your field.
36+
You can add index editable support to your custom fields by defining the [render_index_form/1](Backpex.Field.html#c:render_index_form/1) function and enabling index editable for your field.

guides/fields/what-is-a-field.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Backpex provides the following built-in field types:
1313
- `Backpex.Fields.Currency`
1414
- `Backpex.Fields.DateTime`
1515
- `Backpex.Fields.Date`
16+
- `Backpex.Fields.Email`
1617
- `Backpex.Fields.HasManyThrough`
1718
- `Backpex.Fields.HasMany`
1819
- `Backpex.Fields.InlineCRUD`

lib/backpex/fields/email.ex

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)