Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions reflex/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ export const applyEvent = async (event, socket, navigate, params) => {
return false;
}

if (event.name == "_blur_focus") {
const ref =
event.payload.ref in refs ? refs[event.payload.ref] : event.payload.ref;
const current = ref?.current;
if (current === undefined || current?.blur === undefined) {
console.error(
`No element found for ref ${event.payload.ref} in _blur_focus`,
);
} else {
current.blur();
}
return false;
}

if (event.name == "_set_value") {
const ref =
event.payload.ref in refs ? refs[event.payload.ref] : event.payload.ref;
Expand Down
17 changes: 17 additions & 0 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,22 @@ def set_focus(ref: str) -> EventSpec:
)


def blur_focus(ref: str) -> EventSpec:
"""Blur focus of specified ref.

Args:
ref: The ref.

Returns:
An event to blur focus on the ref
"""
return server_side(
"_blur_focus",
get_fn_signature(blur_focus),
ref=LiteralVar.create(format.format_ref(ref)),
)


def scroll_to(elem_id: str, align_to_top: bool | Var[bool] = True) -> EventSpec:
"""Select the id of a html element for scrolling into view.

Expand Down Expand Up @@ -2293,6 +2309,7 @@ def wrapper(
back = staticmethod(back)
window_alert = staticmethod(window_alert)
set_focus = staticmethod(set_focus)
blur_focus = staticmethod(blur_focus)
scroll_to = staticmethod(scroll_to)
set_value = staticmethod(set_value)
remove_cookie = staticmethod(remove_cookie)
Expand Down
22 changes: 15 additions & 7 deletions tests/units/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,24 @@ def test_event_window_alert():
)


def test_set_focus():
"""Test the event set focus function."""
spec = event.set_focus("input1")
@pytest.mark.parametrize(
("func", "qualname"), [("set_focus", "_set_focus"), ("blur_focus", "_blur_focus")]
)
def test_focus(func: str, qualname: str):
"""Test the event set focus function.

Args:
func: The event function name.
qualname: The sig qual name passed to JS.
"""
spec = getattr(event, func)("input1")
assert isinstance(spec, EventSpec)
assert spec.handler.fn.__qualname__ == "_set_focus"
assert spec.handler.fn.__qualname__ == qualname
assert spec.args[0][0].equals(Var(_js_expr="ref"))
assert spec.args[0][1].equals(LiteralVar.create("ref_input1"))
assert format.format_event(spec) == 'Event("_set_focus", {ref:"ref_input1"})'
spec = event.set_focus("input1")
assert format.format_event(spec) == 'Event("_set_focus", {ref:"ref_input1"})'
assert format.format_event(spec) == f'Event("{qualname}", {{ref:"ref_input1"}})'
spec = getattr(event, func)("input1")
assert format.format_event(spec) == f'Event("{qualname}", {{ref:"ref_input1"}})'
Comment thread
riebecj marked this conversation as resolved.


def test_set_value():
Expand Down
Loading