Skip to content

Commit 545f2f1

Browse files
authored
support keys in HTML fragments (#683)
1 parent 182317a commit 545f2f1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/idom/html.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,24 @@
160160

161161
from typing import Any, Mapping
162162

163-
from .core.proto import VdomDict
163+
from .core.proto import Key, VdomDict
164164
from .core.vdom import coalesce_attributes_and_children, make_vdom_constructor
165165

166166

167-
def _(*children: Any) -> VdomDict:
167+
def _(*children: Any, key: Key | None = None) -> VdomDict:
168168
"""An HTML fragment - this element will not appear in the DOM"""
169169
attributes, coalesced_children = coalesce_attributes_and_children(children)
170170
if attributes:
171171
raise TypeError("Fragments cannot have attributes")
172-
return {"tagName": "", "children": coalesced_children}
172+
model: VdomDict = {"tagName": ""}
173+
174+
if coalesced_children:
175+
model["children"] = coalesced_children
176+
177+
if key is not None:
178+
model["key"] = key
179+
180+
return model
173181

174182

175183
# Dcument metadata

tests/test_html.py

+7
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,14 @@ def test_child_of_script_must_be_string():
145145

146146

147147
def test_simple_fragment():
148+
assert html._() == {"tagName": ""}
148149
assert html._(1, 2, 3) == {"tagName": "", "children": [1, 2, 3]}
150+
assert html._(key="something") == {"tagName": "", "key": "something"}
151+
assert html._(1, 2, 3, key="something") == {
152+
"tagName": "",
153+
"key": "something",
154+
"children": [1, 2, 3],
155+
}
149156

150157

151158
def test_fragment_can_have_no_attributes():

0 commit comments

Comments
 (0)