Skip to content

Commit

Permalink
Replace phx-feedback-for in favor of used_input?/1 (#5713)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed May 3, 2024
1 parent e12f4da commit ed19338
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 39 deletions.
2 changes: 1 addition & 1 deletion guides/howto/file_uploads.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Once you have a multipart form, you need a `file` input. Here's how you would do
When rendered, here is the HTML for the default `HelloWeb.CoreComponents` `input/1` component:

```html
<div phx-feedback-for="product[photo]">
<div>
<label for="product_photo" class="block text-sm...">Photo</label>
<input type="file" name="product[photo]" id="product_photo" class="mt-2 block w-full...">
</div>
Expand Down
1 change: 0 additions & 1 deletion installer/templates/phx_assets/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = {
//
// <div class="phx-click-loading:animate-ping">
//
plugin(({addVariant}) => addVariant("phx-no-feedback", [".phx-no-feedback&", ".phx-no-feedback &"])),
plugin(({addVariant}) => addVariant("phx-click-loading", [".phx-click-loading&", ".phx-click-loading &"])),
plugin(({addVariant}) => addVariant("phx-submit-loading", [".phx-submit-loading&", ".phx-submit-loading &"])),
plugin(({addVariant}) => addVariant("phx-change-loading", [".phx-change-loading&", ".phx-change-loading &"])),
Expand Down
4 changes: 2 additions & 2 deletions installer/templates/phx_single/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ defmodule <%= @app_module %>.MixProject do
defp deps do
[
<%= @phoenix_dep %>,<%= if @ecto do %>
{:phoenix_ecto, "~> 4.4"},
{:phoenix_ecto, "~> 4.5"},
{:ecto_sql, "~> 3.10"},
{<%= inspect @adapter_app %>, ">= 0.0.0"},<% end %><%= if @html do %>
{:phoenix_html, "~> 4.0"},
{:phoenix_html, "~> 4.1"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.20.2"},
{:floki, ">= 0.30.0", only: :test},<% end %><%= if @dashboard do %>
Expand Down
4 changes: 2 additions & 2 deletions installer/templates/phx_umbrella/apps/app_name_web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ defmodule <%= @web_namespace %>.MixProject do
defp deps do
[
<%= @phoenix_dep %>,<%= if @ecto do %>
{:phoenix_ecto, "~> 4.4"},<% end %><%= if @html do %>
{:phoenix_html, "~> 4.0"},
{:phoenix_ecto, "~> 4.5"},<% end %><%= if @html do %>
{:phoenix_html, "~> 4.1"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.20.2"},
{:floki, ">= 0.30.0", only: :test},<% end %><%= if @dashboard do %>
Expand Down
18 changes: 9 additions & 9 deletions installer/templates/phx_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ defmodule <%= @web_namespace %>.CoreComponents do
slot :inner_block

def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []

assigns
|> assign(field: nil, id: assigns.id || field.id)
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign(:errors, Enum.map(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)
|> input()
Expand All @@ -309,7 +311,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
end)

~H"""
<div phx-feedback-for={@name}>
<div>
<label class="flex items-center gap-4 text-sm leading-6 text-zinc-600">
<input type="hidden" name={@name} value="false" />
<input
Expand All @@ -330,7 +332,7 @@ defmodule <%= @web_namespace %>.CoreComponents do

def input(%{type: "select"} = assigns) do
~H"""
<div phx-feedback-for={@name}>
<div>
<.label for={@id}><%%= @label %></.label>
<select
id={@id}
Expand All @@ -349,14 +351,13 @@ defmodule <%= @web_namespace %>.CoreComponents do

def input(%{type: "textarea"} = assigns) do
~H"""
<div phx-feedback-for={@name}>
<div>
<.label for={@id}><%%= @label %></.label>
<textarea
id={@id}
name={@name}
class={[
"mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6",
"min-h-[6rem] phx-no-feedback:border-zinc-300 phx-no-feedback:focus:border-zinc-400",
"mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 min-h-[6rem]",
@errors == [] && "border-zinc-300 focus:border-zinc-400",
@errors != [] && "border-rose-400 focus:border-rose-400"
]}
Expand All @@ -370,7 +371,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
# All other inputs text, datetime-local, url, password, etc. are handled here...
def input(assigns) do
~H"""
<div phx-feedback-for={@name}>
<div>
<.label for={@id}><%%= @label %></.label>
<input
type={@type}
Expand All @@ -379,7 +380,6 @@ defmodule <%= @web_namespace %>.CoreComponents do
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
class={[
"mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6",
"phx-no-feedback:border-zinc-300 phx-no-feedback:focus:border-zinc-400",
@errors == [] && "border-zinc-300 focus:border-zinc-400",
@errors != [] && "border-rose-400 focus:border-rose-400"
]}
Expand Down Expand Up @@ -411,7 +411,7 @@ defmodule <%= @web_namespace %>.CoreComponents do

def error(assigns) do
~H"""
<p class="mt-3 flex gap-3 text-sm leading-6 text-rose-600 phx-no-feedback:hidden">
<p class="mt-3 flex gap-3 text-sm leading-6 text-rose-600">
<.icon name="hero-exclamation-circle-mini" class="mt-0.5 h-5 w-5 flex-none" />
<%%= render_slot(@inner_block) %>
</p>
Expand Down
18 changes: 9 additions & 9 deletions priv/templates/phx.gen.live/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ defmodule <%= @web_namespace %>.CoreComponents do
slot :inner_block

def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []

assigns
|> assign(field: nil, id: assigns.id || field.id)
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign(:errors, Enum.map(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)
|> input()
Expand All @@ -309,7 +311,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
end)

~H"""
<div phx-feedback-for={@name}>
<div>
<label class="flex items-center gap-4 text-sm leading-6 text-zinc-600">
<input type="hidden" name={@name} value="false" />
<input
Expand All @@ -330,7 +332,7 @@ defmodule <%= @web_namespace %>.CoreComponents do

def input(%{type: "select"} = assigns) do
~H"""
<div phx-feedback-for={@name}>
<div>
<.label for={@id}><%%= @label %></.label>
<select
id={@id}
Expand All @@ -349,14 +351,13 @@ defmodule <%= @web_namespace %>.CoreComponents do

def input(%{type: "textarea"} = assigns) do
~H"""
<div phx-feedback-for={@name}>
<div>
<.label for={@id}><%%= @label %></.label>
<textarea
id={@id}
name={@name}
class={[
"mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6",
"min-h-[6rem] phx-no-feedback:border-zinc-300 phx-no-feedback:focus:border-zinc-400",
"mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 min-h-[6rem]",
@errors == [] && "border-zinc-300 focus:border-zinc-400",
@errors != [] && "border-rose-400 focus:border-rose-400"
]}
Expand All @@ -370,7 +371,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
# All other inputs text, datetime-local, url, password, etc. are handled here...
def input(assigns) do
~H"""
<div phx-feedback-for={@name}>
<div>
<.label for={@id}><%%= @label %></.label>
<input
type={@type}
Expand All @@ -379,7 +380,6 @@ defmodule <%= @web_namespace %>.CoreComponents do
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
class={[
"mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6",
"phx-no-feedback:border-zinc-300 phx-no-feedback:focus:border-zinc-400",
@errors == [] && "border-zinc-300 focus:border-zinc-400",
@errors != [] && "border-rose-400 focus:border-rose-400"
]}
Expand Down Expand Up @@ -411,7 +411,7 @@ defmodule <%= @web_namespace %>.CoreComponents do

def error(assigns) do
~H"""
<p class="mt-3 flex gap-3 text-sm leading-6 text-rose-600 phx-no-feedback:hidden">
<p class="mt-3 flex gap-3 text-sm leading-6 text-rose-600">
<.icon name="hero-exclamation-circle-mini" class="mt-0.5 h-5 w-5 flex-none" />
<%%= render_slot(@inner_block) %>
</p>
Expand Down
22 changes: 7 additions & 15 deletions priv/templates/phx.gen.live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,18 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web

@impl true
def update(%{<%= schema.singular %>: <%= schema.singular %>} = assigns, socket) do
changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>)

{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
|> assign_new(:form, fn ->
to_form(<%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>))
end)}
end

@impl true
def handle_event("validate", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do
changeset =
socket.assigns.<%= schema.singular %>
|> <%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>_params)
|> Map.put(:action, :validate)

{:noreply, assign_form(socket, changeset)}
changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(socket.assigns.<%= schema.singular %>, <%= schema.singular %>_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end

def handle_event("save", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do
Expand All @@ -63,7 +59,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> push_patch(to: socket.assigns.patch)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
{:noreply, assign(socket, form: to_form(changeset))}
end
end

Expand All @@ -78,13 +74,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> push_patch(to: socket.assigns.patch)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
{:noreply, assign(socket, form: to_form(changeset))}
end
end

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end

defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end

0 comments on commit ed19338

Please sign in to comment.