From b3807227b2f660c4c63acbf5b3fe8142a433a2c5 Mon Sep 17 00:00:00 2001 From: Hamish Murphy Date: Tue, 10 Dec 2024 16:44:15 +1100 Subject: [PATCH] Adjust tests for forms V1 --- assets/default.css | 4 +++ lib/petal_components/form.ex | 57 ++++++++++++++++++++++++++++-------- mix.lock | 2 +- test/petal/form_test.exs | 32 +++++--------------- 4 files changed, 57 insertions(+), 38 deletions(-) diff --git a/assets/default.css b/assets/default.css index f164669..fb117da 100644 --- a/assets/default.css +++ b/assets/default.css @@ -2521,6 +2521,10 @@ label.has-error { @apply !text-danger-900 dark:!text-danger-200; } +span.has-error { + @apply !text-danger-900 dark:!text-danger-200; +} + textarea.has-error, input.has-error:not(:checked), select.has-error { diff --git a/lib/petal_components/form.ex b/lib/petal_components/form.ex index e684062..58477bb 100644 --- a/lib/petal_components/form.ex +++ b/lib/petal_components/form.ex @@ -14,12 +14,15 @@ defmodule PetalComponents.Form do Deprecated in favor of field.ex and input.ex, which use the new `%Phoenix.HTML.FormField{}` struct. """ - attr(:form, :any, default: nil, doc: "") - attr(:field, :atom, default: nil, doc: "") - attr(:label, :string, default: nil, doc: "labels your field") - attr(:class, :any, doc: "CSS classes to add to your label") - slot(:inner_block, required: false) - attr(:rest, :global, include: ~w(for)) + attr :form, :any, default: nil, doc: "" + attr :field, :atom, default: nil, doc: "" + attr :label, :string, default: nil, doc: "labels your field" + attr :class, :any, doc: "CSS classes to add to your label" + + attr :compound, :boolean, default: false, doc: "Avoid using label/for for compound inputs" + + slot :inner_block, required: false + attr :rest, :global, include: ~w(for) def form_label(assigns) do assigns = @@ -27,7 +30,7 @@ defmodule PetalComponents.Form do |> assign(:classes, label_classes(assigns)) ~H""" - <%= if @form && @field do %> + <%= if @form && @field && !@compound do %> <%= Form.label @form, @field, [class: @classes] ++ Map.to_list(@rest) do %> {render_slot(@inner_block) || @label || Form.humanize(@field)} <% end %> @@ -136,10 +139,22 @@ defmodule PetalComponents.Form do <% "checkbox_group" -> %> - <.form_label form={@form} field={@field} label={@label} class={@label_class} /> + <.form_label + form={@form} + field={@field} + label={@label} + class={@label_class} + compound={true} + /> <.checkbox_group form={@form} field={@field} {@rest} /> <% "radio_group" -> %> - <.form_label form={@form} field={@field} label={@label} class={@label_class} /> + <.form_label + form={@form} + field={@field} + label={@label} + class={@label_class} + compound={true} + /> <.radio_group form={@form} field={@field} {@rest} /> <% "text_input" -> %> <.form_label form={@form} field={@field} label={@label} class={@label_class} /> @@ -166,16 +181,34 @@ defmodule PetalComponents.Form do <.form_label form={@form} field={@field} label={@label} class={@label_class} /> <.time_input form={@form} field={@field} {@rest} /> <% "time_select" -> %> - <.form_label form={@form} field={@field} label={@label} class={@label_class} /> + <.form_label + form={@form} + field={@field} + label={@label} + class={@label_class} + compound={true} + /> <.time_select form={@form} field={@field} {@rest} /> <% "datetime_select" -> %> - <.form_label form={@form} field={@field} label={@label} class={@label_class} /> + <.form_label + form={@form} + field={@field} + label={@label} + class={@label_class} + compound={true} + /> <.datetime_select form={@form} field={@field} {@rest} /> <% "datetime_local_input" -> %> <.form_label form={@form} field={@field} label={@label} class={@label_class} /> <.datetime_local_input form={@form} field={@field} {@rest} /> <% "date_select" -> %> - <.form_label form={@form} field={@field} label={@label} class={@label_class} /> + <.form_label + form={@form} + field={@field} + label={@label} + class={@label_class} + compound={true} + /> <.date_select form={@form} field={@field} {@rest} /> <% "date_input" -> %> <.form_label form={@form} field={@field} label={@label} class={@label_class} /> diff --git a/mix.lock b/mix.lock index 7e219fe..af28e6a 100644 --- a/mix.lock +++ b/mix.lock @@ -13,7 +13,7 @@ "file_system": {:hex, :file_system, "1.0.1", "79e8ceaddb0416f8b8cd02a0127bdbababe7bf4a23d2a395b983c1f8b3f73edd", [:mix], [], "hexpm", "4414d1f38863ddf9120720cd976fce5bdde8e91d8283353f0e31850fa89feb9e"}, "floki": {:hex, :floki, "0.36.3", "1102f93b16a55bc5383b85ae3ec470f82dee056eaeff9195e8afdf0ef2a43c30", [:mix], [], "hexpm", "fe0158bff509e407735f6d40b3ee0d7deb47f3f3ee7c6c182ad28599f9f6b27a"}, "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, - "heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "ad0ad1f6d51bd64dcd67e363d2b2833a8de25154", [tag: "v2.1.5", sparse: "optimized", depth: 1]}, + "heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "ad0ad1f6d51bd64dcd67e363d2b2833a8de25154", [tag: "v2.1.5", sparse: "optimized"]}, "hpax": {:hex, :hpax, "1.0.0", "28dcf54509fe2152a3d040e4e3df5b265dcb6cb532029ecbacf4ce52caea3fd2", [:mix], [], "hexpm", "7f1314731d711e2ca5fdc7fd361296593fc2542570b3105595bb0bc6d0fad601"}, "httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, diff --git a/test/petal/form_test.exs b/test/petal/form_test.exs index 08eb4ed..e1b7618 100644 --- a/test/petal/form_test.exs +++ b/test/petal/form_test.exs @@ -17,7 +17,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" refute html =~ " disabled " assert html =~ "pc-text-input" assert html =~ "!w-max" @@ -56,7 +55,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[description]" assert html =~ "itemid" assert html =~ "placeholder" - assert html =~ "phx-feedback-for" assert html =~ "dummy text" end @@ -75,7 +73,6 @@ defmodule PetalComponents.FormTest do assert html =~ "itemid" assert html =~ " ""} } } > @@ -231,7 +224,6 @@ defmodule PetalComponents.FormTest do """) assert html =~ "pc-form-field-error" - assert html =~ "phx-feedback-for" assert html =~ "blank" assert html =~ "too long" end @@ -284,7 +276,8 @@ defmodule PetalComponents.FormTest do errors: [ name: {"can't be blank", [validation: :required]}, name: {"too long", [validation: :required]} - ] + ], + params: %{"name" => ""} } } > @@ -305,7 +298,7 @@ defmodule PetalComponents.FormTest do assert html =~ "John" assert html =~ "too long" assert html =~ "blank" - assert html =~ "
" + assert html =~ "
" assert html =~ "Help!" end @@ -337,7 +330,8 @@ defmodule PetalComponents.FormTest do errors: [ name: {"can't be blank", [validation: :required]}, name: {"too long", [validation: :required]} - ] + ], + params: %{"name" => ""} } } > @@ -532,7 +526,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "email_input" do @@ -550,7 +543,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "password_input" do @@ -568,7 +560,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "search_input" do @@ -586,7 +577,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "telephone_input" do @@ -604,7 +594,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "url_input" do @@ -622,7 +611,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "time_input" do @@ -640,7 +628,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "time_select" do @@ -672,7 +659,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "datetime_select" do @@ -718,7 +704,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "color_input" do @@ -736,7 +721,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "file_input" do @@ -755,7 +739,6 @@ defmodule PetalComponents.FormTest do assert html =~ "itemid" assert html =~ "something" assert html =~ "pc-file-input" - assert html =~ "phx-feedback-for" end test "range_input" do @@ -773,7 +756,6 @@ defmodule PetalComponents.FormTest do assert html =~ "user[name]" assert html =~ "itemid" assert html =~ "something" - assert html =~ "phx-feedback-for" end test "hidden_input" do