Skip to content

Commit ee51f67

Browse files
committed
Infer key from attributes["key"]
1 parent af68f0e commit ee51f67

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/reactpy/transforms.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,18 @@ def input_element_value_prop_to_defaultValue(vdom: VdomDict) -> None:
9898
def infer_key_from_attributes(vdom: VdomDict) -> None:
9999
"""Infer the ReactJS `key` by looking at any attributes that should be unique."""
100100
attributes = vdom.get("attributes", {})
101+
if not attributes:
102+
return
103+
104+
# Infer 'key' from 'attributes.key'
105+
key = attributes.pop("key", None)
101106

102-
# Infer 'key' from 'id'
107+
# Infer 'key' from 'attributes.id'
108+
if key is None:
103109
key = attributes.get("id")
104110

105-
# Fallback: Infer 'key' from 'name'
106-
if not key and vdom["tagName"] in {"input", "select", "textarea"}:
111+
# Infer 'key' from 'attributes.name'
112+
if key is None and vdom["tagName"] in {"input", "select", "textarea"}:
107113
key = attributes.get("name")
108114

109115
if key:

tests/test_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def test_html_to_vdom(case):
185185
"attributes": {"type": "text", "name": "my-input"},
186186
},
187187
},
188+
# 8: Infer ReactJS `key` from the `key` attribute
189+
{
190+
"source": '<div key="my-key"></div>',
191+
"model": {"tagName": "div", "key": "my-key"},
192+
},
188193
],
189194
)
190195
def test_html_to_vdom_default_transforms(case):

0 commit comments

Comments
 (0)