Skip to content

Commit bbfbc82

Browse files
masenfadhami3310
andauthored
Handle Var passed to rx.toast (#4405)
* Handle Var passed to `rx.toast` If the user provides a `Var` for `message` then apply it as `props["title"]` to avoid a var operations error. * remove unnecessary parentheses * remove weird hacks * get it right pyright --------- Co-authored-by: Khaleel Al-Adhami <[email protected]>
1 parent d4b197b commit bbfbc82

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

reflex/components/sonner/toast.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from reflex.utils.serializers import serializer
1616
from reflex.vars import VarData
1717
from reflex.vars.base import LiteralVar, Var
18+
from reflex.vars.function import FunctionVar
19+
from reflex.vars.object import ObjectVar
1820

1921
LiteralPosition = Literal[
2022
"top-left",
@@ -232,7 +234,9 @@ def add_hooks(self) -> list[Var | str]:
232234
return [hook]
233235

234236
@staticmethod
235-
def send_toast(message: str = "", level: str | None = None, **props) -> EventSpec:
237+
def send_toast(
238+
message: str | Var = "", level: str | None = None, **props
239+
) -> EventSpec:
236240
"""Send a toast message.
237241
238242
Args:
@@ -250,20 +254,27 @@ def send_toast(message: str = "", level: str | None = None, **props) -> EventSpe
250254
raise ValueError(
251255
"Toaster component must be created before sending a toast. (use `rx.toast.provider()`)"
252256
)
253-
toast_command = f"{toast_ref}.{level}" if level is not None else toast_ref
254-
if message == "" and ("title" not in props or "description" not in props):
257+
258+
toast_command = (
259+
ObjectVar.__getattr__(toast_ref.to(dict), level) if level else toast_ref
260+
).to(FunctionVar)
261+
262+
if isinstance(message, Var):
263+
props.setdefault("title", message)
264+
message = ""
265+
elif message == "" and "title" not in props and "description" not in props:
255266
raise ValueError("Toast message or title or description must be provided.")
267+
256268
if props:
257-
args = LiteralVar.create(ToastProps(component_name="rx.toast", **props)) # type: ignore
258-
toast = f"{toast_command}(`{message}`, {str(args)})"
269+
args = LiteralVar.create(ToastProps(component_name="rx.toast", **props)) # pyright: ignore [reportCallIssue, reportGeneralTypeIssues]
270+
toast = toast_command.call(message, args)
259271
else:
260-
toast = f"{toast_command}(`{message}`)"
272+
toast = toast_command.call(message)
261273

262-
toast_action = Var(_js_expr=toast)
263-
return run_script(toast_action)
274+
return run_script(toast)
264275

265276
@staticmethod
266-
def toast_info(message: str = "", **kwargs):
277+
def toast_info(message: str | Var = "", **kwargs):
267278
"""Display an info toast message.
268279
269280
Args:
@@ -276,7 +287,7 @@ def toast_info(message: str = "", **kwargs):
276287
return Toaster.send_toast(message, level="info", **kwargs)
277288

278289
@staticmethod
279-
def toast_warning(message: str = "", **kwargs):
290+
def toast_warning(message: str | Var = "", **kwargs):
280291
"""Display a warning toast message.
281292
282293
Args:
@@ -289,7 +300,7 @@ def toast_warning(message: str = "", **kwargs):
289300
return Toaster.send_toast(message, level="warning", **kwargs)
290301

291302
@staticmethod
292-
def toast_error(message: str = "", **kwargs):
303+
def toast_error(message: str | Var = "", **kwargs):
293304
"""Display an error toast message.
294305
295306
Args:
@@ -302,7 +313,7 @@ def toast_error(message: str = "", **kwargs):
302313
return Toaster.send_toast(message, level="error", **kwargs)
303314

304315
@staticmethod
305-
def toast_success(message: str = "", **kwargs):
316+
def toast_success(message: str | Var = "", **kwargs):
306317
"""Display a success toast message.
307318
308319
Args:

reflex/components/sonner/toast.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ class Toaster(Component):
5959
def add_hooks(self) -> list[Var | str]: ...
6060
@staticmethod
6161
def send_toast(
62-
message: str = "", level: str | None = None, **props
62+
message: str | Var = "", level: str | None = None, **props
6363
) -> EventSpec: ...
6464
@staticmethod
65-
def toast_info(message: str = "", **kwargs): ...
65+
def toast_info(message: str | Var = "", **kwargs): ...
6666
@staticmethod
67-
def toast_warning(message: str = "", **kwargs): ...
67+
def toast_warning(message: str | Var = "", **kwargs): ...
6868
@staticmethod
69-
def toast_error(message: str = "", **kwargs): ...
69+
def toast_error(message: str | Var = "", **kwargs): ...
7070
@staticmethod
71-
def toast_success(message: str = "", **kwargs): ...
71+
def toast_success(message: str | Var = "", **kwargs): ...
7272
@staticmethod
7373
def toast_dismiss(id: Var | str | None = None): ...
7474
@overload
@@ -176,7 +176,7 @@ class ToastNamespace(ComponentNamespace):
176176

177177
@staticmethod
178178
def __call__(
179-
message: str = "", level: Optional[str] = None, **props
179+
message: Union[str, Var] = "", level: Optional[str] = None, **props
180180
) -> "Optional[EventSpec]":
181181
"""Send a toast message.
182182

reflex/utils/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ def _isinstance(obj: Any, cls: GenericType, nested: bool = False) -> bool:
576576
return does_obj_satisfy_typed_dict(obj, cls)
577577
return isinstance(obj, dict)
578578

579+
# cls is a float
580+
if cls is float:
581+
return isinstance(obj, (float, int))
582+
579583
# cls is a simple class
580584
return isinstance(obj, cls)
581585

0 commit comments

Comments
 (0)