From 78d6a561fa1938d4307eaef5a849c652c1db7af3 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Wed, 6 Sep 2023 09:51:45 +0200 Subject: [PATCH] fix: non Event objects do not need processing before serialization 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. --- js/src/VueRenderer.js | 6 +++--- tests/ui/test_template.py | 40 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/js/src/VueRenderer.js b/js/src/VueRenderer.js index 2971262..aff2355 100644 --- a/js/src/VueRenderer.js +++ b/js/src/VueRenderer.js @@ -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) { diff --git a/tests/ui/test_template.py b/tests/ui/test_template.py index 085d4ba..baaed19 100644 --- a/tests/ui/test_template.py +++ b/tests/ui/test_template.py @@ -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): @@ -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 """ + + """ + + 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"