Format component as React string (#1848)

This commit is contained in:
Nikhil Rao 2023-09-20 19:30:38 -07:00 committed by GitHub
parent d81d544b97
commit ac8dfc5f91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,2 @@
{% import 'web/pages/utils.js.jinja2' as utils %}
{{utils.render(component.render())}}

View File

@ -170,6 +170,18 @@ def _compile_root_stylesheet(stylesheets: List[str]) -> str:
return templates.STYLE.render(stylesheets=sheets)
def _compile_component(component: Component) -> str:
"""Compile a single component.
Args:
component: The component to compile.
Returns:
The compiled component.
"""
return templates.COMPONENT.render(component=component)
def _compile_components(components: Set[CustomComponent]) -> str:
"""Compile the components.

View File

@ -69,6 +69,9 @@ CONTEXT = get_template("web/utils/context.js.jinja2")
# Template for Tailwind config.
TAILWIND_CONFIG = get_template("web/tailwind.config.js.jinja2")
# Template to render a component tag.
COMPONENT = get_template("web/pages/component.js.jinja2")
# Code to render a single NextJS page.
PAGE = get_template("web/pages/index.js.jinja2")

View File

@ -317,7 +317,9 @@ class Component(Base, ABC):
Returns:
The code to render the component.
"""
return format.json_dumps(self.render())
from reflex.compiler.compiler import _compile_component
return _compile_component(self)
def _render(self) -> Tag:
"""Define how to render the component in React.

View File

@ -534,3 +534,23 @@ def test_component_with_only_valid_children(fixture, request):
== f"The component `{component.__name__}` only allows the components: `Text` as children. "
f"Got `Box` instead."
)
@pytest.mark.parametrize(
"component,rendered",
[
(rx.text("hi"), "<Text>\n {`hi`}\n</Text>"),
(
rx.box(rx.heading("test", size="md")),
"<Box>\n <Heading size={`md`}>\n {`test`}\n</Heading>\n</Box>",
),
],
)
def test_format_component(component, rendered):
"""Test that a component is formatted correctly.
Args:
component: The component to format.
rendered: The expected rendered component.
"""
assert str(component) == rendered