Skip to content

Commit b936543

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 b936543

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

idom/core/layout.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,29 +154,38 @@ def _render_element(self, element_state: ElementState) -> Dict[str, Any]:
154154
return element_state.model
155155

156156
def _render_model(
157-
self, element_state: ElementState, model: Mapping[str, Any]
157+
self,
158+
element_state: ElementState,
159+
model: Mapping[str, Any],
160+
path: str = "",
158161
) -> Dict[str, Any]:
159162
serialized_model: Dict[str, Any] = {}
160163
event_handlers = self._render_model_event_targets(element_state, model)
161164
if event_handlers:
162165
serialized_model["eventHandlers"] = event_handlers
163166
if "children" in model:
164167
serialized_model["children"] = self._render_model_children(
165-
element_state, model["children"]
168+
element_state, model["children"], path
166169
)
167170
return {**model, **serialized_model}
168171

169172
def _render_model_children(
170-
self, element_state: ElementState, children: Union[List[Any], Tuple[Any, ...]]
173+
self,
174+
element_state: ElementState,
175+
children: Union[List[Any], Tuple[Any, ...]],
176+
path: str,
171177
) -> List[Any]:
172178
resolved_children: List[Any] = []
173179
for index, child in enumerate(
174180
children if isinstance(children, (list, tuple)) else [children]
175181
):
176182
if isinstance(child, dict):
177-
resolved_children.append(self._render_model(element_state, child))
183+
child_path = f"{path}/children/{index}"
184+
resolved_children.append(
185+
self._render_model(element_state, child, child_path)
186+
)
178187
elif isinstance(child, AbstractElement):
179-
child_path = f"{element_state.path}/children/{index}"
188+
child_path = f"{path}/children/{index}"
180189
child_state = self._create_element_state(child, child_path, save=True)
181190
resolved_children.append(self._render_element(child_state))
182191
element_state.child_elements_ids.append(id(child))

tests/test_core/test_layout.py

Lines changed: 21 additions & 0 deletions
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)