Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Var passed to rx.toast #4405

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 23 additions & 12 deletions reflex/components/sonner/toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from reflex.utils.serializers import serializer
from reflex.vars import VarData
from reflex.vars.base import LiteralVar, Var
from reflex.vars.function import FunctionVar
from reflex.vars.object import ObjectVar

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

@staticmethod
def send_toast(message: str = "", level: str | None = None, **props) -> EventSpec:
def send_toast(
message: str | Var = "", level: str | None = None, **props
) -> EventSpec:
"""Send a toast message.

Args:
Expand All @@ -250,20 +254,27 @@ def send_toast(message: str = "", level: str | None = None, **props) -> EventSpe
raise ValueError(
"Toaster component must be created before sending a toast. (use `rx.toast.provider()`)"
)
toast_command = f"{toast_ref}.{level}" if level is not None else toast_ref
if message == "" and ("title" not in props or "description" not in props):

toast_command = (
ObjectVar.__getattr__(toast_ref.to(dict), level) if level else toast_ref
).to(FunctionVar)

if isinstance(message, Var):
props.setdefault("title", message)
message = ""
elif message == "" and "title" not in props and "description" not in props:
raise ValueError("Toast message or title or description must be provided.")

if props:
args = LiteralVar.create(ToastProps(component_name="rx.toast", **props)) # type: ignore
toast = f"{toast_command}(`{message}`, {str(args)})"
args = LiteralVar.create(ToastProps(component_name="rx.toast", **props)) # pyright: ignore [reportCallIssue]
toast = toast_command.call(message, args)
else:
toast = f"{toast_command}(`{message}`)"
toast = toast_command.call(message)

toast_action = Var(_js_expr=toast)
return run_script(toast_action)
return run_script(toast)

@staticmethod
def toast_info(message: str = "", **kwargs):
def toast_info(message: str | Var = "", **kwargs):
"""Display an info toast message.

Args:
Expand All @@ -276,7 +287,7 @@ def toast_info(message: str = "", **kwargs):
return Toaster.send_toast(message, level="info", **kwargs)

@staticmethod
def toast_warning(message: str = "", **kwargs):
def toast_warning(message: str | Var = "", **kwargs):
"""Display a warning toast message.

Args:
Expand All @@ -289,7 +300,7 @@ def toast_warning(message: str = "", **kwargs):
return Toaster.send_toast(message, level="warning", **kwargs)

@staticmethod
def toast_error(message: str = "", **kwargs):
def toast_error(message: str | Var = "", **kwargs):
"""Display an error toast message.

Args:
Expand All @@ -302,7 +313,7 @@ def toast_error(message: str = "", **kwargs):
return Toaster.send_toast(message, level="error", **kwargs)

@staticmethod
def toast_success(message: str = "", **kwargs):
def toast_success(message: str | Var = "", **kwargs):
"""Display a success toast message.

Args:
Expand Down
12 changes: 6 additions & 6 deletions reflex/components/sonner/toast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ class Toaster(Component):
def add_hooks(self) -> list[Var | str]: ...
@staticmethod
def send_toast(
message: str = "", level: str | None = None, **props
message: str | Var = "", level: str | None = None, **props
) -> EventSpec: ...
@staticmethod
def toast_info(message: str = "", **kwargs): ...
def toast_info(message: str | Var = "", **kwargs): ...
@staticmethod
def toast_warning(message: str = "", **kwargs): ...
def toast_warning(message: str | Var = "", **kwargs): ...
@staticmethod
def toast_error(message: str = "", **kwargs): ...
def toast_error(message: str | Var = "", **kwargs): ...
@staticmethod
def toast_success(message: str = "", **kwargs): ...
def toast_success(message: str | Var = "", **kwargs): ...
@staticmethod
def toast_dismiss(id: Var | str | None = None): ...
@overload
Expand Down Expand Up @@ -176,7 +176,7 @@ class ToastNamespace(ComponentNamespace):

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

Expand Down
4 changes: 4 additions & 0 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ def _isinstance(obj: Any, cls: GenericType, nested: bool = False) -> bool:
return does_obj_satisfy_typed_dict(obj, cls)
return isinstance(obj, dict)

# cls is a float
if cls is float:
return isinstance(obj, (float, int))
adhami3310 marked this conversation as resolved.
Show resolved Hide resolved

# cls is a simple class
return isinstance(obj, cls)

Expand Down
Loading