Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to liveview 1.0 #687

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions demo/assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
@import "tailwindcss/utilities";

@layer utilities {
/* LiveView specific classes */
.phx-no-feedback.invalid-feedback,
.phx-no-feedback .invalid-feedback {
display: none;
}

.phx-click-loading {
opacity: 0.5;
transition: opacity 1s ease-out;
Expand Down
1 change: 0 additions & 1 deletion demo/assets/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ module.exports = {
plugins: [
require('@tailwindcss/typography'),
require('daisyui'),
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
2 changes: 1 addition & 1 deletion demo/lib/demo_web/components/layouts/admin.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@
</Backpex.HTML.Layout.sidebar_section>
</:sidebar>
<Backpex.HTML.Layout.flash_messages flash={@flash} />
<%= @inner_content %>
{@inner_content}
</Backpex.HTML.Layout.app_shell>
2 changes: 1 addition & 1 deletion demo/lib/demo_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= @inner_content %>
{@inner_content}
4 changes: 2 additions & 2 deletions demo/lib/demo_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<meta name="csrf-token" content={get_csrf_token()} />
<.sentry_meta_tag />
<.live_title suffix=" · Backpex">
<%= assigns[:page_title] || "Phoenix Admin Panel build with PETAL" %>
{assigns[:page_title] || "Phoenix Admin Panel build with PETAL"}
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="h-full">
<%= @inner_content %>
{@inner_content}
<.analytics />
</body>
</html>
2 changes: 1 addition & 1 deletion demo/lib/demo_web/live/post_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ defmodule DemoWeb.PostLive do
searchable: true,
render: fn assigns ->
~H"""
<p><%= Number.Delimit.number_to_delimited(@value, precision: 0, delimiter: ".") %></p>
<p>{Number.Delimit.number_to_delimited(@value, precision: 0, delimiter: ".")}</p>
"""
end
},
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/product_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule DemoWeb.ProductLive do
"""

assigns ->
~H"<p><%= Backpex.HTML.pretty_value(@value) %></p>"
~H"<p>{Backpex.HTML.pretty_value(@value)}</p>"
end,
except: [:index, :resource_action],
align: :center
Expand Down Expand Up @@ -80,7 +80,7 @@ defmodule DemoWeb.ProductLive do
end,
render: fn assigns ->
~H"""
<p><%= Number.Delimit.number_to_delimited(@value, precision: 0, delimiter: ".") %></p>
<p>{Number.Delimit.number_to_delimited(@value, precision: 0, delimiter: ".")}</p>
"""
end
},
Expand Down
2 changes: 1 addition & 1 deletion demo/lib/demo_web/live/user_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule DemoWeb.UserLive do
list_existing_files: &list_existing_files/1,
render: fn
%{value: value} = assigns when value == "" or is_nil(value) ->
~H"<p><%= Backpex.HTML.pretty_value(@value) %></p>"
~H"<p>{Backpex.HTML.pretty_value(@value)}</p>"

assigns ->
~H'<img class="h-10 w-auto" src={file_url(@value)} />'
Expand Down
4 changes: 2 additions & 2 deletions demo/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ defmodule Demo.MixProject do
{:telemetry_poller, "~> 1.0"},
{:telemetry_metrics, "~> 1.0"},
{:gettext, "~> 0.26"},
{:sentry, "~> 10.1"},
{:sentry, "~> 10.8"},
{:hackney, "~> 1.17", override: true},
{:circular_buffer, "~> 0.4.0"},

# phoenix
{:bandit, "~> 1.0"},
{:phoenix, "~> 1.7.6"},
{:phoenix_pubsub, "~> 2.0"},
{:phoenix_live_view, "~> 0.20.0"},
{:phoenix_live_view, "~> 1.0"},
{:phoenix_live_dashboard, "~> 0.8"},
{:phoenix_live_reload, "~> 1.2", only: :dev},

Expand Down
20 changes: 10 additions & 10 deletions demo/mix.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/backpex/fields/belongs_to.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Backpex.Fields.BelongsTo do
def render_value(%{value: value} = assigns) when is_nil(value) do
~H"""
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= HTML.pretty_value(nil) %>
{HTML.pretty_value(nil)}
</p>
"""
end
Expand All @@ -105,11 +105,11 @@ defmodule Backpex.Fields.BelongsTo do
<div class={[@live_action in [:index, :resource_action] && "truncate"]}>
<%= if @link do %>
<.link navigate={@link} class={[@live_action in [:index, :resource_action] && "truncate", "hover:underline"]}>
<%= @display_text %>
{@display_text}
</.link>
<% else %>
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= HTML.pretty_value(@display_text) %>
{HTML.pretty_value(@display_text)}
</p>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/fields/currency.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Backpex.Fields.Currency do

~H"""
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= @casted_value %>
{@casted_value}
</p>
"""
end
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/fields/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Backpex.Fields.Date do

~H"""
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= @value %>
{@value}
</p>
"""
end
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/fields/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Backpex.Fields.DateTime do

~H"""
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= @value %>
{@value}
</p>
"""
end
Expand Down
35 changes: 19 additions & 16 deletions lib/backpex/fields/has_many.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule Backpex.Fields.HasMany do
def render_value(assigns) do
~H"""
<div class={[@live_action in [:index, :resource_action] && "truncate"]}>
<%= if @value == [], do: raw("&mdash;") %>
{if @value == [], do: raw("&mdash;")}

<div class={["flex", @live_action == :show && "flex-wrap"]}>
<.intersperse :let={item} enum={@value |> Enum.sort_by(&Map.get(&1, display_field(@field)), :asc)}>
Expand Down Expand Up @@ -146,23 +146,23 @@ defmodule Backpex.Fields.HasMany do
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
<Layout.input_label text={@field_options[:label]} />
</:label>
<div class="dropdown w-full" phx-feedback-for={@form[@name].name}>
<div class="dropdown w-full">
<label
tabindex="0"
class={[
"input block h-fit w-full p-2 phx-no-feedback:input-bordered phx-no-feedback:bg-transparent",
"input block h-fit w-full p-2",
@errors == [] && "input-bordered bg-transparent",
@errors != [] && "input-error bg-error/10"
]}
>
<div class="flex h-full w-full flex-wrap items-center gap-1 px-2">
<p :if={@selected == []} class="p-0.5 text-sm">
<%= @prompt %>
{@prompt}
</p>

<div :for={{label, value} <- @selected} class="badge badge-primary p-[11px]">
<p class="mr-1">
<%= label %>
{label}
</p>

<label
Expand All @@ -175,7 +175,7 @@ defmodule Backpex.Fields.HasMany do
</div>
</div>
</label>
<.error :for={msg <- @errors}><%= msg %></.error>
<.error :for={msg <- @errors}>{msg}</.error>
<div tabindex="0" class="dropdown-content z-[1] menu bg-base-100 rounded-box w-full overflow-y-auto shadow">
<div class="max-h-72 p-2">
<input
Expand All @@ -188,7 +188,7 @@ defmodule Backpex.Fields.HasMany do
value={@search_input}
/>
<p :if={@options == []} class="w-full">
<%= @not_found_text %>
{@not_found_text}
</p>

<label :if={Enum.any?(@options)}>
Expand All @@ -200,14 +200,14 @@ defmodule Backpex.Fields.HasMany do
/>
<span role="button" class="text-primary my-2 cursor-pointer text-sm underline">
<%= if @all_selected do %>
<%= Backpex.translate("Deselect all") %>
{Backpex.translate("Deselect all")}
<% else %>
<%= Backpex.translate("Select all") %>
{Backpex.translate("Select all")}
<% end %>
</span>
</label>

<input type="hidden" id={"has-many-#{@name}-hidden-input"} name={@form[@name].name} value="" />
<input type="hidden" id={"has-many-#{@name}-hidden-input"} name={"#{@form[@name].name}[]"} value="" />

<input
:for={value <- @selected_ids}
Expand All @@ -231,7 +231,7 @@ defmodule Backpex.Fields.HasMany do
class="checkbox checkbox-sm checkbox-primary"
/>
<span class="label-text">
<%= label %>
{label}
</span>
</label>
</div>
Expand All @@ -243,7 +243,7 @@ defmodule Backpex.Fields.HasMany do
phx-click="show-more"
phx-target={@myself}
>
<%= Backpex.translate("Show more") %>
{Backpex.translate("Show more")}
</button>
</div>
</div>
Expand Down Expand Up @@ -336,8 +336,10 @@ defmodule Backpex.Fields.HasMany do
defp get_assocs_by_ids(assoc_ids, schema, repo, field_options, assigns) do
case assoc_ids do
ids when is_list(ids) and ids != [] ->
filtered_ids = Enum.reject(ids, &(&1 == ""))

schema
|> where([x], x.id in ^ids)
|> where([x], x.id in ^filtered_ids)
|> maybe_options_query(field_options, assigns)
|> repo.all()

Expand All @@ -360,11 +362,11 @@ defmodule Backpex.Fields.HasMany do
~H"""
<%= if is_nil(@link) do %>
<span>
<%= HTML.pretty_value(@display_text) %>
{HTML.pretty_value(@display_text)}
</span>
<% else %>
<.link navigate={@link} class="hover:underline">
<%= @display_text %>
{@display_text}
</.link>
<% end %>
"""
Expand Down Expand Up @@ -538,9 +540,10 @@ defmodule Backpex.Fields.HasMany do
defp assign_form_errors(socket) do
%{assigns: %{form: form, name: name, field_options: field_options}} = socket

errors = if Phoenix.Component.used_input?(form[name]), do: form[name].errors, else: []
Flo0807 marked this conversation as resolved.
Show resolved Hide resolved
translate_error_fun = Map.get(field_options, :translate_error, &Function.identity/1)

assign(socket, :errors, translate_form_errors(form[name], translate_error_fun))
assign(socket, :errors, translate_form_errors(errors, translate_error_fun))
end

defp display_field_form({_name, field_options} = field),
Expand Down
16 changes: 8 additions & 8 deletions lib/backpex/fields/has_many_through.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ defmodule Backpex.Fields.HasManyThrough do
:for={{_name, %{label: label}} <- action_fields(@field_options.child_fields, assigns, :index)}
class="font-medium"
>
<%= label %>
{label}
</th>
<th
:for={{_name, %{label: label}} <- action_fields(@field_options.pivot_fields, assigns, :index)}
class="font-medium"
>
<%= label %>
{label}
</th>
<th></th>
</tr>
Expand Down Expand Up @@ -252,13 +252,13 @@ defmodule Backpex.Fields.HasManyThrough do
:for={{_name, %{label: label}} <- action_fields(@field_options.child_fields, assigns, :index)}
class="font-medium"
>
<%= label %>
{label}
</th>
<th
:for={{_name, %{label: label}} <- action_fields(@field_options.pivot_fields, assigns, :index)}
class="font-medium"
>
<%= label %>
{label}
</th>
<th></th>
</tr>
Expand Down Expand Up @@ -330,7 +330,7 @@ defmodule Backpex.Fields.HasManyThrough do
>
<div class="py-3">
<div :for={e <- @editables} class={[unless(e.index == @edit_relational, do: "hidden")]}>
<%= hidden_inputs_for(e) %>
{hidden_inputs_for(e)}
<.select_relational_field
form={e}
label={@field_options.live_resource.singular_name()}
Expand All @@ -343,17 +343,17 @@ defmodule Backpex.Fields.HasManyThrough do
</div>
<div class="bg-base-200 flex justify-end space-x-4 px-6 py-3">
<button type="button" class="btn" phx-click="cancel-relational" phx-target={@myself}>
<%= Backpex.translate("Cancel") %>
{Backpex.translate("Cancel")}
</button>

<button type="button" class="btn btn-primary" phx-click="complete-relational" phx-target={@myself}>
<%= Backpex.translate("Apply") %>
{Backpex.translate("Apply")}
</button>
</div>
</.modal>

<button type="button" class="btn btn-sm btn-outline btn-primary" phx-click="new-relational" phx-target={@myself}>
<%= @relational_title %>
{@relational_title}
</button>
</Layout.field_container>
</div>
Expand Down
6 changes: 3 additions & 3 deletions lib/backpex/fields/inline_crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ defmodule Backpex.Fields.InlineCRUD do
<thead class="bg-base-200/50 text-base-content uppercase">
<tr>
<th :for={{_name, %{label: label}} <- @field_options.child_fields} class="font-medium">
<%= label %>
{label}
</th>
</tr>
</thead>
<tbody class="text-base-content/75">
<tr :for={row <- @value} class="border-base-content/10 border-b last:border-b-0">
<td :for={{name, _field_options} <- @field_options.child_fields}>
<%= HTML.pretty_value(Map.get(row, name)) %>
{HTML.pretty_value(Map.get(row, name))}
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -143,7 +143,7 @@ defmodule Backpex.Fields.InlineCRUD do
class={child_field_class(child_field_options, assigns)}
>
<p :if={f_nested.index == 0} class="mb-1 text-xs">
<%= child_field_options.label %>
{child_field_options.label}
</p>
<BackpexForm.input
type={input_type(child_field_options) |> Atom.to_string()}
Expand Down
4 changes: 2 additions & 2 deletions lib/backpex/fields/multi_select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ defmodule Backpex.Fields.MultiSelect do

~H"""
<div class={[@live_action in [:index, :resource_action] && "truncate"]}>
<%= if @selected_labels == [], do: raw("&mdash;") %>
{if @selected_labels == [], do: raw("&mdash;")}

<div class={["flex", @live_action == :show && "flex-wrap"]}>
<.intersperse :let={item} enum={@selected_labels}>
<:separator>
,&nbsp;
</:separator>
<p>
<%= HTML.pretty_value(item) %>
{HTML.pretty_value(item)}
</p>
</.intersperse>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/fields/number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Backpex.Fields.Number do
def render_value(assigns) do
~H"""
<p class={@live_action in [:index, :resource_action] && "truncate"}>
<%= HTML.pretty_value(@value) %>
{HTML.pretty_value(@value)}
</p>
"""
end
Expand Down
Loading