mirror of
https://github.com/ash-project/ash_admin.git
synced 2024-09-17 03:43:26 +12:00
improvement: various fixes around unions
This commit is contained in:
parent
9a02dc4631
commit
89bee3a1f2
3 changed files with 79 additions and 7 deletions
1
dev.exs
1
dev.exs
|
@ -46,6 +46,7 @@ end
|
||||||
|
|
||||||
Application.ensure_all_started(:os_mon)
|
Application.ensure_all_started(:os_mon)
|
||||||
Application.put_env(:phoenix, :serve_endpoints, true)
|
Application.put_env(:phoenix, :serve_endpoints, true)
|
||||||
|
:erlang.system_flag(:backtrace_depth, 100)
|
||||||
|
|
||||||
Task.start(fn ->
|
Task.start(fn ->
|
||||||
children = [
|
children = [
|
||||||
|
|
|
@ -1045,17 +1045,75 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
) %>
|
) %>
|
||||||
</.inputs_for>
|
</.inputs_for>
|
||||||
<button
|
<button
|
||||||
:if={can_append_embed?(@form.source, @attribute.name)}
|
:if={can_append_embed?(@form.source, @attribute.name, @attribute.type)}
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="add_form"
|
phx-click="add_form"
|
||||||
phx-target={@myself}
|
phx-target={@myself}
|
||||||
phx-value-pkey={embedded_type_pkey(@attribute.type)}
|
phx-value-pkey={embedded_type_pkey(@attribute.type)}
|
||||||
phx-value-union-type={@union_type}
|
phx-value-union-type={@union_type}
|
||||||
phx-value-path={@name || @form.name <> "[#{@attribute.name}]"}
|
phx-value-path={@form.name <> "[#{@attribute.name}]"}
|
||||||
class="flex h-6 w-6 mt-2 border-gray-600 hover:bg-gray-400 rounded-md justify-center items-center"
|
class="flex h-6 w-6 mt-2 border-gray-600 hover:bg-gray-400 rounded-md justify-center items-center"
|
||||||
>
|
>
|
||||||
<.icon name="hero-plus" class="h-4 w-4 text-gray-500" />
|
<.icon name="hero-plus" class="h-4 w-4 text-gray-500" />
|
||||||
</button>
|
</button>
|
||||||
|
<% Ash.Type.embedded_type?(@attribute.type) && match?([%AshPhoenix.Form{} | _], @value) -> %>
|
||||||
|
<%= for inner_form <- Enum.map(@value, &to_form/1) do %>
|
||||||
|
<.input
|
||||||
|
:for={kv <- inner_form.hidden}
|
||||||
|
name={inner_form.name <> "[#{elem(kv, 0)}]"}
|
||||||
|
value={elem(kv, 1)}
|
||||||
|
type="hidden"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
phx-click="remove_form"
|
||||||
|
phx-target={@myself}
|
||||||
|
phx-value-path={inner_form.name}
|
||||||
|
class="flex h-6 w-6 mt-2 border-gray-600 hover:bg-gray-400 rounded-md justify-center items-center"
|
||||||
|
>
|
||||||
|
<.icon name="hero-minus" class="h-4 w-4 text-gray-500" />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<%= render_attributes(
|
||||||
|
assigns,
|
||||||
|
inner_form.source.resource,
|
||||||
|
inner_form.source.source.action,
|
||||||
|
%{
|
||||||
|
inner_form
|
||||||
|
| id: @id || @form.id <> "_#{@attribute.name}",
|
||||||
|
name: @name || @form.name <> "[#{@attribute.name}]"
|
||||||
|
}
|
||||||
|
) %>
|
||||||
|
<% end %>
|
||||||
|
<button
|
||||||
|
:if={can_append_embed?(@form.source, @attribute.name, @attribute.type)}
|
||||||
|
type="button"
|
||||||
|
phx-click="add_form"
|
||||||
|
phx-target={@myself}
|
||||||
|
phx-value-union-type={@union_type}
|
||||||
|
phx-value-pkey={embedded_type_pkey(@attribute.type)}
|
||||||
|
phx-value-path={@form.name <> "[#{@attribute.name}]"}
|
||||||
|
class="flex h-6 w-6 mt-2 border-gray-600 hover:bg-gray-400 rounded-md justify-center items-center"
|
||||||
|
>
|
||||||
|
<.icon name="hero-plus" class="h-4 w-4 text-gray-500" />
|
||||||
|
</button>
|
||||||
|
<% Ash.Type.embedded_type?(@attribute.type) && match?(%AshPhoenix.Form{}, @value) -> %>
|
||||||
|
<% inner_form = to_form(@value) %>
|
||||||
|
<.input
|
||||||
|
:for={kv <- inner_form.hidden}
|
||||||
|
name={inner_form.name <> "[#{elem(kv, 0)}]"}
|
||||||
|
value={elem(kv, 1)}
|
||||||
|
type="hidden"
|
||||||
|
/>
|
||||||
|
<%= render_attributes(
|
||||||
|
assigns,
|
||||||
|
inner_form.source.resource,
|
||||||
|
inner_form.source.source.action,
|
||||||
|
%{ inner_form |
|
||||||
|
id: @id || @form.id <> "_#{@attribute.name}",
|
||||||
|
name: @name || @form.name <> "[#{@attribute.name}]"
|
||||||
|
}
|
||||||
|
) %>
|
||||||
<% Ash.Type.embedded_type?(@attribute.type) -> %>
|
<% Ash.Type.embedded_type?(@attribute.type) -> %>
|
||||||
<.inputs_for :let={inner_form} field={@form[@attribute.name]}>
|
<.inputs_for :let={inner_form} field={@form[@attribute.name]}>
|
||||||
<.input
|
<.input
|
||||||
|
@ -1086,13 +1144,13 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
) %>
|
) %>
|
||||||
</.inputs_for>
|
</.inputs_for>
|
||||||
<button
|
<button
|
||||||
:if={can_append_embed?(@form.source, @attribute.name)}
|
:if={can_append_embed?(@form.source, @attribute.name, @attribute.type)}
|
||||||
type="button"
|
type="button"
|
||||||
phx-click="add_form"
|
phx-click="add_form"
|
||||||
phx-target={@myself}
|
phx-target={@myself}
|
||||||
phx-value-union-type={@union_type}
|
phx-value-union-type={@union_type}
|
||||||
phx-value-pkey={embedded_type_pkey(@attribute.type)}
|
phx-value-pkey={embedded_type_pkey(@attribute.type)}
|
||||||
phx-value-path={@name || @form.name <> "[#{@attribute.name}]"}
|
phx-value-path={@form.name <> "[#{@attribute.name}]"}
|
||||||
class="flex h-6 w-6 mt-2 border-gray-600 hover:bg-gray-400 rounded-md justify-center items-center"
|
class="flex h-6 w-6 mt-2 border-gray-600 hover:bg-gray-400 rounded-md justify-center items-center"
|
||||||
>
|
>
|
||||||
<.icon name="hero-plus" class="h-4 w-4 text-gray-500" />
|
<.icon name="hero-plus" class="h-4 w-4 text-gray-500" />
|
||||||
|
@ -1138,6 +1196,7 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
id,
|
id,
|
||||||
union_type
|
union_type
|
||||||
) do
|
) do
|
||||||
|
|
||||||
name = name || form.name <> "[#{attribute.name}]"
|
name = name || form.name <> "[#{attribute.name}]"
|
||||||
id = id || form.id <> "_#{attribute.name}"
|
id = id || form.id <> "_#{attribute.name}"
|
||||||
|
|
||||||
|
@ -1332,7 +1391,7 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
|
|
||||||
defp allow_nil_option(_, _), do: "Select an option"
|
defp allow_nil_option(_, _), do: "Select an option"
|
||||||
|
|
||||||
defp can_append_embed?(form, attribute) do
|
defp can_append_embed?(form, attribute, {:array, _}) do
|
||||||
case AshPhoenix.Form.value(form, attribute) do
|
case AshPhoenix.Form.value(form, attribute) do
|
||||||
%Ash.Union{value: nil} ->
|
%Ash.Union{value: nil} ->
|
||||||
true
|
true
|
||||||
|
@ -1348,6 +1407,8 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp can_append_embed?(_, _, _), do: false
|
||||||
|
|
||||||
defp placeholder(value) when is_function(value) do
|
defp placeholder(value) when is_function(value) do
|
||||||
"DEFAULT"
|
"DEFAULT"
|
||||||
end
|
end
|
||||||
|
@ -1504,7 +1565,14 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
list =
|
list =
|
||||||
AshPhoenix.Form.get_form(socket.assigns.form, path)
|
AshPhoenix.Form.get_form(socket.assigns.form, path)
|
||||||
|> AshPhoenix.Form.value(String.to_existing_atom(field))
|
|> AshPhoenix.Form.value(String.to_existing_atom(field))
|
||||||
|> Kernel.||([])
|
|> List.wrap()
|
||||||
|
|> Enum.map(fn
|
||||||
|
%AshPhoenix.Form{} = form ->
|
||||||
|
AshPhoenix.Form.params(form)
|
||||||
|
|
||||||
|
other ->
|
||||||
|
other
|
||||||
|
end)
|
||||||
|> indexed_list()
|
|> indexed_list()
|
||||||
|> append_to_and_map(to_append)
|
|> append_to_and_map(to_append)
|
||||||
|
|
||||||
|
@ -1515,6 +1583,8 @@ defmodule AshAdmin.Components.Resource.Form do
|
||||||
list
|
list
|
||||||
)
|
)
|
||||||
|
|
||||||
|
IO.inspect(params)
|
||||||
|
|
||||||
form = AshPhoenix.Form.validate(socket.assigns.form, params)
|
form = AshPhoenix.Form.validate(socket.assigns.form, params)
|
||||||
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
|
|
3
mix.exs
3
mix.exs
|
@ -116,7 +116,8 @@ defmodule AshAdmin.MixProject do
|
||||||
defp deps do
|
defp deps do
|
||||||
[
|
[
|
||||||
{:ash, "~> 3.0"},
|
{:ash, "~> 3.0"},
|
||||||
{:ash_phoenix, "~> 2.0 and >= 2.0.2"},
|
# {:ash_phoenix, "~> 2.0 and >= 2.0.2"},
|
||||||
|
{:ash_phoenix, path: "../ash_phoenix"},
|
||||||
{:phoenix_view, "~> 2.0"},
|
{:phoenix_view, "~> 2.0"},
|
||||||
{:phoenix, "~> 1.7"},
|
{:phoenix, "~> 1.7"},
|
||||||
{:phoenix_live_view, "~> 0.19"},
|
{:phoenix_live_view, "~> 0.19"},
|
||||||
|
|
Loading…
Reference in a new issue