Skip to content

Commit 2737ce5

Browse files
committed
reactpy.html(...) to be shorthand notation for fragments
1 parent a1746f3 commit 2737ce5

File tree

8 files changed

+17
-16
lines changed

8 files changed

+17
-16
lines changed

docs/source/about/changelog.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Unreleased
7979
- :pull:`1311` - Removed deprecated exception type ``reactpy.core.serve.Stop``.
8080
- :pull:`1311` - Removed deprecated component ``reactpy.widgets.hotswap``.
8181
- :pull:`1255` - Removed ``reactpy.sample`` module.
82-
- :pull:`1255` - Removed ``reactpy.svg`` module. Contents previously within ``reactpy.svg.*`` can now be accessed via ``html.svg.*``.
83-
- :pull:`1255` - Removed ``reactpy.html._`` function. Use ``html.fragment`` instead.
82+
- :pull:`1255` - Removed ``reactpy.svg`` module. Contents previously within ``reactpy.svg.*`` can now be accessed via ``reactpy.html.svg.*``.
83+
- :pull:`1255` - Removed ``reactpy.html._`` function. Use ``reactpy.html(...)`` or ``reactpy.html.fragment(...)`` instead.
8484
- :pull:`1113` - Removed ``reactpy.run``. See the documentation for the new method to run ReactPy applications.
8585
- :pull:`1113` - Removed ``reactpy.backend.*``. See the documentation for the new method to run ReactPy applications.
8686
- :pull:`1113` - Removed ``reactpy.core.types`` module. Use ``reactpy.types`` instead.

src/reactpy/_html.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ class HtmlConstructor:
287287
"fragment": Vdom("", custom_constructor=_fragment),
288288
"svg": SvgConstructor(),
289289
}
290+
__call__ = __cache__["fragment"].__call__
290291

291292
def __getattr__(self, value: str) -> VdomConstructor:
292293
value = value.rstrip("_").replace("_", "-")

src/reactpy/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def component_to_vdom(component: Component) -> VdomDict:
242242
return component_to_vdom(cast(Component, result))
243243
elif isinstance(result, str):
244244
return html.div(result)
245-
return html.fragment()
245+
return html()
246246

247247

248248
def _react_attribute_to_html(key: str, value: Any) -> tuple[str, str]:

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def test_automatic_reconnect(
1717
@reactpy.component
1818
def SomeComponent():
1919
count, incr_count = use_counter(0)
20-
return reactpy.html.fragment(
20+
return reactpy.html(
2121
reactpy.html.p({"data-count": count, "id": "count"}, "count", count),
2222
reactpy.html.button(
2323
{"onClick": lambda e: incr_count(), "id": "incr"}, "incr"

tests/test_core/test_hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ async def test_context_values_are_scoped():
981981

982982
@reactpy.component
983983
def Parent():
984-
return html.fragment(
984+
return html(
985985
Context(Context(Child1(), value=1), value="something-else"),
986986
Context(Child2(), value=2),
987987
)

tests/test_core/test_layout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def colorize(event):
12251225
@component
12261226
def App():
12271227
items = use_state(["A", "B", "C"])
1228-
return html.fragment([Item(item, items, key=item) for item in items.value])
1228+
return html([Item(item, items, key=item) for item in items.value])
12291229

12301230
async with layout_runner(Layout(App())) as runner:
12311231
tree = await runner.render()
@@ -1266,7 +1266,7 @@ async def test_async_renders(async_rendering):
12661266

12671267
@component
12681268
def outer():
1269-
return html.fragment(child_1(), child_2())
1269+
return html(child_1(), child_2())
12701270

12711271
@component
12721272
@child_1_hook.capture

tests/test_html.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def test_script_has_no_event_handlers():
104104

105105

106106
def test_simple_fragment():
107-
assert html.fragment() == {"tagName": ""}
108-
assert html.fragment(1, 2, 3) == {"tagName": "", "children": [1, 2, 3]}
109-
assert html.fragment({"key": "something"}) == {
107+
assert html() == {"tagName": ""}
108+
assert html(1, 2, 3) == {"tagName": "", "children": [1, 2, 3]}
109+
assert html({"key": "something"}) == {
110110
"tagName": "",
111111
"attributes": {"key": "something"},
112112
}
113-
assert html.fragment({"key": "something"}, 1, 2, 3) == {
113+
assert html({"key": "something"}, 1, 2, 3) == {
114114
"tagName": "",
115115
"attributes": {"key": "something"},
116116
"children": [1, 2, 3],
@@ -119,7 +119,7 @@ def test_simple_fragment():
119119

120120
def test_fragment_can_have_no_attributes():
121121
with pytest.raises(TypeError, match=r"Fragments cannot have attributes"):
122-
html.fragment({"someAttribute": 1})
122+
html({"someAttribute": 1})
123123

124124

125125
async def test_svg(display: DisplayFixture):

tests/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ def example_none_return():
325325
"<button></button>",
326326
),
327327
(
328-
html.fragment("hello ", html.fragment("world")),
328+
html("hello ", html("world")),
329329
"hello world",
330330
),
331331
(
332-
html.fragment(html.div("hello"), html.fragment("world")),
332+
html(html.div("hello"), html("world")),
333333
"<div>hello</div>world",
334334
),
335335
(
@@ -341,15 +341,15 @@ def example_none_return():
341341
'<div style="background-color:blue;margin-left:10px"></div>',
342342
),
343343
(
344-
html.fragment(
344+
html(
345345
html.div("hello"),
346346
html.a({"href": "https://example.com"}, "example"),
347347
),
348348
'<div>hello</div><a href="https://example.com">example</a>',
349349
),
350350
(
351351
html.div(
352-
html.fragment(
352+
html(
353353
html.div("hello"),
354354
html.a({"href": "https://example.com"}, "example"),
355355
),

0 commit comments

Comments
 (0)