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

Move _create_event_chain to EventChain.create #4557

Merged
merged 1 commit into from
Jan 2, 2025
Merged

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Dec 19, 2024

The ability to convert EventType arguments into EventChain is crucial for wrapping JS libraries that need to pass event handlers via hooks or other non-component centric interfaces.

Improve typing such that wrapped components accepting EventType arguments can be directly converted to EventChain / EventChainVar to be rendered as JS code.


import reflex as rx
from reflex.event import BASE_STATE


class GoogleAuthState(rx.State):
    def on_success(self, data: dict):
        print(data)


def _on_success_signature(data: rx.Var[dict]) -> tuple[rx.Var[dict]]:
    return (data,)


def handle_google_login(
    on_success: EventType[[dict], BASE_STATE] = GoogleAuthState.on_success,
) -> rx.Var[rx.EventChain]:
    on_success_event_chain = rx.Var.create(
        rx.EventChain.create(
            value=on_success,
            args_spec=_on_success_signature,
            key="on_success",
        )
    )
    return rx.Var(
        "() => login()",
        _var_type=rx.EventChain,
        _var_data=rx.vars.VarData(
            hooks={
                """
const login = useGoogleLogin({
  onSuccess: %s,
  flow: 'auth-code',
});"""
                % on_success_event_chain: None,
            },
            imports={"@react-oauth/google": "useGoogleLogin"},
        ).merge(on_success_event_chain._get_all_var_data()),
    )

The ability to convert `EventType` arguments into `EventChain` is crucial for
wrapping JS libraries that need to pass event handlers via hooks or
other non-component centric interfaces.

Improve typing such that wrapped components accepting `EventType` arguments can
be directly converted to `EventChain` / `EventChainVar` to be rendered as JS
code.
@masenf masenf merged commit 41ed9f0 into main Jan 2, 2025
41 checks passed
masenf added a commit that referenced this pull request Jan 2, 2025
The ability to convert `EventType` arguments into `EventChain` is crucial for
wrapping JS libraries that need to pass event handlers via hooks or
other non-component centric interfaces.

Improve typing such that wrapped components accepting `EventType` arguments can
be directly converted to `EventChain` / `EventChainVar` to be rendered as JS
code.
@masenf masenf deleted the masenf/eventchain-create branch January 2, 2025 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants