Skip to content

Commit

Permalink
Adjust tests for forms V1
Browse files Browse the repository at this point in the history
  • Loading branch information
mitkins committed Dec 10, 2024
1 parent 97e6119 commit b380722
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
4 changes: 4 additions & 0 deletions assets/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
57 changes: 45 additions & 12 deletions lib/petal_components/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ 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 =
assigns
|> 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 %>
Expand Down Expand Up @@ -136,10 +139,22 @@ defmodule PetalComponents.Form do
</div>
</label>
<% "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} />
Expand All @@ -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} />
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
32 changes: 7 additions & 25 deletions test/petal/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -75,7 +73,6 @@ defmodule PetalComponents.FormTest do
assert html =~ "itemid"
assert html =~ "<option"
assert html =~ "admin"
assert html =~ "phx-feedback-for"
assert html =~ "Admin"
end

Expand All @@ -91,7 +88,6 @@ defmodule PetalComponents.FormTest do

assert html =~ "checkbox"
assert html =~ "user[read_terms]"
assert html =~ "phx-feedback-for"
assert html =~ "itemid"
end

Expand All @@ -111,7 +107,6 @@ defmodule PetalComponents.FormTest do
assert html =~ "user_roles_write"
assert html =~ "user[roles][]"
assert html =~ "Read"
assert html =~ "phx-feedback-for"
assert html =~ "Write"
refute html =~ "checked"

Expand Down Expand Up @@ -143,7 +138,6 @@ defmodule PetalComponents.FormTest do

assert html =~ "checkbox"
assert html =~ "user[read_terms]"
assert html =~ "phx-feedback-for"
assert html =~ "itemid"
assert html =~ "sr-only"
assert html =~ "peer"
Expand All @@ -163,7 +157,6 @@ defmodule PetalComponents.FormTest do
assert html =~ "user[eye_color]"
assert html =~ "green"
assert html =~ "itemid"
assert html =~ "phx-feedback-for"
end

test "form_label" do
Expand All @@ -178,7 +171,6 @@ defmodule PetalComponents.FormTest do

assert html =~ "label"
assert html =~ "Name"
assert html =~ "phx-feedback-for"
assert html =~ "text-pink-500"

html =
Expand Down Expand Up @@ -222,7 +214,8 @@ defmodule PetalComponents.FormTest do
errors: [
name: {"can't be blank", [validation: :required]},
name: {"too long", [validation: :required]}
]
],
params: %{"name" => ""}
}
}
>
Expand All @@ -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
Expand Down Expand Up @@ -284,7 +276,8 @@ defmodule PetalComponents.FormTest do
errors: [
name: {"can't be blank", [validation: :required]},
name: {"too long", [validation: :required]}
]
],
params: %{"name" => ""}
}
}
>
Expand All @@ -305,7 +298,7 @@ defmodule PetalComponents.FormTest do
assert html =~ "John"
assert html =~ "too long"
assert html =~ "blank"
assert html =~ "<div class=\"wrapper-test\" phx-feedback-for=\"user[name]\">"
assert html =~ "<div class=\"wrapper-test\">"
assert html =~ "Help!"
end

Expand Down Expand Up @@ -337,7 +330,8 @@ defmodule PetalComponents.FormTest do
errors: [
name: {"can't be blank", [validation: :required]},
name: {"too long", [validation: :required]}
]
],
params: %{"name" => ""}
}
}
>
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b380722

Please sign in to comment.