fix rx.image src not working with state (#1915)

This commit is contained in:
Thomas Brandého 2023-10-04 23:15:18 +02:00 committed by GitHub
parent 43cc8d4f6c
commit 58933278ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View File

@ -7,7 +7,6 @@ from typing import Any, Optional, Union
from reflex.components.component import Component from reflex.components.component import Component
from reflex.components.libs.chakra import ChakraComponent from reflex.components.libs.chakra import ChakraComponent
from reflex.components.tags import Tag
from reflex.utils.serializers import serializer from reflex.utils.serializers import serializer
from reflex.vars import Var from reflex.vars import Var
@ -65,11 +64,21 @@ class Image(ChakraComponent):
"on_load": lambda: [], "on_load": lambda: [],
} }
def _render(self) -> Tag: @classmethod
self.src.is_string = True def create(cls, *children, **props) -> Component:
"""Create an Image component.
# Render the table. Args:
return super()._render() *children: The children of the image.
**props: The props of the image.
Returns:
The Image component.
"""
src = props.get("src", None)
if src is not None and not isinstance(src, (Var)):
props["src"] = Var.create(value=src, is_string=True)
return super().create(*children, **props)
try: try:

View File

@ -34,7 +34,7 @@ try:
def test_set_src_str(): def test_set_src_str():
"""Test that setting the src works.""" """Test that setting the src works."""
image = rx.image(src="pic2.jpeg") image = rx.image(src="pic2.jpeg")
assert str(image.src) == "pic2.jpeg" # type: ignore assert str(image.src) == "{`pic2.jpeg`}" # type: ignore
def test_set_src_img(pil_image: Img): def test_set_src_img(pil_image: Img):
"""Test that setting the src works. """Test that setting the src works.
@ -43,7 +43,7 @@ try:
pil_image: The image to serialize. pil_image: The image to serialize.
""" """
image = Image.create(src=pil_image) image = Image.create(src=pil_image)
assert str(image.src) == serialize_image(pil_image) # type: ignore assert str(image.src.name) == serialize_image(pil_image) # type: ignore
def test_render(pil_image: Img): def test_render(pil_image: Img):
"""Test that rendering an image works. """Test that rendering an image works.
@ -52,8 +52,6 @@ try:
pil_image: The image to serialize. pil_image: The image to serialize.
""" """
image = Image.create(src=pil_image) image = Image.create(src=pil_image)
assert not image.src.is_string # type: ignore
image._render()
assert image.src.is_string # type: ignore assert image.src.is_string # type: ignore
except ImportError: except ImportError: