reflex/reflex/components/props.py
Masen Furer 76c8b2dfbd
Get action, cancel, on_dismiss and on_auto_close working for rx.toast (#3216)
* Get `action` and `cancel` working for rx.toast

Respect defaults set in ToastProvider toast_options when firing a toast with
it's own ToastProps set.

* Update reflex/components/sonner/toast.py

Co-authored-by: Thomas Brandého <thomas.brandeho@gmail.com>

* Move queueEvent formatting into rx.utils.format module

Implement on_auto_close and on_dismiss callbacks inside ToastProps

* Update rx.call_script to use new format.format_queue_events

Replace duplicate logic in rx.call_script for handling the callback function.

* Move PropsBase to reflex.components.props

This base class will be exposed via rx._x.PropsBase and can be shared by other
wrapped components that need to pass a JS object full of extra props.

---------

Co-authored-by: Thomas Brandého <thomas.brandeho@gmail.com>
2024-05-15 11:54:52 +02:00

31 lines
906 B
Python

"""A class that holds props to be passed or applied to a component."""
from __future__ import annotations
from reflex.base import Base
from reflex.utils import format
from reflex.utils.serializers import serialize
class PropsBase(Base):
"""Base for a class containing props that can be serialized as a JS object."""
def json(self) -> str:
"""Convert the object to a json-like string.
Vars will be unwrapped so they can represent actual JS var names and functions.
Keys will be converted to camelCase.
Returns:
The object as a Javascript Object literal.
"""
return format.unwrap_vars(
self.__config__.json_dumps(
{
format.to_camel_case(key): value
for key, value in self.dict().items()
},
default=serialize,
)
)