Skip to content

Commit

Permalink
Fix focus issue on first select
Browse files Browse the repository at this point in the history
Do not move focus back to search box on first select.
Also refactor to use more efficient way of checking for already selected.
  • Loading branch information
ringvold committed Sep 3, 2024
1 parent f4d4e24 commit 1040874
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/live_select/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ defmodule LiveSelect.Component do
&if &1.assigns.mode == :single do
clear(&1, %{input_event: false, parent_event: &1.assigns[:"phx-focus"]})
else
parent_event(&1, &1.assigns[:"phx-focus"], %{id: &1.assigns.id})
if &1.assigns.tags_mode == :multiple_select do
&1
else
parent_event(&1, &1.assigns[:"phx-focus"], %{id: &1.assigns.id})
end
end
)
|> assign(hide_dropdown: false)
Expand Down Expand Up @@ -464,8 +468,7 @@ defmodule LiveSelect.Component do

socket
|> assign(
active_option:
if(multi_select_mode?(socket), do: socket.assigns.active_option, else: -1),
active_option: if(multi_select_mode?(socket), do: socket.assigns.active_option, else: -1),
selection: selection,
hide_dropdown: not multi_select_mode?(socket)
)
Expand Down Expand Up @@ -689,7 +692,7 @@ defmodule LiveSelect.Component do
end

def already_selected?(option, selection) do
option.label in Enum.map(selection, & &1.label)
Enum.any?(selection, fn item -> item.label == option.label end)
end

defp multi_select_mode?(socket) do
Expand Down

0 comments on commit 1040874

Please sign in to comment.