Skip to content

Commit fe56b77

Browse files
committed
fixes: #246
The layout was not producing the correct path in its update for element which were not direct children of eachother.
1 parent d67e2be commit fe56b77

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Diff for: idom/core/layout.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Tuple,
77
Mapping,
88
NamedTuple,
9+
Optional,
910
Any,
1011
Set,
1112
Iterator,
@@ -154,29 +155,41 @@ def _render_element(self, element_state: ElementState) -> Dict[str, Any]:
154155
return element_state.model
155156

156157
def _render_model(
157-
self, element_state: ElementState, model: Mapping[str, Any]
158+
self,
159+
element_state: ElementState,
160+
model: Mapping[str, Any],
161+
path: Optional[str] = None,
158162
) -> Dict[str, Any]:
163+
if path is None:
164+
path = element_state.path
165+
159166
serialized_model: Dict[str, Any] = {}
160167
event_handlers = self._render_model_event_targets(element_state, model)
161168
if event_handlers:
162169
serialized_model["eventHandlers"] = event_handlers
163170
if "children" in model:
164171
serialized_model["children"] = self._render_model_children(
165-
element_state, model["children"]
172+
element_state, model["children"], path
166173
)
167174
return {**model, **serialized_model}
168175

169176
def _render_model_children(
170-
self, element_state: ElementState, children: Union[List[Any], Tuple[Any, ...]]
177+
self,
178+
element_state: ElementState,
179+
children: Union[List[Any], Tuple[Any, ...]],
180+
path: str,
171181
) -> List[Any]:
172182
resolved_children: List[Any] = []
173183
for index, child in enumerate(
174184
children if isinstance(children, (list, tuple)) else [children]
175185
):
176186
if isinstance(child, dict):
177-
resolved_children.append(self._render_model(element_state, child))
187+
child_path = f"{path}/children/{index}"
188+
resolved_children.append(
189+
self._render_model(element_state, child, child_path)
190+
)
178191
elif isinstance(child, AbstractElement):
179-
child_path = f"{element_state.path}/children/{index}"
192+
child_path = f"{path}/children/{index}"
180193
child_state = self._create_element_state(child, child_path, save=True)
181194
resolved_children.append(self._render_element(child_state))
182195
element_state.child_elements_ids.append(id(child))

Diff for: tests/test_core/test_layout.py

+21
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,24 @@ def AnElement():
248248
pass # the render should still be rendering since we only update once
249249

250250
assert run_count.current == 2
251+
252+
253+
async def test_update_path_to_element_that_is_not_direct_child_is_correct():
254+
hook = HookCatcher()
255+
256+
@idom.element
257+
def Parent():
258+
return idom.html.div(idom.html.div(Child()))
259+
260+
@idom.element
261+
@hook.capture
262+
def Child():
263+
return idom.html.div()
264+
265+
async with idom.Layout(Parent()) as layout:
266+
await layout.render()
267+
268+
hook.current.schedule_render()
269+
270+
update = await layout.render()
271+
assert update.path == "/children/0/children/0"

0 commit comments

Comments
 (0)