Skip to content

Commit

Permalink
do not delete last selection
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmarcon committed Jun 18, 2024
1 parent 5be6988 commit 5015191
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/live_select/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule LiveSelect.Component do
active_option: -1,
hide_dropdown: true,
awaiting_update: true,
saved_selection: nil,
last_selection: nil,
selection: [],
value_mapper: & &1
)
Expand Down Expand Up @@ -464,21 +464,20 @@ defmodule LiveSelect.Component do

defp maybe_save_selection(socket) do
socket
|> update(:saved_selection, fn
|> update(:last_selection, fn
_, %{selection: selection, mode: :single} when selection != [] -> selection
saved_selection, _ -> saved_selection
last_selection, _ -> last_selection
end)
end

defp maybe_restore_selection(socket) do
update(socket, :selection, fn
_, %{saved_selection: saved_selection, mode: :single} when saved_selection != nil ->
saved_selection
_, %{last_selection: last_selection, mode: :single} when last_selection != nil ->
last_selection

selection, _ ->
selection
end)
|> assign(:saved_selection, nil)
end

defp clear(socket, params) do
Expand Down
13 changes: 13 additions & 0 deletions test/live_select_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,19 @@ defmodule LiveSelectTest do
assert_selected_static(live, :B, 2)
end

test "blurring, then focusing, then blurring again restores the selection", %{live: live} do
element(live, selectors()[:text_input])
|> render_blur()

element(live, selectors()[:text_input])
|> render_focus()

element(live, selectors()[:text_input])
|> render_blur()

assert_selected_static(live, :B, 2)
end

test "a focus event is sent to the parent", %{live: live} do
assert_push_event(live, "select", %{
id: "my_form_city_search_live_select_component",
Expand Down

0 comments on commit 5015191

Please sign in to comment.