Skip to content

Commit

Permalink
improvement: use prompt instead of adding a nil option for dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Nov 28, 2023
1 parent 081b690 commit eac54c3
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions lib/ash_admin/components/resource/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,8 @@ defmodule AshAdmin.Components.Resource.Form do
<%= Phoenix.HTML.Form.select(
@form,
@attribute.name,
add_nil_option([True: "true", False: "false"], @value, @attribute.allow_nil?),
[True: "true", False: "false"],
prompt: allow_nil_option(@attribute, @value),
selected: value(@value, @form, @attribute, "true"),
name: @name || @form.name <> "[#{@attribute.name}]"
) %>
Expand Down Expand Up @@ -719,9 +720,9 @@ defmodule AshAdmin.Components.Resource.Form do
<%= Phoenix.HTML.Form.select(
@form,
@attribute.name,
add_nil_option(Enum.map(@attribute.constraints[:one_of], &{to_name(&1), &1}), @value),
Enum.map(@attribute.constraints[:one_of], &{to_name(&1), &1}),
selected: value(@value, @form, @attribute, List.first(@attribute.constraints[:one_of])),
prompt: allow_nil_option(@attribute),
prompt: allow_nil_option(@attribute, @attribute),
name: @name || @form.name <> "[#{@attribute.name}]"
) %>
<% markdown?(@form.source.resource, @attribute) -> %>
Expand Down Expand Up @@ -875,9 +876,9 @@ defmodule AshAdmin.Components.Resource.Form do
<%= Phoenix.HTML.Form.select(
@form,
@attribute.name,
add_nil_option(Enum.map(@attribute.type.values(), &{to_name(&1), &1}), @value),
Enum.map(@attribute.type.values(), &{to_name(&1), &1}),
selected: value(@value, @form, @attribute, List.first(@attribute.type.values())),
prompt: allow_nil_option(@attribute),
prompt: allow_nil_option(@attribute, @value),
name: @name || @form.name <> "[#{@attribute.name}]"
) %>
<% true -> %>
Expand Down Expand Up @@ -991,18 +992,6 @@ defmodule AshAdmin.Components.Resource.Form do
end
end

defp add_nil_option(options, value, condition \\ true)

defp add_nil_option(options, {:list_value, _}, _condition) do
[{"-", nil} | options]
end

defp add_nil_option(options, _, condition) when condition == true do
[{"-", nil} | options]
end

defp add_nil_option(options, _, _), do: options

defp list_value(value) do
if is_map(value) do
value
Expand Down Expand Up @@ -1059,9 +1048,13 @@ defmodule AshAdmin.Components.Resource.Form do
end
end

defp allow_nil_option(%{allow_nil?: true}), do: "-"
defp allow_nil_option(%{default: default, allow_nil?: false}) when not is_nil(default), do: nil
defp allow_nil_option(_), do: "Select an option"
defp allow_nil_option(_, {:array, _}), do: "-"
defp allow_nil_option(%{allow_nil?: true}, _), do: "-"

defp allow_nil_option(%{default: default, allow_nil?: false}, _) when not is_nil(default),
do: nil

defp allow_nil_option(_, _), do: "Select an option"

defp can_append_embed?(changeset, attribute) do
case Ash.Changeset.get_attribute(changeset, attribute) do
Expand Down

0 comments on commit eac54c3

Please sign in to comment.