Skip to content

Multiple event handlers on the same element. #1014

@Sahil-Chhoker

Description

@Sahil-Chhoker

Is there a way to implement multiple event handlers on the same object? Because its not working for me. My code:

def use_event(el: solara.Element, callback: Callable):
    def add_event_handler():
        def on_enter(widget, event, data):
            print("enter pressed")

        def on_up(widget, event, data):
            print("up pressed")

        widget = cast(ipyvue.VueWidget, solara.get_widget(el))
        
        widget.on_event("keyup.enter", on_enter)
        widget.on_event("keyup.38", on_up)

        def cleanup():
            widget.on_event("keyup.enter", on_enter, remove=True)
            widget.on_event("keyup.38", on_up, remove=True)

        return cleanup

    solara.use_effect(add_event_handler, dependencies=[])

input_elem = solara.v.TextField(
        v_model=input_text,
        on_v_model=set_input_text,
        flat=True,
        style_="font-family: monospace;",
        label=">>>",
        outlined=True,
        placeholder="Enter Python code...",
        attributes={"spellcheck": "false"},
    )

    def on_enter(*ignore_args):
        console_ref.current.execute_code(input_text, set_input_text)
        set_refresh(lambda x: x + 1)

    use_event(input_elem, on_enter)

Only the handler that is added afterwards work. Is there a different way to implement this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions