Current section

Files

Jump to
backpex lib backpex fields number.ex
Raw

lib/backpex/fields/number.ex

defmodule Backpex.Fields.Number do
@moduledoc """
A field for handling a number value.
## Options
* `:placeholder` - Optional placeholder value or function that receives the assigns.
* `:debounce` - Optional integer timeout value (in milliseconds), "blur" or function that receives the assigns.
* `:throttle` - Optional integer timeout value (in milliseconds) or function that receives the assigns.
"""
use BackpexWeb, :field
import Ecto.Query
@impl Backpex.Field
def render_value(assigns) do
~H"""
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= HTML.pretty_value(@value) %>
</p>
"""
end
@impl Backpex.Field
def render_form(assigns) do
~H"""
<div>
<Layout.field_container>
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
<Layout.input_label text={@field_options[:label]} />
</:label>
<BackpexForm.field_input type="text" field={@form[@name]} field_options={@field_options} />
</Layout.field_container>
</div>
"""
end
@impl Backpex.Field
def render_form_readonly(assigns) do
~H"""
<div>
<Layout.field_container>
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
<Layout.input_label text={@field_options[:label]} />
</:label>
<BackpexForm.field_input type="text" field={@form[@name]} field_options={@field_options} readonly disabled />
</Layout.field_container>
</div>
"""
end
@impl Backpex.Field
def render_index_form(assigns) do
form = to_form(%{"value" => assigns.value}, as: :index_form)
assigns =
assigns
|> assign_new(:form, fn -> form end)
|> assign_new(:valid, fn -> true end)
~H"""
<div>
<.form for={@form} class="relative" phx-change="update-field" phx-submit="update-field" phx-target={@myself}>
<input
type="text"
name={@form[:value].name}
value={@form[:value].value}
class={["input input-sm", @valid && "hover:input-bordered", !@valid && "input-error"]}
phx-debounce="100"
readonly={@readonly}
/>
</.form>
</div>
"""
end
@impl Phoenix.LiveComponent
def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do
Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.name, value))
end
@impl Backpex.Field
def search_condition(schema_name, field_name, search_string) do
dynamic(
[{^schema_name, schema_name}],
ilike(fragment("CAST(? AS TEXT)", schema_name |> field(^field_name)), ^search_string)
)
end
end