Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"babel-plugin-import-map": "^1.0.0",
"es-module-shims": "^2.0.0",
"esbuild": "^0.17.14",
"lodash": "^4.17.21",
"raw-loader": "^4.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
19 changes: 19 additions & 0 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { transform } from "sucrase";
import { ErrorBoundary, JupyterWidget } from "./components";
import { Root } from "react-dom/client";
import { ModelDestroyOptions } from "backbone";
import { isEqual } from "lodash";

declare function importShim<Default, Exports extends object>(
specifier: string,
Expand Down Expand Up @@ -728,6 +729,24 @@ export class ReactModel extends DOMWidgetModel {
}
this.listenTo(this, `change:${key}`, updateChildren);
}
// If props or children were updated while we were initializing the view,
// we want to do a rerender
const checkPropsChange = async () => {
const [currentProps, currentChildren] = await Promise.all([
replaceWidgetWithComponent(this.get("props"), get_model),
replaceWidgetWithComponent(
{ children: this.get("children") },
get_model,
),
]);
if (!isEqual(currentProps, initialModelProps)) {
updateModelProps();
}
if (!isEqual(currentChildren, initialChildrenComponents)) {
updateChildren();
}
};
this.enqueue(checkPropsChange);
return () => {
this.stopListening(this, "change:props", updateModelProps);
this.stopListening(this, "change:children", updateChildren);
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/children_test.py → tests/ui/basics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,27 @@ def on_click(event_data):
button.click()
# not per se a direct child
page_session.locator(".test-button >> .test-html").wait_for()


def test_update_children_after_create(solara_test, page_session: playwright.sync_api.Page):
b = ipyreact.Widget(
_type="button",
children=["Initial"],
props={"class": "test-button"},
)
b.children = ["Updated"]

display(b)
page_session.locator(".test-button >> text=Updated").wait_for()


def test_update_props_after_create(solara_test, page_session: playwright.sync_api.Page):
b = ipyreact.Widget(
_type="button",
children=["Button"],
props={"class": "test-button-initial"},
)
b.props = {"class": "test-button-updated"}

display(b)
page_session.locator(".test-button-updated >> text=Button").wait_for()
Loading