Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support keys in HTML fragments #683

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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