Packages
phoenix_duskmoon
7.2.1
9.9.2
9.9.1
9.9.0
9.8.0
9.7.1
9.7.0
9.6.4
9.6.3
9.6.2
9.6.1
9.6.0
9.5.4
9.5.3
9.5.2
9.5.1
9.5.0
9.4.3
9.4.2
9.4.1
9.4.0
9.3.0
9.2.0
9.1.4
9.1.3
9.1.1
9.1.1-rc.1
retired
9.0.1
9.0.0-rc.3
9.0.0-rc.2
9.0.0-rc.1
8.0.0
7.2.1
7.2.0
7.1.3
7.1.2
7.1.1
6.3.2
6.3.1
6.3.0
6.2.0
6.1.3
6.1.2
6.1.1
6.1.0
6.0.10
6.0.9
6.0.8
6.0.7
6.0.6
6.0.5
6.0.4
6.0.3
6.0.2
6.0.1
6.0.0
5.2.0-beta.5
5.2.0-beta.4
5.2.0-beta.3
5.2.0-beta.2
5.2.0-beta.1
5.1.0
5.0.6
5.0.5
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
4.6.6
4.6.5
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.2
4.5.1
4.5.0
4.4.4
4.4.3
4.4.2
4.4.1
4.4.0
4.3.2
4.3.1
4.3.0
4.2.0
4.1.1
4.1.0
4.0.0
Duskmoon UI component library for Phoenix LiveView
Current section
Files
Jump to
Current section
Files
lib/phoenix_duskmoon/component/form/compact_input.ex
defmodule PhoenixDuskmoon.Component.Form.CompactInput do
@moduledoc """
Compact form input components for PhoenixDuskmoon UI.
This module provides compact input components that combine the label
and input in a more condensed layout, useful for forms with limited space
or when a more streamlined appearance is desired.
## Examples
<.dm_compact_input field={@form[:email]} type="email" label="Email" />
<.dm_compact_input name="username" label="Username" value="john" />
<.dm_compact_input type="select" name="country" label="Country" options={[{"USA", "us"}, {"Canada", "ca"}]} />
"""
use PhoenixDuskmoon.Component, :html
import PhoenixDuskmoon.Component.Form
@doc """
Renders a compact input with label and error messages.
A `Phoenix.HTML.FormField` may be passed as argument,
which is used to retrieve the input name, id, and values.
Otherwise all attributes may be passed explicitly.
## Types
Only input and select are supported.
## Examples
<.dm_compact_input field={@form[:email]} type="email" />
<.dm_compact_input name="my-input" errors={["oh no!"]} />
"""
attr(:field_class, :any, default: nil)
attr(:id, :any, default: nil)
attr(:class, :any, default: nil)
attr(:name, :any)
attr(:label, :string, default: nil)
attr(:value, :any)
attr(:type, :string,
default: "text",
values: ~w(color date datetime-local email file month number password
search select tel text time url week)
)
attr(:field, Phoenix.HTML.FormField,
doc: "a form field struct retrieved from the form, for example: @form[:email]"
)
attr(:errors, :list, default: [])
attr(:checked, :boolean, doc: "the checked flag for checkbox inputs")
attr(:prompt, :string, default: nil, doc: "the prompt for select inputs")
attr(:options, :list, doc: "the options to pass to Phoenix.HTML.Form.options_for_select/2")
attr(:multiple, :boolean, default: false, doc: "the multiple flag for select inputs")
attr(:rest, :global,
include: ~w(accept autocomplete capture cols disabled form list max maxlength min minlength
multiple pattern placeholder readonly required rows size step)
)
slot(:inner_block)
def dm_compact_input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
assigns
|> assign(field: nil, id: assigns.id || field.id)
# |> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
|> assign_new(:value, fn -> field.value end)
|> dm_compact_input()
end
def dm_compact_input(%{type: "select"} = assigns) do
~H"""
<div
class={[
"select select-bordered",
@errors != [] && "select-error",
@class
]}
>
<label for={@id} class={["label", @errors != [] && "text-error"]}>
<%= @label %>
</label>
<select
id={@id}
name={@name}
class={[
"compact-select",
]}
multiple={@multiple}
{@rest}
>
<option :if={@prompt} value=""><%= @prompt %></option>
<%= Phoenix.HTML.Form.options_for_select(@options, @value) %>
</select>
</div>
<.dm_error :for={msg <- @errors}><%= msg %></.dm_error>
"""
end
def dm_compact_input(assigns) do
~H"""
<div
class={[
"input input-bordered",
@errors != [] && "input-error",
@class
]}
phx-feedback-for={@name}
>
<label for={@id} class={["label", @errors != [] && "text-error"]}>
<%= @label %>
</label>
<input
type={@type}
name={@name}
id={@id}
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
class={[
"compact-input",
]}
{@rest}
/>
<%= render_slot(@inner_block) %>
</div>
<.dm_error :for={msg <- @errors}><%= msg %></.dm_error>
"""
end
end