Skip to content

Commit e6187f4

Browse files
Fix name clash in v-for directives
1 parent ea06475 commit e6187f4

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

collagraph/sfc/compiler.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,18 @@ def create_children(
675675
if parent_node.tag and parent_node.tag[0].isupper():
676676
attributes.append(ast_set_slot_name(el, "default"))
677677

678+
# Check if this tag is a loop variable (from v-for)
679+
# Loop variables should not be treated as components
680+
is_loop_variable = any(
681+
child.tag in loop_vars
682+
for loop_dict in list_names
683+
for loop_vars in loop_dict.values()
684+
)
685+
678686
is_component = (
679-
child.tag in names or child.tag[0].isupper() or "." in child.tag
687+
(child.tag in names and not is_loop_variable)
688+
or child.tag[0].isupper()
689+
or "." in child.tag
680690
)
681691
result.append(
682692
ast_create_fragment(

tests/test_directive_for.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,32 @@ def on_button(self, value):
698698
handler()
699699

700700
assert calls == ["first", "second"]
701+
702+
703+
def test_for_context_and_naming(parse_source):
704+
"""
705+
Make sure that using a loop variable can have the same
706+
name as an element.
707+
"""
708+
Items, _ = parse_source(
709+
"""
710+
<item
711+
v-for="item in items"
712+
:value="item['value']"
713+
/>
714+
<script>
715+
import collagraph as cg
716+
class Items(cg.Component):
717+
pass
718+
</script>
719+
"""
720+
)
721+
state = reactive({"items": [{"value": "a"}, {"value": "b"}]})
722+
723+
gui = Collagraph(DictRenderer())
724+
container = {"type": "root"}
725+
gui.render(Items, container, state)
726+
727+
first_item, second_item = container["children"]
728+
assert first_item["attrs"]["value"] == "a"
729+
assert second_item["attrs"]["value"] == "b"

0 commit comments

Comments
 (0)