-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement on_mount
and on_unmount
for all components.
#1636
Changes from all commits
fe37d2c
596d671
144b5a9
551a43f
7c5d6d7
9fb29fa
b77fdf0
682c247
5959362
86ef177
eb49f04
f416413
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,11 @@ | |
|
||
from reflex import constants | ||
from reflex.utils import types | ||
from reflex.vars import Var | ||
|
||
if TYPE_CHECKING: | ||
from reflex.components.component import ComponentStyle | ||
from reflex.event import EventHandler, EventSpec | ||
from reflex.event import EventChain, EventHandler, EventSpec | ||
|
||
WRAP_MAP = { | ||
"{": "}", | ||
|
@@ -182,6 +183,24 @@ def format_string(string: str) -> str: | |
return string | ||
|
||
|
||
def format_var(var: Var) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be the same as the Var.str method? Should we move this code there? This was the intention of that method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, it's a little different. i cribbed this from |
||
"""Format the given Var as a javascript value. | ||
|
||
Args: | ||
var: The Var to format. | ||
|
||
Returns: | ||
The formatted Var. | ||
""" | ||
if not var.is_local or var.is_string: | ||
return str(var) | ||
if types._issubclass(var.type_, str): | ||
return format_string(var.full_name) | ||
if is_wrapped(var.full_name, "{"): | ||
return var.full_name | ||
return json_dumps(var.full_name) | ||
|
||
|
||
def format_route(route: str) -> str: | ||
"""Format the given route. | ||
|
||
|
@@ -311,6 +330,46 @@ def format_event(event_spec: EventSpec) -> str: | |
return f"E({', '.join(event_args)})" | ||
|
||
|
||
def format_event_chain( | ||
event_chain: EventChain | Var[EventChain], | ||
event_arg: Var | None = None, | ||
) -> str: | ||
"""Format an event chain as a javascript invocation. | ||
|
||
Args: | ||
event_chain: The event chain to queue on the frontend. | ||
event_arg: The browser-native event (only used to preventDefault). | ||
|
||
Returns: | ||
Compiled javascript code to queue the given event chain on the frontend. | ||
|
||
Raises: | ||
ValueError: When the given event chain is not a valid event chain. | ||
""" | ||
if isinstance(event_chain, Var): | ||
from reflex.event import EventChain | ||
|
||
if event_chain.type_ is not EventChain: | ||
raise ValueError(f"Invalid event chain: {event_chain}") | ||
return "".join( | ||
[ | ||
"(() => {", | ||
format_var(event_chain), | ||
f"; preventDefault({format_var(event_arg)})" if event_arg else "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we're formatting an arbitrary var as an event chain, there's no guarantee that |
||
"})()", | ||
] | ||
) | ||
|
||
chain = ",".join([format_event(event) for event in event_chain.events]) | ||
return "".join( | ||
[ | ||
f"Event([{chain}]", | ||
f", {format_var(event_arg)}" if event_arg else "", | ||
")", | ||
] | ||
) | ||
|
||
|
||
def format_query_params(router_data: Dict[str, Any]) -> Dict[str, str]: | ||
"""Convert back query params name to python-friendly case. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we just add these to the default
EVENT_TRIGGERS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i went back and forth on this, and ultimately did it this way because these aren't "real" event triggers that come from browser events, but are synthetic events that reflex handles.