[REF-3597] Type check Radio items (#3856)

This commit is contained in:
Elijah Ahianyo 2024-08-29 20:23:21 +00:00 committed by GitHub
parent dd6feff13f
commit 4d9f427b19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View File

@ -12,6 +12,7 @@ from reflex.components.radix.themes.typography.text import Text
from reflex.event import EventHandler
from reflex.ivars.base import ImmutableVar, LiteralVar
from reflex.ivars.sequence import StringVar
from reflex.utils import types
from reflex.vars import Var
from ..base import (
@ -133,6 +134,9 @@ class HighLevelRadioGroup(RadixThemesComponent):
Returns:
The created radio group component.
Raises:
TypeError: If the type of items is invalid.
"""
direction = props.pop("direction", "column")
spacing = props.pop("spacing", "2")
@ -141,6 +145,16 @@ class HighLevelRadioGroup(RadixThemesComponent):
color_scheme = props.pop("color_scheme", None)
default_value = props.pop("default_value", "")
if (
not isinstance(items, (list, Var))
or isinstance(items, Var)
and not types._issubclass(items._var_type, list)
):
items_type = type(items) if not isinstance(items, Var) else items._var_type
raise TypeError(
f"The radio group component takes in a list, got {items_type} instead"
)
default_value = LiteralVar.create(default_value)
# convert only non-strings to json(JSON.stringify) so quotes are not rendered

View File

@ -419,6 +419,9 @@ class HighLevelRadioGroup(RadixThemesComponent):
Returns:
The created radio group component.
Raises:
TypeError: If the type of items is invalid.
"""
...
@ -588,6 +591,9 @@ class RadioGroup(ComponentNamespace):
Returns:
The created radio group component.
Raises:
TypeError: If the type of items is invalid.
"""
...