From 09828f49ba762f724466e282f745f4c2da4a24f5 Mon Sep 17 00:00:00 2001 From: rmorshea Date: Sun, 20 Feb 2022 12:26:12 -0800 Subject: [PATCH] support keys in HTML fragments --- src/idom/html.py | 14 +++++++++++--- tests/test_html.py | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/idom/html.py b/src/idom/html.py index ace4dc862..4865dc7e5 100644 --- a/src/idom/html.py +++ b/src/idom/html.py @@ -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 coalesced_children: + model["children"] = coalesced_children + + if key is not None: + model["key"] = key + + return model # Dcument metadata diff --git a/tests/test_html.py b/tests/test_html.py index 976daf419..cc6521efa 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -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():