Skip to content

Commit bfcc6bd

Browse files
committed
fix Error: key :label not found in :nil
Fixes #125
1 parent 31253ac commit bfcc6bd

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

lib/live_select/component.ex

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,7 @@ defmodule LiveSelect.Component do
261261
end
262262

263263
@impl true
264-
def handle_event("options_clear", _params, socket) do
265-
socket =
266-
socket
267-
|> assign(current_text: "", options: [])
268-
269-
{:noreply, socket}
270-
end
264+
def handle_event("options_clear", _params, socket), do: {:noreply, clear_options(socket)}
271265

272266
@impl true
273267
def handle_event("keydown", %{"key" => "ArrowDown"}, socket) do
@@ -486,7 +480,6 @@ defmodule LiveSelect.Component do
486480
else
487481
[selected]
488482
end
489-
|> Enum.reject(&is_nil/1)
490483

491484
socket =
492485
socket
@@ -499,7 +492,7 @@ defmodule LiveSelect.Component do
499492
&if keep_options_on_select?(&1) do
500493
&1
501494
else
502-
assign(&1, %{options: [], current_text: ""})
495+
clear_options(&1)
503496
end
504497
)
505498

@@ -533,6 +526,8 @@ defmodule LiveSelect.Component do
533526
|> client_select(params)
534527
end
535528

529+
defp clear_options(socket), do: assign(socket, current_text: "", options: [], active_option: -1)
530+
536531
defp client_select(socket, extra_params) do
537532
socket
538533
|> push_event(

test/live_select_test.exs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,8 @@ defmodule LiveSelectTest do
11071107
end
11081108
end
11091109

1110-
test "handles nil in options list when navigating and clearing input", %{conn: conn} do
1111-
stub_options([{"A", 1}, {"B", 2}, {"C", 3}])
1110+
test "handles hitting enter after options have been cleared", %{conn: conn} do
1111+
stub_options(["A", "B", "C"])
11121112

11131113
{:ok, live, _html} = live(conn, "/")
11141114

@@ -1125,4 +1125,34 @@ defmodule LiveSelectTest do
11251125

11261126
refute_selected(live)
11271127
end
1128+
1129+
test "handles hitting enter after options have been cleared and something was previously selected",
1130+
%{
1131+
conn: conn
1132+
} do
1133+
stub_options(["A", "B", "C"])
1134+
1135+
{:ok, live, _html} = live(conn, "/")
1136+
1137+
type(live, "ABC")
1138+
1139+
assert_options(live, ["A", "B", "C"])
1140+
1141+
select_nth_option(live, 2)
1142+
1143+
assert_selected(live, "B")
1144+
1145+
type(live, "ABC")
1146+
1147+
assert_options(live, ["A", "B", "C"])
1148+
1149+
keydown(live, "ArrowDown")
1150+
keydown(live, "Backspace")
1151+
1152+
type(live, "")
1153+
1154+
keydown(live, "Enter")
1155+
1156+
assert_selected_static(live, "B")
1157+
end
11281158
end

0 commit comments

Comments
 (0)