Current section
Files
Jump to
Current section
Files
lib/flowbite_phoenix/components/forms.ex
defmodule FlowbitePhoenix.Components.Forms do
@moduledoc """
Form components for FlowbitePhoenix using Flowbite CSS framework.
This module provides form-related components including inputs, buttons,
toggles, and form containers with consistent Flowbite styling.
"""
use Phoenix.Component
alias Phoenix.LiveView.JS
@doc """
Renders a simple form with Flowbite styling.
## Examples
<.simple_form for={@form} phx-change="validate" phx-submit="save">
<.input field={@form[:email]} label="Email"/>
<.input field={@form[:username]} label="Username" />
<:actions>
<.button>Save</.button>
</:actions>
</.simple_form>
"""
attr :for, :any, required: true, doc: "the data structure for the form"
attr :as, :any, default: nil, doc: "the server side parameter to collect all input under"
attr :rest, :global,
include: ~w(autocomplete name rel action enctype method novalidate target multipart),
doc: "the arbitrary HTML attributes to apply to the form tag"
slot :inner_block, required: true
slot :actions, doc: "the slot for form actions, such as a submit button"
def simple_form(assigns) do
~H"""
<.form :let={f} for={@for} as={@as} {@rest}>
<div class="space-y-6">
{render_slot(@inner_block, f)}
<div :for={action <- @actions} class="flex items-center justify-end space-x-2 pt-4">
{render_slot(action, f)}
</div>
</div>
</.form>
"""
end
@doc """
Renders a button with Flowbite styling and multiple variants.
## Examples
<.button>Send!</.button>
<.button phx-click="go" color="alternative">Cancel</.button>
<.button loading={true} color="blue">Loading...</.button>
<.button variant="outline" color="red">Delete</.button>
"""
attr :type, :string, default: nil
attr :class, :string, default: nil
attr :loading, :boolean, default: false
attr :variant, :string, default: "default", values: ~w(default outline)
attr :color, :string, default: "blue", values: ~w(blue alternative dark light green red yellow purple)
attr :size, :string, default: "default", values: ~w(xs sm default lg xl)
attr :rest, :global, include: ~w(disabled form name value)
slot :inner_block, required: true
def button(assigns) do
~H"""
<button
type={@type}
disabled={@loading}
class={[
FlowbitePhoenix.Config.get([:button, :base_classes]) || "focus:ring-4 focus:outline-none font-medium rounded-lg text-center inline-flex items-center",
button_variant_class(@variant, @color),
button_size_class(@size),
@loading && "opacity-50 cursor-not-allowed",
@class
]}
{@rest}
>
<%= if @loading do %>
<svg
aria-hidden="true"
role="status"
class="inline w-4 h-4 me-3 text-white animate-spin"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="#E5E7EB"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentColor"
/>
</svg>
Loading...
<% else %>
{render_slot(@inner_block)}
<% end %>
</button>
"""
end
# Default button variants (filled)
defp button_variant_class("default", "blue"), do: "text-white bg-blue-700 hover:bg-blue-800 focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
defp button_variant_class("default", "alternative"), do: "text-gray-900 bg-white border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:ring-gray-200 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700"
defp button_variant_class("default", "dark"), do: "text-white bg-gray-800 hover:bg-gray-900 focus:ring-gray-300 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700 dark:border-gray-700"
defp button_variant_class("default", "light"), do: "text-gray-900 bg-white border border-gray-300 hover:bg-gray-100 focus:ring-gray-200 dark:bg-gray-800 dark:text-white dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600 dark:focus:ring-gray-700"
defp button_variant_class("default", "green"), do: "text-white bg-green-700 hover:bg-green-800 focus:ring-green-300 dark:bg-green-600 dark:hover:bg-green-700 dark:focus:ring-green-800"
defp button_variant_class("default", "red"), do: "text-white bg-red-700 hover:bg-red-800 focus:ring-red-300 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900"
defp button_variant_class("default", "yellow"), do: "text-white bg-yellow-400 hover:bg-yellow-500 focus:ring-yellow-300 dark:focus:ring-yellow-900"
defp button_variant_class("default", "purple"), do: "text-white bg-purple-700 hover:bg-purple-800 focus:ring-purple-300 dark:bg-purple-600 dark:hover:bg-purple-700 dark:focus:ring-purple-900"
# Outline button variants
defp button_variant_class("outline", "blue"), do: "text-blue-700 hover:text-white border border-blue-700 hover:bg-blue-800 focus:ring-blue-300 dark:border-blue-500 dark:text-blue-500 dark:hover:text-white dark:hover:bg-blue-500 dark:focus:ring-blue-800"
defp button_variant_class("outline", "dark"), do: "text-gray-900 hover:text-white border border-gray-800 hover:bg-gray-900 focus:ring-gray-300 dark:border-gray-600 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-800"
defp button_variant_class("outline", "green"), do: "text-green-700 hover:text-white border border-green-700 hover:bg-green-800 focus:ring-green-300 dark:border-green-500 dark:text-green-500 dark:hover:text-white dark:hover:bg-green-600 dark:focus:ring-green-800"
defp button_variant_class("outline", "red"), do: "text-red-700 hover:text-white border border-red-700 hover:bg-red-800 focus:ring-red-300 dark:border-red-500 dark:text-red-500 dark:hover:text-white dark:hover:bg-red-600 dark:focus:ring-red-900"
defp button_variant_class("outline", "yellow"), do: "text-yellow-400 hover:text-white border border-yellow-400 hover:bg-yellow-500 focus:ring-yellow-300 dark:border-yellow-300 dark:text-yellow-300 dark:hover:text-white dark:hover:bg-yellow-400 dark:focus:ring-yellow-900"
defp button_variant_class("outline", "purple"), do: "text-purple-700 hover:text-white border border-purple-700 hover:bg-purple-800 focus:ring-purple-300 dark:border-purple-400 dark:text-purple-400 dark:hover:text-white dark:hover:bg-purple-500 dark:focus:ring-purple-900"
# Default fallback for outline variants not specified
defp button_variant_class("outline", _), do: "text-blue-700 hover:text-white border border-blue-700 hover:bg-blue-800 focus:ring-blue-300 dark:border-blue-500 dark:text-blue-500 dark:hover:text-white dark:hover:bg-blue-500 dark:focus:ring-blue-800"
defp button_size_class(size) do
FlowbitePhoenix.Config.get([:button, :sizes, String.to_atom(size)]) ||
case size do
"xs" -> "px-3 py-2 text-xs"
"sm" -> "px-3 py-2 text-sm"
"default" -> "px-5 py-2.5 text-sm"
"lg" -> "px-5 py-3 text-base"
"xl" -> "px-6 py-3.5 text-base"
end
end
@doc """
Renders an input with label and error messages using Flowbite styling.
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
This function accepts all HTML input types, considering that:
* You may also set `type="select"` to render a `<select>` tag
* `type="checkbox"` is used exclusively to render boolean values
* For live file uploads, see `Phoenix.Component.live_file_input/1`
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
for more information. Unsupported types, such as hidden and radio,
are best written directly in your templates.
## Examples
<.input field={@form[:email]} type="email" />
<.input name="my-input" errors={["oh no!"]} />
"""
attr :id, :any, default: nil
attr :name, :any
attr :label, :string, default: nil
attr :value, :any
attr :type, :string,
default: "text",
values: ~w(checkbox color date datetime-local email file hidden month number password
range search select tel text textarea 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)
def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
errors = field.errors
assigns
|> assign(field: nil, id: assigns.id || field.id)
|> assign(:errors, Enum.map(errors, &FlowbitePhoenix.translate_error(&1)))
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
|> assign_new(:value, fn -> field.value end)
|> input()
end
def input(%{type: "checkbox"} = assigns) do
assigns = assign_new(assigns, :checked, fn -> false end)
~H"""
<div class="mb-5">
<div class="flex items-center">
<input
type="checkbox"
id={@id}
name={@name}
value="true"
checked={@checked}
class={[
"w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600",
@errors != [] && "border-red-500 dark:border-red-500"
]}
{@rest}
/>
<label :if={@label} for={@id} class="ms-2 text-sm font-medium text-gray-900 dark:text-gray-300">
{@label}
</label>
</div>
<.error :for={msg <- @errors}>{msg}</.error>
</div>
"""
end
def input(%{type: "select"} = assigns) do
~H"""
<div class="mb-5">
<.label for={@id}>{@label}</.label>
<select
id={@id}
name={@name}
class={[
"bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500",
@errors != [] && "border-red-500 dark:border-red-500"
]}
multiple={@multiple}
{@rest}
>
<option :if={@prompt} value="">{@prompt}</option>
{Phoenix.HTML.Form.options_for_select(@options, @value)}
</select>
<.error :for={msg <- @errors}>{@label} {msg}</.error>
</div>
"""
end
def input(%{type: "textarea"} = assigns) do
~H"""
<div class="mb-5">
<.label for={@id}>{@label}</.label>
<textarea
id={@id}
name={@name}
class={[
"block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500",
@errors != [] && "border-red-500 dark:border-red-500"
]}
{@rest}
>{Phoenix.HTML.Form.normalize_value("textarea", @value)}</textarea>
<.error :for={msg <- @errors}>{@label} {msg}</.error>
</div>
"""
end
# All other inputs text, datetime-local, url, password, etc. are handled here...
def input(assigns) do
~H"""
<div class="mb-5">
<.label for={@id}>{@label}</.label>
<input
type={@type}
name={@name}
id={@id}
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
class={[
"bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500",
@errors != [] && "border-red-500 dark:border-red-500"
]}
{@rest}
/>
<.error :for={msg <- @errors}>{@label} {msg}</.error>
</div>
"""
end
@doc """
Renders a label with Flowbite styling.
"""
attr :for, :string, default: nil
slot :inner_block, required: true
def label(assigns) do
~H"""
<label for={@for} class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
{render_slot(@inner_block)}
</label>
"""
end
@doc """
Generates a generic error message with Flowbite styling.
"""
slot :inner_block, required: true
def error(assigns) do
~H"""
<p class="mt-2 text-sm text-red-600 dark:text-red-500">
<span class="font-medium">{render_slot(@inner_block)}</span>
</p>
"""
end
@doc """
Renders a toggle switch component using Flowbite styling.
## Examples
<.toggle name="notifications" label="Enable notifications" />
<.toggle name="dark_mode" label="Dark mode" checked={true} />
"""
attr :id, :string, default: nil
attr :name, :string, required: true
attr :label, :string, default: nil
attr :checked, :boolean, default: false
attr :class, :string, default: ""
attr :rest, :global
def toggle(assigns) do
~H"""
<label class={["inline-flex items-center cursor-pointer", @class]}>
<input
type="checkbox"
name={@name}
id={@id}
checked={@checked}
class="sr-only peer"
{@rest}
/>
<div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"></div>
<span :if={@label} class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">
{@label}
</span>
</label>
"""
end
end