Skip to content

Commit

Permalink
fix: non Event objects do not need processing before serialization
Browse files Browse the repository at this point in the history
If the event object was a string, we would call pickSerializable
on it which would convert it to an object, which results in a dict
on the Python side.
  • Loading branch information
maartenbreddels committed Sep 6, 2023
1 parent fa1668c commit 78d6a56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
6 changes: 3 additions & 3 deletions js/src/VueRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ function pickSerializable(object, depth=0, max_depth=2) {
}

export function eventToObject(event) {
if (event == null) {
return event;
if (event instanceof Event) {
return pickSerializable(event);
}
return pickSerializable(event);
return event;
}

export function vueRender(createElement, model, parentView, slotScopes) {
Expand Down
40 changes: 39 additions & 1 deletion tests/ui/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import playwright.sync_api

from IPython.display import display
from traitlets import default, Int
from traitlets import default, Int, Callable, Unicode


class MyTemplate(vue.VueTemplate):
Expand Down Expand Up @@ -37,3 +37,41 @@ def kernel_code():
widget.wait_for()
widget.click()
page_session.locator("text=Clicked 1").wait_for()


class MyEventTemplate(vue.VueTemplate):
on_custom = Callable()
text = Unicode("Click Me").tag(sync=True)

@default("template")
def _default_vue_template(self):
return """
<template>
<div @click="custom_event('not-an-event-object')">{{text}}</div>
</template>
"""

def vue_custom_event(self, data):
self.on_custom(data)


def test_template_custom_event(solara_test, page_session: playwright.sync_api.Page):
last_event_data = None

def on_custom(data):
print()
nonlocal last_event_data
last_event_data = data
div.text = "Clicked"

div = MyEventTemplate(on_custom=on_custom)

display(div)

# click in the div
box = page_session.locator("text=Click Me").bounding_box()
assert box is not None
page_session.mouse.click(box["x"], box["y"])

page_session.locator("text=Clicked").wait_for()
assert last_event_data == "not-an-event-object"

0 comments on commit 78d6a56

Please sign in to comment.