Skip to content

Commit 5833118

Browse files
committed
Added focus_in and focus_out events to component text_field
1 parent 8b03277 commit 5833118

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/scenic/component/input/text_field.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ defmodule Scenic.Component.Input.TextField do
254254
end
255255

256256
# --------------------------------------------------------
257-
defp capture_focus(%{assigns: %{focused: false, graph: graph, theme: theme}} = scene) do
257+
defp capture_focus(%{assigns: %{focused: false, graph: graph, id: id, theme: theme}} = scene) do
258258
# capture the input
259259
capture_input(scene, @input_capture)
260+
:ok = send_parent_event(scene, {:focus_in, id})
260261

261262
# start animating the caret
262263
cast_children(scene, :start_caret)
@@ -274,9 +275,10 @@ defmodule Scenic.Component.Input.TextField do
274275
end
275276

276277
# --------------------------------------------------------
277-
defp release_focus(%{assigns: %{focused: true, graph: graph, theme: theme}} = scene) do
278+
defp release_focus(%{assigns: %{focused: true, graph: graph, id: id, theme: theme}} = scene) do
278279
# release the input
279280
release_input(scene)
281+
:ok = send_parent_event(scene, {:focus_out, id})
280282

281283
# stop animating the caret
282284
cast_children(scene, :stop_caret)

test/scenic/component/input/text_field_test.exs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,29 @@ defmodule Scenic.Component.Input.TextFieldTest do
9494
:_pong_ = GenServer.call(vp_pid, :_ping_)
9595
end
9696

97-
test "press_in captures and starts editing", %{vp: vp, pid: pid} do
97+
test "press_in captures, starts editing and fire focus_in event", %{vp: vp, pid: pid} do
9898
assert Input.fetch_captures!(vp) == {:ok, []}
9999
Input.send(vp, @press_in)
100100
force_sync(vp.pid, pid)
101+
101102
assert Input.fetch_captures!(vp) ~> {:ok, sorted_list([:codepoint, :cursor_button, :key])}
103+
assert_receive({:fwd_event, {:focus_in, :text_field}}, 200)
102104

103105
Input.send(vp, @cp_k)
104106
assert_receive({:fwd_event, {:value_changed, :text_field, "kInitial value"}}, 200)
105107
end
106108

107-
test "press_out releases and ends editing", %{vp: vp, pid: pid} do
109+
test "press_out releases, ends editing and fire focus_out event", %{vp: vp, pid: pid} do
108110
Input.send(vp, @press_in)
109111
force_sync(vp.pid, pid)
110112

111113
assert Input.fetch_captures!(vp) ~> {:ok, sorted_list([:codepoint, :cursor_button, :key])}
114+
assert_receive({:fwd_event, {:focus_in, :text_field}}, 200)
112115

113116
Input.send(vp, @press_out)
114117
force_sync(vp.pid, pid)
115118
assert Input.fetch_captures!(vp) == {:ok, []}
119+
assert_receive({:fwd_event, {:focus_out, :text_field}}, 200)
116120

117121
Input.send(vp, @cp_k)
118122
refute_receive(_, 10)
@@ -121,6 +125,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
121125
test "pressing in the field moves the cursor to the nearst character gap", %{vp: vp, pid: pid} do
122126
Input.send(vp, @press_in)
123127
force_sync(vp.pid, pid)
128+
assert_receive({:fwd_event, {:focus_in, :text_field}}, 200)
124129

125130
Input.send(vp, @cp_k)
126131
assert_receive({:fwd_event, {:value_changed, :text_field, "kInitial value"}}, 200)
@@ -201,6 +206,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
201206
test "backspace does nothing at the start of the string", %{vp: vp, pid: pid} do
202207
Input.send(vp, @press_in)
203208
force_sync(vp.pid, pid)
209+
assert_receive({:fwd_event, {:focus_in, :text_field}}, 200)
204210

205211
Input.send(vp, @key_backspace)
206212
refute_receive(_, 10)
@@ -217,6 +223,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
217223
test "delete does nothing at the end of the field", %{vp: vp, pid: pid} do
218224
Input.send(vp, @press_in)
219225
force_sync(vp.pid, pid)
226+
assert_receive({:fwd_event, {:focus_in, :text_field}}, 200)
220227

221228
Input.send(vp, @key_end)
222229
Input.send(vp, @key_delete)
@@ -229,6 +236,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
229236

230237
Input.send(vp, {:cursor_button, {:btn_left, 1, [], {20, 60}}})
231238
force_sync(vp.pid, pid)
239+
assert_receive({:fwd_event, {:focus_in, :number_field}}, 200)
232240

233241
Input.send(vp, {:codepoint, {"a", []}})
234242
refute_receive(_, 10)
@@ -248,6 +256,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
248256

249257
Input.send(vp, {:cursor_button, {:btn_left, 1, [], {14, 86}}})
250258
force_sync(vp.pid, pid)
259+
assert_receive({:fwd_event, {:focus_in, :integer_field}}, 200)
251260

252261
Input.send(vp, {:codepoint, {"a", []}})
253262
refute_receive(_, 10)
@@ -267,6 +276,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
267276

268277
Input.send(vp, {:cursor_button, {:btn_left, 1, [], {14, 121}}})
269278
force_sync(vp.pid, pid)
279+
assert_receive({:fwd_event, {:focus_in, :abcdefg_field}}, 200)
270280

271281
Input.send(vp, {:codepoint, {"a", []}})
272282
assert_receive({:fwd_event, {:value_changed, :abcdefg_field, "a"}}, 200)
@@ -284,6 +294,7 @@ defmodule Scenic.Component.Input.TextFieldTest do
284294

285295
Input.send(vp, {:cursor_button, {:btn_left, 1, [], {14, 171}}})
286296
force_sync(vp.pid, pid)
297+
assert_receive({:fwd_event, {:focus_in, :fn_field}}, 200)
287298

288299
Input.send(vp, {:codepoint, {"a", []}})
289300
refute_receive(_, 10)

0 commit comments

Comments
 (0)