2022-07-23 01:44:03 +02:00
|
|
|
defmodule ClaperWeb.Component.Input do
|
2023-09-09 18:10:11 +02:00
|
|
|
@moduledoc """
|
|
|
|
|
Input component for forms
|
|
|
|
|
"""
|
2022-07-23 01:44:03 +02:00
|
|
|
use ClaperWeb, :view_component
|
|
|
|
|
|
|
|
|
|
def text(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
|
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:readonly, fn -> false end)
|
|
|
|
|
|> assign_new(:labelClass, fn -> "text-gray-700" end)
|
|
|
|
|
|> assign_new(:fieldClass, fn -> "bg-white" end)
|
2023-02-26 22:40:34 +01:00
|
|
|
|> assign_new(:value, fn -> input_value(assigns.form, assigns.key) end)
|
2022-07-23 01:44:03 +02:00
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="relative">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium #{@labelClass}") %>
|
2022-07-23 01:44:03 +02:00
|
|
|
<div class="mt-1">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= text_input(@form, @key,
|
|
|
|
|
required: @required,
|
|
|
|
|
readonly: @readonly,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
placeholder: @placeholder,
|
|
|
|
|
autocomplete: @key,
|
|
|
|
|
value: @value,
|
|
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"#{@fieldClass} read-only:opacity-50 outline-none shadow-base focus:ring-primary-500 focus:border-primary-500 focus:ring-2 block w-full text-lg border-gray-300 rounded-md py-2 px-3"
|
2023-11-23 16:36:39 +05:30
|
|
|
) %>
|
|
|
|
|
</div>
|
|
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
|
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
|
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def textarea(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
|
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:readonly, fn -> false end)
|
|
|
|
|
|> assign_new(:labelClass, fn -> "text-gray-700" end)
|
|
|
|
|
|> assign_new(:fieldClass, fn -> "bg-white" end)
|
|
|
|
|
|> assign_new(:value, fn -> input_value(assigns.form, assigns.key) end)
|
|
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="relative">
|
|
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium #{@labelClass}") %>
|
|
|
|
|
<div class="mt-1">
|
|
|
|
|
<%= text_input(@form, @key,
|
|
|
|
|
required: @required,
|
|
|
|
|
readonly: @readonly,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
placeholder: @placeholder,
|
|
|
|
|
autocomplete: @key,
|
|
|
|
|
value: @value,
|
|
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"#{@fieldClass} read-only:opacity-50 outline-none shadow-base focus:ring-primary-500 focus:border-primary-500 focus:ring-2 block w-full text-lg border-gray-300 rounded-md py-2 px-3"
|
2023-04-20 22:10:33 +02:00
|
|
|
) %>
|
2023-02-26 22:40:34 +01:00
|
|
|
</div>
|
|
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
2023-02-26 22:40:34 +01:00
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def select(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
|
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:labelClass, fn -> "text-gray-700" end)
|
|
|
|
|
|> assign_new(:fieldClass, fn -> "bg-white" end)
|
|
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="relative">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium #{@labelClass}") %>
|
2023-02-26 22:40:34 +01:00
|
|
|
<div class="mt-1">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= select(@form, @key, @array,
|
|
|
|
|
required: @required,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
placeholder: @placeholder,
|
|
|
|
|
autocomplete: @key,
|
|
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"#{@fieldClass} outline-none shadow-base focus:ring-primary-500 focus:border-primary-500 block w-full text-lg border-gray-300 rounded-md py-2 px-3"
|
2023-04-20 22:10:33 +02:00
|
|
|
) %>
|
2022-07-23 01:44:03 +02:00
|
|
|
</div>
|
|
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
2022-07-23 01:44:03 +02:00
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:disabled, fn -> false end)
|
2023-10-29 14:32:05 +05:30
|
|
|
|> assign_new(:shortcut, fn -> nil end)
|
2022-07-23 01:44:03 +02:00
|
|
|
|
|
|
|
|
~H"""
|
2023-04-20 22:10:33 +02:00
|
|
|
<button
|
|
|
|
|
phx-click={checked(@checked, @key)}
|
|
|
|
|
disabled={@disabled}
|
|
|
|
|
phx-value-key={@key}
|
|
|
|
|
type="button"
|
2024-04-06 11:48:47 +02:00
|
|
|
class="group relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer items-center justify-center rounded-full"
|
2023-04-20 22:10:33 +02:00
|
|
|
role="switch"
|
|
|
|
|
aria-checked="false"
|
2023-10-29 14:32:05 +05:30
|
|
|
phx-key={@shortcut}
|
|
|
|
|
phx-window-keydown={if @shortcut && not @disabled, do: checked(@checked, @key)}
|
2023-04-20 22:10:33 +02:00
|
|
|
>
|
2024-04-06 11:48:47 +02:00
|
|
|
<span class="pointer-events-none absolute h-full w-full rounded-md bg-white" aria-hidden="true">
|
|
|
|
|
</span>
|
|
|
|
|
<span
|
|
|
|
|
aria-hidden="true"
|
2024-08-11 11:16:34 +02:00
|
|
|
class={"#{if @checked, do: "bg-primary-500", else: "bg-gray-200"} pointer-events-none absolute mx-auto h-4 w-9 rounded-full transition-colors duration-200 ease-in-out"}
|
2024-04-06 11:48:47 +02:00
|
|
|
>
|
|
|
|
|
</span>
|
|
|
|
|
<span
|
2024-08-11 11:16:34 +02:00
|
|
|
class={"#{if @checked, do: "translate-x-5", else: "translate-x-0"} pointer-events-none absolute left-0 inline-block h-5 w-5 transform rounded-full border border-gray-200 bg-white shadow ring-0 transition-transform duration-200 ease-in-out"}
|
2024-04-06 11:48:47 +02:00
|
|
|
aria-hidden="true"
|
|
|
|
|
>
|
2022-07-23 01:44:03 +02:00
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def checked(is_checked, key, js \\ %JS{})
|
|
|
|
|
|
|
|
|
|
def checked(false, key, js) do
|
|
|
|
|
js
|
|
|
|
|
|> JS.push("checked", value: %{key: key, value: true})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def checked(true, key, js) do
|
|
|
|
|
js
|
|
|
|
|
|> JS.remove_class("translate-x-6",
|
|
|
|
|
to: "#check-#{key} > span"
|
|
|
|
|
)
|
|
|
|
|
|> JS.add_class("translate-x-0",
|
|
|
|
|
to: "#check-#{key} > span"
|
|
|
|
|
)
|
|
|
|
|
|> JS.remove_class("opacity-0 ease-out duration-100",
|
|
|
|
|
to: "#check-#{key} > span > span"
|
|
|
|
|
)
|
|
|
|
|
|> JS.add_class("opacity-100 ease-in duration-200",
|
|
|
|
|
to: "#check-#{key} > span > span"
|
|
|
|
|
)
|
|
|
|
|
|> JS.remove_class("opacity-100 ease-in duration-200",
|
|
|
|
|
to: "#check-#{key} > span > span:nth-child(2)"
|
|
|
|
|
)
|
|
|
|
|
|> JS.add_class("opacity-0 ease-out duration-100",
|
|
|
|
|
to: "#check-#{key} > span > span:nth-child(2)"
|
|
|
|
|
)
|
|
|
|
|
|> JS.push("checked", value: %{key: key, value: false})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def code(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
|
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:readonly, fn -> false end)
|
|
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="relative">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium text-gray-700") %>
|
2022-07-23 01:44:03 +02:00
|
|
|
<div class="mt-1 relative">
|
2023-04-20 22:10:33 +02:00
|
|
|
<img
|
2024-08-11 11:16:34 +02:00
|
|
|
class="icon absolute transition-all top-2.5 left-2 duration-100 h-6"
|
2023-04-20 22:10:33 +02:00
|
|
|
src="/images/icons/hashtag.svg"
|
|
|
|
|
alt="code"
|
|
|
|
|
/>
|
|
|
|
|
<%= text_input(@form, @key,
|
|
|
|
|
required: @required,
|
|
|
|
|
readonly: @readonly,
|
|
|
|
|
placeholder: @placeholder,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
autocomplete: @key,
|
|
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"read-only:opacity-50 outline-none shadow-base focus:ring-primary-500 focus:border-primary-500 block w-full text-lg border-gray-300 rounded-md py-2 pr-3 pl-9 uppercase"
|
2023-04-20 22:10:33 +02:00
|
|
|
) %>
|
2022-07-23 01:44:03 +02:00
|
|
|
</div>
|
|
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
2022-07-23 01:44:03 +02:00
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def date(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
|
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:readonly, fn -> false end)
|
|
|
|
|
|
|
|
|
|
~H"""
|
2024-04-06 11:48:47 +02:00
|
|
|
<div>
|
|
|
|
|
<div class="relative" id="date" phx-hook="Pickr">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium text-gray-700") %>
|
2022-07-23 01:44:03 +02:00
|
|
|
<div class="mt-1 relative">
|
2024-04-06 11:48:47 +02:00
|
|
|
<%= hidden_input(@form, @key) %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= text_input(@form, :local_date,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
placeholder: @placeholder,
|
2024-04-06 11:48:47 +02:00
|
|
|
autocomplete: false,
|
2023-04-20 22:10:33 +02:00
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"outline-none shadow-base focus:ring-primary-500 focus:border-primary-500 block w-full text-lg border-gray-300 rounded-md py-2 px-3 read-only:opacity-50"
|
2023-04-20 22:10:33 +02:00
|
|
|
) %>
|
2022-07-23 01:44:03 +02:00
|
|
|
</div>
|
2024-04-06 11:48:47 +02:00
|
|
|
|
2022-07-23 01:44:03 +02:00
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
2022-07-23 01:44:03 +02:00
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def email(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
2023-02-26 22:40:34 +01:00
|
|
|
|> assign_new(:readonly, fn -> false end)
|
2022-07-23 01:44:03 +02:00
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:labelClass, fn -> "text-gray-700" end)
|
|
|
|
|
|> assign_new(:fieldClass, fn -> "bg-white" end)
|
2023-02-26 22:40:34 +01:00
|
|
|
|> assign_new(:value, fn -> input_value(assigns.form, assigns.key) end)
|
2022-07-23 01:44:03 +02:00
|
|
|
|
|
|
|
|
~H"""
|
2023-02-26 22:40:34 +01:00
|
|
|
<div class="relative" x-data={"{input: '#{assigns.value}'}"}>
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium #{@labelClass}") %>
|
2022-07-23 01:44:03 +02:00
|
|
|
<div class="mt-1">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= email_input(@form, @key,
|
|
|
|
|
required: @required,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
placeholder: @placeholder,
|
|
|
|
|
readonly: @readonly,
|
|
|
|
|
autocomplete: @key,
|
|
|
|
|
value: @value,
|
|
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"#{@fieldClass} read-only:opacity-50 shadow-base block w-full text-lg focus:ring-primary-500 focus:ring-2 outline-none rounded-md py-2 px-3",
|
2023-04-20 22:10:33 +02:00
|
|
|
"x-model": "input",
|
|
|
|
|
"x-ref": "input"
|
|
|
|
|
) %>
|
2022-07-23 01:44:03 +02:00
|
|
|
</div>
|
|
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
2022-07-23 01:44:03 +02:00
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def password(assigns) do
|
|
|
|
|
assigns =
|
|
|
|
|
assigns
|
|
|
|
|
|> assign_new(:required, fn -> false end)
|
|
|
|
|
|> assign_new(:autofocus, fn -> false end)
|
2022-08-22 23:40:38 +02:00
|
|
|
|> assign_new(:placeholder, fn -> false end)
|
|
|
|
|
|> assign_new(:labelClass, fn -> "text-gray-700" end)
|
|
|
|
|
|> assign_new(:fieldClass, fn -> "bg-white" end)
|
2023-04-20 17:15:31 +02:00
|
|
|
|> assign_new(:value, fn -> Map.get(assigns.form.data, assigns.key, "") end)
|
2022-07-23 01:44:03 +02:00
|
|
|
|
|
|
|
|
~H"""
|
2023-04-20 17:15:31 +02:00
|
|
|
<div class="relative" x-data={"{input: '#{assigns.value}'}"}>
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= label(@form, @key, @name, class: "block text-sm font-medium #{@labelClass}") %>
|
2022-08-22 23:40:38 +02:00
|
|
|
<div class="mt-1">
|
2023-04-20 22:10:33 +02:00
|
|
|
<%= password_input(@form, @key,
|
|
|
|
|
required: @required,
|
|
|
|
|
autofocus: @autofocus,
|
|
|
|
|
placeholder: @placeholder,
|
|
|
|
|
class:
|
2024-08-11 11:16:34 +02:00
|
|
|
"#{@fieldClass} shadow-base block w-full text-lg focus:ring-primary-500 focus:ring-2 outline-none rounded-md py-2 px-3",
|
2023-04-20 22:10:33 +02:00
|
|
|
"x-model": "input",
|
|
|
|
|
"x-ref": "input"
|
|
|
|
|
) %>
|
2022-08-22 23:40:38 +02:00
|
|
|
</div>
|
2022-07-23 01:44:03 +02:00
|
|
|
<%= if Keyword.has_key?(@form.errors, @key) do %>
|
2023-04-20 22:10:33 +02:00
|
|
|
<p class="text-supporting-red-500 text-sm"><%= error_tag(@form, @key) %></p>
|
2022-07-23 01:44:03 +02:00
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
end
|