Skip to content

Commit 799b0cc

Browse files
committed
allow elements with the same key to change type
1 parent 062529d commit 799b0cc

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

docs/source/reference-material/_examples/snake_game.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ def GameView():
1818
game_state, set_game_state = idom.hooks.use_state(GameState.init)
1919

2020
if game_state == GameState.play:
21-
return GameLoop(
22-
grid_size=6,
23-
block_scale=50,
24-
set_game_state=set_game_state,
25-
key="game loop",
26-
)
21+
return GameLoop(grid_size=6, block_scale=50, set_game_state=set_game_state)
2722

2823
start_button = idom.html.button(
2924
{"onClick": lambda event: set_game_state(GameState.play)},
@@ -45,12 +40,7 @@ def GameView():
4540
"""
4641
)
4742

48-
return idom.html.div(
49-
{"className": "snake-game-menu"},
50-
menu_style,
51-
menu,
52-
key="menu",
53-
)
43+
return idom.html.div({"className": "snake-game-menu"}, menu_style, menu)
5444

5545

5646
class Direction(enum.Enum):

src/idom/core/layout.py

+13-20
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ def _render_model_attributes(
271271

272272
model_event_handlers = new_state.model.current["eventHandlers"] = {}
273273
for event, handler in handlers_by_event.items():
274-
target = old_state.targets_by_event.get(
275-
event,
276-
uuid4().hex if handler.target is None else handler.target,
277-
)
274+
if event in old_state.targets_by_event:
275+
target = old_state.targets_by_event[event]
276+
else:
277+
target = uuid4().hex if handler.target is None else handler.target
278278
new_state.targets_by_event[event] = target
279279
self._event_handlers[target] = handler
280280
model_event_handlers[event] = {
@@ -350,6 +350,8 @@ def _render_model_children(
350350
key,
351351
)
352352
else:
353+
if hasattr(old_child_state, "life_cycle_hook"):
354+
old_child_state.life_cycle_state.hook.component_will_unmount()
353355
new_child_state = _update_element_model_state(
354356
old_child_state,
355357
new_state,
@@ -374,6 +376,7 @@ def _render_model_children(
374376
new_state,
375377
index,
376378
child,
379+
self._rendering_queue.put,
377380
)
378381
self._render_component(old_child_state, new_child_state, child)
379382
else:
@@ -459,7 +462,6 @@ def _make_component_model_state(
459462

460463

461464
def _copy_component_model_state(old_model_state: _ModelState) -> _ModelState:
462-
463465
# use try/except here because not having a parent is rare (only the root state)
464466
try:
465467
parent: Optional[_ModelState] = old_model_state.parent
@@ -483,15 +485,8 @@ def _update_component_model_state(
483485
new_parent: _ModelState,
484486
new_index: int,
485487
new_component: ComponentType,
488+
schedule_render: Callable[[_LifeCycleStateId], None],
486489
) -> _ModelState:
487-
try:
488-
old_life_cycle_state = old_model_state.life_cycle_state
489-
except AttributeError:
490-
raise ValueError(
491-
f"Failed to render layout at {old_model_state.patch_path!r} with key "
492-
f"{old_model_state.key!r} - prior element with this key wasn't a component"
493-
)
494-
495490
return _ModelState(
496491
parent=new_parent,
497492
index=new_index,
@@ -500,7 +495,11 @@ def _update_component_model_state(
500495
patch_path=old_model_state.patch_path,
501496
children_by_key={},
502497
targets_by_event={},
503-
life_cycle_state=_update_life_cycle_state(old_life_cycle_state, new_component),
498+
life_cycle_state=(
499+
old_model_state.life_cycle_state
500+
if hasattr(old_model_state, "life_cycle_hook")
501+
else _make_life_cycle_state(new_component, schedule_render)
502+
),
504503
)
505504

506505

@@ -525,12 +524,6 @@ def _update_element_model_state(
525524
new_parent: _ModelState,
526525
new_index: int,
527526
) -> _ModelState:
528-
if hasattr(old_model_state, "life_cycle_state"):
529-
raise ValueError(
530-
f"Failed to render layout at {old_model_state.patch_path!r} with key "
531-
f"{old_model_state.key!r} - prior element with this key was a component"
532-
)
533-
534527
return _ModelState(
535528
parent=new_parent,
536529
index=new_index,

0 commit comments

Comments
 (0)