Skip to content

Commit 9ffa3cb

Browse files
riebecjCasey Riebegreptile-apps[bot]
authored
If we have set_focus() we need blur_focus(). (#5522)
* If we have `set_focus()` we need `blur_focus()`. * Update reflex/.templates/web/utils/state.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Making darglint happy --------- Co-authored-by: Casey Riebe <cjriebe@sbgtv.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent a83ce0c commit 9ffa3cb

3 files changed

Lines changed: 46 additions & 7 deletions

File tree

reflex/.templates/web/utils/state.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ export const applyEvent = async (event, socket, navigate, params) => {
295295
return false;
296296
}
297297

298+
if (event.name == "_blur_focus") {
299+
const ref =
300+
event.payload.ref in refs ? refs[event.payload.ref] : event.payload.ref;
301+
const current = ref?.current;
302+
if (current === undefined || current?.blur === undefined) {
303+
console.error(
304+
`No element found for ref ${event.payload.ref} in _blur_focus`,
305+
);
306+
} else {
307+
current.blur();
308+
}
309+
return false;
310+
}
311+
298312
if (event.name == "_set_value") {
299313
const ref =
300314
event.payload.ref in refs ? refs[event.payload.ref] : event.payload.ref;

reflex/event.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,22 @@ def set_focus(ref: str) -> EventSpec:
10251025
)
10261026

10271027

1028+
def blur_focus(ref: str) -> EventSpec:
1029+
"""Blur focus of specified ref.
1030+
1031+
Args:
1032+
ref: The ref.
1033+
1034+
Returns:
1035+
An event to blur focus on the ref
1036+
"""
1037+
return server_side(
1038+
"_blur_focus",
1039+
get_fn_signature(blur_focus),
1040+
ref=LiteralVar.create(format.format_ref(ref)),
1041+
)
1042+
1043+
10281044
def scroll_to(elem_id: str, align_to_top: bool | Var[bool] = True) -> EventSpec:
10291045
"""Select the id of a html element for scrolling into view.
10301046
@@ -2293,6 +2309,7 @@ def wrapper(
22932309
back = staticmethod(back)
22942310
window_alert = staticmethod(window_alert)
22952311
set_focus = staticmethod(set_focus)
2312+
blur_focus = staticmethod(blur_focus)
22962313
scroll_to = staticmethod(scroll_to)
22972314
set_value = staticmethod(set_value)
22982315
remove_cookie = staticmethod(remove_cookie)

tests/units/test_event.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,24 @@ def test_event_window_alert():
262262
)
263263

264264

265-
def test_set_focus():
266-
"""Test the event set focus function."""
267-
spec = event.set_focus("input1")
265+
@pytest.mark.parametrize(
266+
("func", "qualname"), [("set_focus", "_set_focus"), ("blur_focus", "_blur_focus")]
267+
)
268+
def test_focus(func: str, qualname: str):
269+
"""Test the event set focus function.
270+
271+
Args:
272+
func: The event function name.
273+
qualname: The sig qual name passed to JS.
274+
"""
275+
spec = getattr(event, func)("input1")
268276
assert isinstance(spec, EventSpec)
269-
assert spec.handler.fn.__qualname__ == "_set_focus"
277+
assert spec.handler.fn.__qualname__ == qualname
270278
assert spec.args[0][0].equals(Var(_js_expr="ref"))
271279
assert spec.args[0][1].equals(LiteralVar.create("ref_input1"))
272-
assert format.format_event(spec) == 'Event("_set_focus", {ref:"ref_input1"})'
273-
spec = event.set_focus("input1")
274-
assert format.format_event(spec) == 'Event("_set_focus", {ref:"ref_input1"})'
280+
assert format.format_event(spec) == f'Event("{qualname}", {{ref:"ref_input1"}})'
281+
spec = getattr(event, func)("input1")
282+
assert format.format_event(spec) == f'Event("{qualname}", {{ref:"ref_input1"}})'
275283

276284

277285
def test_set_value():

0 commit comments

Comments
 (0)