Skip to content

Commit

Permalink
support keys in HTML fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Feb 20, 2022
1 parent 182317a commit a5d44de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/idom/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,24 @@

from typing import Any, Mapping

from .core.proto import VdomDict
from .core.proto import Key, VdomDict
from .core.vdom import coalesce_attributes_and_children, make_vdom_constructor


def _(*children: Any) -> VdomDict:
def _(*children: Any, key: Key | None = None) -> VdomDict:
"""An HTML fragment - this element will not appear in the DOM"""
attributes, coalesced_children = coalesce_attributes_and_children(children)
if attributes:
raise TypeError("Fragments cannot have attributes")
return {"tagName": "", "children": coalesced_children}
model: VdomDict = {"tagName": ""}

if not coalesced_children:
model["children"] = coalesced_children

if key is not None:
model["key"] = key

return model


# Dcument metadata
Expand Down
7 changes: 7 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ def test_child_of_script_must_be_string():


def test_simple_fragment():
assert html._() == {"tagName": ""}
assert html._(1, 2, 3) == {"tagName": "", "children": [1, 2, 3]}
assert html._(key="something") == {"tagName": "", "key": "something"}
assert html._(1, 2, 3, key="something") == {
"tagName": "",
"key": "something",
"children": [1, 2, 3],
}


def test_fragment_can_have_no_attributes():
Expand Down

0 comments on commit a5d44de

Please sign in to comment.