Skip to content

Commit 94d006c

Browse files
committed
modules with mount func should not have children
1 parent e7c11d0 commit 94d006c

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

Diff for: src/idom/client/app/packages/idom-client-react/src/index.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function ImportedElement({ model }) {
6666
model.importSource.source
6767
);
6868
eval(`import("${importSource}")`).then((module) => {
69-
mountImportSource(module, mountPoint.current, model, config);
69+
mountImportSource(mountPoint.current, module, model, config);
7070
});
7171
});
7272
return html`<div ref=${mountPoint} />`;
@@ -137,21 +137,26 @@ function eventHandler(sendEvent, eventSpec) {
137137
};
138138
}
139139

140-
function mountImportSource(module, mountPoint, model, config) {
141-
const mountFunction = model.importSource.hasMount
142-
? module.mount
143-
: (element, component, props, children) =>
144-
reactDOM.render(
145-
react.createElement(component, props, ...children),
146-
element
147-
);
148-
149-
const component = module[model.tagName];
150-
const children = elementChildren(model);
151-
const attributes = elementAttributes(model, config.sendEvent);
152-
const props = { key: model.key, ...attributes };
153-
154-
mountFunction(mountPoint, component, props, children);
140+
function mountImportSource(element, module, model, config) {
141+
if (model.importSource.hasMount) {
142+
if (model.children) {
143+
console.error("Mount function does not support children");
144+
}
145+
module.mount(
146+
element,
147+
module[model.tagName],
148+
elementAttributes(model, config.sendEvent)
149+
);
150+
} else {
151+
reactDOM.render(
152+
react.createElement(
153+
module[model.tagName],
154+
elementAttributes(model, config.sendEvent),
155+
...elementChildren(model)
156+
),
157+
element
158+
);
159+
}
155160
}
156161

157162
function useInplaceJsonPatch(doc) {

Diff for: src/idom/client/module.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __init__(
129129
def declare(
130130
self,
131131
name: str,
132-
has_children: bool = True,
132+
has_children: Optional[bool] = None,
133133
fallback: Optional[str] = None,
134134
) -> Import:
135135
"""Return an :class:`Import` for the given :class:`Module` and ``name``
@@ -147,10 +147,11 @@ def declare(
147147
raise ValueError(
148148
f"{self} does not export {name!r}, available options are {list(self.exports)}"
149149
)
150+
150151
return Import(
151152
self.url,
152153
name,
153-
has_children,
154+
has_children=has_children,
154155
has_mount=self.has_mount,
155156
fallback=fallback or self.fallback,
156157
)
@@ -183,7 +184,7 @@ def __init__(
183184
self,
184185
module: str,
185186
name: str,
186-
has_children: bool = True,
187+
has_children: Optional[bool] = None,
187188
has_mount: bool = False,
188189
fallback: Optional[str] = None,
189190
) -> None:
@@ -192,6 +193,16 @@ def __init__(
192193
f"{IDOM_CLIENT_MODULES_MUST_HAVE_MOUNT} is set and {module} has no mount"
193194
)
194195

196+
if has_mount:
197+
if has_children is True:
198+
raise ValueError(
199+
f"Components of {module!r} do not support "
200+
"children because has_mount=True"
201+
)
202+
has_children = False
203+
else:
204+
has_children = bool(has_children)
205+
195206
self._name = name
196207
self._constructor = make_vdom_constructor(name, has_children)
197208
self._import_source = ImportSourceDict(

Diff for: tests/test_client/js/vanilla-js-component.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export function mount(element, component, props, children) {
2-
component(element, props, children);
1+
export function mount(element, component, props) {
2+
component(element, props);
33
}
44

5-
export function SetInnerHtml(element, props, children) {
5+
export function SetInnerHtml(element, props) {
66
element.innerHTML = props.innerHTML;
77
}

Diff for: tests/test_client/test_module.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ def test_idom_client_modules_must_have_mount():
9595
IDOM_CLIENT_MODULES_MUST_HAVE_MOUNT.current = True
9696
try:
9797
with pytest.raises(RuntimeError, match="has no mount"):
98-
idom.Module("https://some.url", has_mount=False).SomeComponent
98+
idom.Import(
99+
"https://some.url",
100+
"SomeComponent",
101+
has_mount=False,
102+
)
99103
finally:
100104
IDOM_CLIENT_MODULES_MUST_HAVE_MOUNT.current = old_opt
105+
106+
107+
def test_no_children_if_import_has_mount():
108+
with pytest.raises(ValueError, match="do not support children"):
109+
idom.Import(
110+
"https://some.url",
111+
"SomeComponent",
112+
has_children=True,
113+
has_mount=True,
114+
fallback=None,
115+
)

0 commit comments

Comments
 (0)