Skip to content

Commit ccd7625

Browse files
committed
support mappings in vdom() constructor
1 parent 4a3d960 commit ccd7625

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

idom/core/vdom.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VdomDict(_VdomDictRequired, _VdomDictOptional):
2727

2828

2929
_TagArg = str
30-
_AttributesAndChildrenArg = Union[Mapping[str, Any], Iterable[Any]]
30+
_AttributesAndChildrenArg = Union[Mapping[str, Any], str, Iterable[Any], Any]
3131
_EventHandlersArg = Optional[EventsMapping]
3232
_ImportSourceArg = Optional[ImportSourceDict]
3333

@@ -59,11 +59,15 @@ def vdom(
5959

6060
began_children = False
6161
for argument in attributes_and_children:
62-
if isinstance(argument, dict) and "tagName" not in argument:
63-
if began_children:
64-
raise ValueError("Attribute dictionaries should precede child lists.")
65-
attributes.update(argument)
66-
elif isinstance(argument, (list, tuple)):
62+
if isinstance(argument, Mapping):
63+
if "tagName" not in argument:
64+
if began_children:
65+
raise ValueError("Attribute dictionaries should precede children.")
66+
attributes.update(argument)
67+
else:
68+
children.append(argument)
69+
began_children = True
70+
elif not isinstance(argument, str) and isinstance(argument, Iterable):
6771
children.extend(argument)
6872
began_children = True
6973
else:

tests/test_core/test_vdom.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ async def handler(event):
6868
idom.vdom("div", {"tagName": "div"}),
6969
{"tagName": "div", "children": [{"tagName": "div"}]},
7070
),
71+
(
72+
idom.vdom("div", (i for i in range(3))),
73+
{"tagName": "div", "children": [0, 1, 2]},
74+
),
75+
(
76+
idom.vdom("div", map(lambda x: x ** 2, [1, 2, 3])),
77+
{"tagName": "div", "children": [1, 4, 9]},
78+
),
7179
],
7280
)
7381
def test_simple_node_construction(actual, expected):

0 commit comments

Comments
 (0)