Skip to content

Commit

Permalink
Changes based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ringvold committed Dec 27, 2024
1 parent 792ee34 commit fb80e77
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 50 deletions.
21 changes: 6 additions & 15 deletions lib/live_select/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/live_select/component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
data-debounce={@debounce}
>
<div class={class(@style, :tags_container, @tags_container_class, @tags_container_extra_class)}>
<%= 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 %>
<div class={class(@style, :tag, @tag_class, @tag_extra_class)}>
<%= if @tag == [] do %>
Expand Down
41 changes: 16 additions & 25 deletions lib/support/live_select_web/live/showcase_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -141,19 +141,6 @@ defmodule LiveSelectWeb.ShowcaseLive do
(settings.mode != :single && option == :allow_clear)
end)
|> Keyword.new()
|> then(&maybe_set_classes_for_multiselect/1)
end

defp maybe_set_classes_for_multiselect(opts) do
if LiveSelectWeb.ShowcaseLive.quick_tags?(opts[:mode]) do
Keyword.put(
opts,
:selected_option_class,
"cursor-pointer font-bold hover:bg-gray-400 rounded"
)
else
opts
end
end

def has_style_errors?(%Ecto.Changeset{errors: errors}) do
Expand Down Expand Up @@ -260,7 +247,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 %>
<div>
<span class="text-success">&lt;.live_select</span>
<br />&nbsp;&nbsp; <span class="text-success">field</span>=<span class="text-info">&#123;my_form[:city_search]&#125;</span>
Expand Down Expand Up @@ -341,6 +328,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(
Expand All @@ -355,16 +353,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)
Expand Down
8 changes: 4 additions & 4 deletions lib/support/live_select_web/live/showcase_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
{checkbox(@settings_form, :disabled, class: "toggle")}
<% end %>
<%= label class: "label cursor-pointer" do %>
<span class="label-text mr-1">Custom option HTML:&nbsp;</span>
<%= checkbox(@settings_form, :custom_option_html, class: "toggle") %>
<span class="label-text mr-1">Options styles as checkboxes:&nbsp;</span>
<%= checkbox(@settings_form, :options_styled_as_checkboxes, class: "toggle") %>
<% end %>
</div>
<div class="form-control max-w-sm">
Expand Down Expand Up @@ -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 %>
<div class="flex justify-content items-center">
<input
class="rounded w-4 h-4 mr-3 border border-border"
Expand Down Expand Up @@ -346,7 +346,7 @@
</button>
</div>
<Render.live_select
custom_option_html={@settings_form[:custom_option_html].value}
options_styled_as_checkboxes={@settings_form[:options_styled_as_checkboxes].value}
opts={Settings.live_select_opts(@settings_form.data, true)}
/>
</div>
Expand Down
5 changes: 0 additions & 5 deletions test/live_select/component_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fb80e77

Please sign in to comment.