Unpack component tuples in a fragment (#2985)

This commit is contained in:
Elijah Ahianyo 2024-04-03 10:44:04 -07:00 committed by GitHub
parent 00b1e193ad
commit d43a85bab2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View File

@ -457,6 +457,10 @@ class App(Base):
# Generate the component if it is a callable.
component = self._generate_component(component)
# unpack components that return tuples in an rx.fragment.
if isinstance(component, tuple):
component = Fragment.create(*component)
# Ensure state is enabled if this page uses state.
if self.state is None:
if on_load or component._has_event_triggers():

View File

@ -1355,3 +1355,32 @@ def test_app_state_determination():
# Referencing an event handler enables state.
a4.add_page(rx.box(rx.button("Click", on_click=rx.console_log(""))), route="/")
assert a4.state is not None
def test_add_page_component_returning_tuple():
"""Test that a component or render method returning a
tuple is unpacked in a Fragment.
"""
app = App()
def index():
return rx.text("first"), rx.text("second")
def page2():
return (rx.text("third"),)
app.add_page(index) # type: ignore
app.add_page(page2) # type: ignore
assert isinstance((fragment_wrapper := app.pages["index"].children[0]), Fragment)
assert isinstance((first_text := fragment_wrapper.children[0]), Text)
assert str(first_text.children[0].contents) == "{`first`}" # type: ignore
assert isinstance((second_text := fragment_wrapper.children[1]), Text)
assert str(second_text.children[0].contents) == "{`second`}" # type: ignore
# Test page with trailing comma.
assert isinstance(
(page2_fragment_wrapper := app.pages["page2"].children[0]), Fragment
)
assert isinstance((third_text := page2_fragment_wrapper.children[0]), Text)
assert str(third_text.children[0].contents) == "{`third`}" # type: ignore