From 42fc146ad8b229ef2964556f35a80e69313c59c0 Mon Sep 17 00:00:00 2001 From: Harald Ringvold Date: Fri, 27 Dec 2024 12:49:05 +0100 Subject: [PATCH] Changes based on review comments --- lib/live_select/component.ex | 21 ++++---------- lib/live_select/component.html.heex | 2 +- .../live_select_web/live/showcase_live.ex | 28 +++++++++++-------- .../live/showcase_live.html.heex | 8 +++--- test/live_select/component_test.exs | 5 ---- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/lib/live_select/component.ex b/lib/live_select/component.ex index 6a5eb4c..d0970a0 100644 --- a/lib/live_select/component.ex +++ b/lib/live_select/component.ex @@ -441,7 +441,7 @@ defmodule LiveSelect.Component do pos = get_selection_index(option, selection) unselect(socket, pos) else - select(socket, Enum.at(options, active_option), extra_params) + select(socket, option, extra_params) end end @@ -455,15 +455,10 @@ defmodule LiveSelect.Component do defp select(socket, selected, extra_params) do selection = - case socket.assigns.mode do - :tags -> - socket.assigns.selection ++ [selected] - - :quick_tags -> - socket.assigns.selection ++ [selected] - - _ -> - [selected] + if socket.assigns.mode in [:tags, :quick_tags] do + socket.assigns.selection ++ [selected] + else + [selected] end socket @@ -687,11 +682,7 @@ defmodule LiveSelect.Component do defp encode(value), do: Jason.encode!(value) - def already_selected?(idx, selection) when is_integer(idx) do - Enum.at(selection, idx) != nil - end - - def already_selected?(option, selection) do + defp already_selected?(option, selection) do Enum.any?(selection, fn item -> item.label == option.label end) end diff --git a/lib/live_select/component.html.heex b/lib/live_select/component.html.heex index 855614d..319fafc 100644 --- a/lib/live_select/component.html.heex +++ b/lib/live_select/component.html.heex @@ -9,7 +9,7 @@ data-debounce={@debounce} >
- <%= if (@mode == :tags || @mode == :quick_tags) && Enum.any?(@selection) do %> + <%= if (@mode in [:tags, :quick_tags]) && Enum.any?(@selection) do %> <%= for {option, idx} <- Enum.with_index(@selection) do %>
<%= if @tag == [] do %> diff --git a/lib/support/live_select_web/live/showcase_live.ex b/lib/support/live_select_web/live/showcase_live.ex index 24210ad..bfa397d 100644 --- a/lib/support/live_select_web/live/showcase_live.ex +++ b/lib/support/live_select_web/live/showcase_live.ex @@ -67,7 +67,7 @@ defmodule LiveSelectWeb.ShowcaseLive do field(:allow_clear, :boolean) field(:debounce, :integer, default: Component.default_opts()[:debounce]) field(:disabled, :boolean) - field(:custom_option_html, :boolean) + field(:options_styled_as_checkboxes, :boolean) field(:max_selectable, :integer, default: Component.default_opts()[:max_selectable]) field(:user_defined_options, :boolean) @@ -99,7 +99,7 @@ defmodule LiveSelectWeb.ShowcaseLive do :allow_clear, :debounce, :disabled, - :custom_option_html, + :options_styled_as_checkboxes, :max_selectable, :user_defined_options, :mode, @@ -126,7 +126,7 @@ defmodule LiveSelectWeb.ShowcaseLive do default_opts = Component.default_opts() settings - |> Map.drop([:search_delay, :new, :selection, :custom_option_html]) + |> Map.drop([:search_delay, :new, :selection, :options_styled_as_checkboxes]) |> Map.from_struct() |> then( &if is_nil(&1.style) do @@ -260,7 +260,7 @@ defmodule LiveSelectWeb.ShowcaseLive do assigns = assign(assigns, opts: opts, format_value: format_value) ~H""" - <%= if @custom_option_html do %> + <%= if @options_styled_as_checkboxes do %>
<.live_select
   field={my_form[:city_search]} @@ -341,6 +341,17 @@ defmodule LiveSelectWeb.ShowcaseLive do socket end + params = + if params["mode"] == "quick_tags" do + Map.put_new( + params, + "selected_option_class", + "cursor-pointer font-bold hover:bg-gray-400 rounded" + ) + else + params + end + changeset = Settings.changeset(params) |> then( @@ -355,16 +366,9 @@ defmodule LiveSelectWeb.ShowcaseLive do {:ok, settings} -> socket.assigns - attrs = - if settings.mode == :quick_select do - %{selected_option_class: "cursor-pointer font-bold hover:bg-gray-400 rounded"} - else - %{} - end - socket = socket - |> assign(:settings_form, Settings.changeset(settings, attrs) |> to_form) + |> assign(:settings_form, Settings.changeset(settings, %{}) |> to_form) |> update(:schema_module, fn _, %{settings_form: settings_form} -> if settings_form[:mode].value == :single, do: CitySearchSingle, else: CitySearchMany end) diff --git a/lib/support/live_select_web/live/showcase_live.html.heex b/lib/support/live_select_web/live/showcase_live.html.heex index 32a7a92..1e9b1cf 100644 --- a/lib/support/live_select_web/live/showcase_live.html.heex +++ b/lib/support/live_select_web/live/showcase_live.html.heex @@ -72,8 +72,8 @@ {checkbox(@settings_form, :disabled, class: "toggle")} <% end %> <%= label class: "label cursor-pointer" do %> - Custom option HTML:  - <%= checkbox(@settings_form, :custom_option_html, class: "toggle") %> + Options styles as checkboxes:  + <%= checkbox(@settings_form, :options_styled_as_checkboxes, class: "toggle") %> <% end %>
@@ -292,7 +292,7 @@ {live_select_assigns(@settings_form.source)} > <:option :let={%{label: label, value: _value, selected: selected}}> - <%= if @settings_form[:custom_option_html].value do %> + <%= if @settings_form[:options_styled_as_checkboxes].value do %>
diff --git a/test/live_select/component_test.exs b/test/live_select/component_test.exs index c36a49e..463e7d4 100644 --- a/test/live_select/component_test.exs +++ b/test/live_select/component_test.exs @@ -539,11 +539,6 @@ defmodule LiveSelect.ComponentTest do ] end - describe "in quick_tags mode" do - test "" do - end - end - for style <- [:daisyui, :tailwind, :none, nil] do @style style