Skip to content

Commit 737e427

Browse files
authored
add popup option for redirect (#5764)
* add popup option for redirect * fix test event
1 parent 910e0c7 commit 737e427

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ export const applyEvent = async (event, socket, navigate, params) => {
210210
return false;
211211
}
212212
if (event.payload.external) {
213-
window.open(event.payload.path, "_blank", "noopener");
213+
window.open(
214+
event.payload.path,
215+
"_blank",
216+
"noopener" + (event.payload.popup ? ",popup" : ""),
217+
);
214218
return false;
215219
}
216220
const url = urlFrom(event.payload.path);

reflex/event.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Annotated,
1414
Any,
1515
Generic,
16+
Literal,
1617
NoReturn,
1718
Protocol,
1819
TypeVar,
@@ -962,16 +963,37 @@ def fn():
962963
)
963964

964965

966+
@overload
967+
def redirect(
968+
path: str | Var[str],
969+
*,
970+
is_external: Literal[False] = False,
971+
replace: bool = False,
972+
) -> EventSpec: ...
973+
974+
975+
@overload
976+
def redirect(
977+
path: str | Var[str],
978+
*,
979+
is_external: Literal[True],
980+
popup: bool = False,
981+
) -> EventSpec: ...
982+
983+
965984
def redirect(
966985
path: str | Var[str],
986+
*,
967987
is_external: bool = False,
988+
popup: bool = False,
968989
replace: bool = False,
969990
) -> EventSpec:
970991
"""Redirect to a new path.
971992
972993
Args:
973994
path: The path to redirect to.
974995
is_external: Whether to open in new tab or not.
996+
popup: Whether to open in a new window or not.
975997
replace: If True, the current page will not create a new history entry.
976998
977999
Returns:
@@ -982,6 +1004,7 @@ def redirect(
9821004
get_fn_signature(redirect),
9831005
path=path,
9841006
external=is_external,
1007+
popup=popup,
9851008
replace=replace,
9861009
)
9871010

tests/units/test_event.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from collections.abc import Callable
23

34
import pytest
@@ -169,29 +170,13 @@ def fn_with_args(_, arg1, arg2):
169170
("input", "output"),
170171
[
171172
(
172-
("/path", None, None),
173-
'ReflexEvent("_redirect", {path:"/path",external:false,replace:false})',
174-
),
175-
(
176-
("/path", True, None),
177-
'ReflexEvent("_redirect", {path:"/path",external:true,replace:false})',
178-
),
179-
(
180-
("/path", False, None),
181-
'ReflexEvent("_redirect", {path:"/path",external:false,replace:false})',
182-
),
183-
(
184-
(Var(_js_expr="path"), None, None),
185-
'ReflexEvent("_redirect", {path:path,external:false,replace:false})',
186-
),
187-
(
188-
("/path", None, True),
189-
'ReflexEvent("_redirect", {path:"/path",external:false,replace:true})',
190-
),
191-
(
192-
("/path", True, True),
193-
'ReflexEvent("_redirect", {path:"/path",external:true,replace:true})',
194-
),
173+
(path, is_external, replace, popup),
174+
f'ReflexEvent("_redirect", {{path:{json.dumps(path) if isinstance(path, str) else path._js_expr},external:{"true" if is_external else "false"},popup:{"true" if popup else "false"},replace:{"true" if replace else "false"}}})',
175+
)
176+
for path in ("/path", Var(_js_expr="path"))
177+
for is_external in (None, True, False)
178+
for replace in (None, True, False)
179+
for popup in (None, True, False)
195180
],
196181
)
197182
def test_event_redirect(input, output):
@@ -201,12 +186,14 @@ def test_event_redirect(input, output):
201186
input: The input for running the test.
202187
output: The expected output to validate the test.
203188
"""
204-
path, is_external, replace = input
189+
path, is_external, replace, popup = input
205190
kwargs = {}
206191
if is_external is not None:
207192
kwargs["is_external"] = is_external
208193
if replace is not None:
209194
kwargs["replace"] = replace
195+
if popup is not None:
196+
kwargs["popup"] = popup
210197
spec = event.redirect(path, **kwargs)
211198
assert isinstance(spec, EventSpec)
212199
assert spec.handler.fn.__qualname__ == "_redirect"

0 commit comments

Comments
 (0)