defmodule Uikit.FormComponents do
@moduledoc """
Provides UIkit form components for Phoenix LiveView applications.
These components integrate with `Phoenix.HTML.FormField` for seamless use
with `<.form>` and `to_form/2`-based forms, including error display and
field name/id generation.
## Setup
Run `mix uikit.setup` to automatically configure your project and import this module.
See the [installation guide](https://hexdocs.pm/elixir_uikit) for details.
## Basic Example
<.uk_form for={@form} id="user-form" phx-change="validate" phx-submit="save">
<.uk_fieldset>
<:legend>User Details
<.uk_input field={@form[:name]}>
<:label>Full Name
<.uk_input field={@form[:email]} type="email" margin>
<:label>Email *
<.uk_input field={@form[:role]} type="select" options={["Admin", "User"]}>
<:label>Role
<.uk_input field={@form[:bio]} type="textarea">
<:label>Bio
"""
use Phoenix.Component, global_prefixes: ~w(uk-)
@doc """
Renders a UIkit-styled form wrapper.
Wraps `<.form>` with optional UIkit layout classes.
## Examples
<.uk_form for={@form} id="my-form" phx-submit="save">
...
<.uk_form for={@form} id="my-form" layout="stacked" phx-submit="save">
...
"""
attr :for, :any, required: true, doc: "the form data source (result of to_form/2)"
attr :id, :string, required: true, doc: "the DOM ID of the form (required for LiveView)"
attr :layout, :string,
default: nil,
values: [nil, "stacked", "horizontal"],
doc: "the UIkit form layout — stacked places labels above, horizontal places them beside"
attr :class, :any, default: nil, doc: "additional CSS classes"
attr :rest, :global,
include: ~w(action method phx-change phx-submit phx-trigger-action autocomplete),
doc: "arbitrary HTML attributes for the form element"
slot :inner_block, required: true, doc: "the form fields"
def uk_form(assigns) do
~H"""
<.form
for={@for}
id={@id}
class={[
@layout && "uk-form-#{@layout}",
@class
]}
{@rest}
>
{render_slot(@inner_block)}
"""
end
@doc """
Renders a UIkit input with label and error messages.
Accepts a `Phoenix.HTML.FormField` via the `field` attribute for automatic
`id`, `name`, `value`, and error extraction. All attributes can also be
passed explicitly when not using a form field.
## Types
Supports all standard HTML input types. Use `type="select"` to render a
`