remove all instances of Var.create

This commit is contained in:
Khaleel Al-Adhami 2024-08-05 16:01:56 -07:00
parent 450610ac48
commit da95d0f519
5 changed files with 28 additions and 12 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from reflex.components.chakra import ChakraComponent, LiteralColorScheme from reflex.components.chakra import ChakraComponent, LiteralColorScheme
from reflex.event import EventHandler from reflex.event import EventHandler
from reflex.ivars.base import LiteralVar
from reflex.vars import Var from reflex.vars import Var
@ -34,7 +35,7 @@ class Switch(ChakraComponent):
name: Var[str] name: Var[str]
# The value of the input field when checked (use is_checked prop for a bool) # The value of the input field when checked (use is_checked prop for a bool)
value: Var[str] = Var.create(True) # type: ignore value: Var[str] = LiteralVar.create("true")
# The spacing between the switch and its label text (0.5rem) # The spacing between the switch and its label text (0.5rem)
spacing: Var[str] spacing: Var[str]

View File

@ -154,7 +154,7 @@ def cond(condition: Any, c1: Any, c2: Any = None) -> Component | ImmutableVar:
raise ValueError("For conditional vars, the second argument must be set.") raise ValueError("For conditional vars, the second argument must be set.")
def create_var(cond_part): def create_var(cond_part):
return LiteralVar.create_safe(cond_part) return LiteralVar.create(cond_part)
# convert the truth and false cond parts into vars so the _var_data can be obtained. # convert the truth and false cond parts into vars so the _var_data can be obtained.
c1 = create_var(c1) c1 = create_var(c1)

View File

@ -12,6 +12,7 @@ from reflex.components.radix.themes.typography.text import Text
from reflex.event import EventHandler from reflex.event import EventHandler
from reflex.ivars.base import ImmutableVar, LiteralVar from reflex.ivars.base import ImmutableVar, LiteralVar
from reflex.ivars.function import JSON_STRINGIFY from reflex.ivars.function import JSON_STRINGIFY
from reflex.ivars.sequence import StringVar
from reflex.vars import Var from reflex.vars import Var
from ..base import ( from ..base import (
@ -160,19 +161,18 @@ class HighLevelRadioGroup(RadixThemesComponent):
else: else:
default_value = JSON_STRINGIFY.call(ImmutableVar.create(default_value)) default_value = JSON_STRINGIFY.call(ImmutableVar.create(default_value))
def radio_group_item(value: str | Var) -> Component: def radio_group_item(value: Var) -> Component:
item_value = Var.create(value, _var_is_string=False) # type: ignore
item_value = rx.cond( item_value = rx.cond(
item_value._type() == str, # type: ignore value._type() == "string",
item_value, value,
JSON_STRINGIFY.call(item_value), # type: ignore JSON_STRINGIFY.call(value),
) ).to(StringVar)
return Text.create( return Text.create(
Flex.create( Flex.create(
RadioGroupItem.create( RadioGroupItem.create(
value=item_value, value=item_value,
disabled=props.get("disabled", Var.create(False)), disabled=props.get("disabled", LiteralVar.create(False)),
), ),
item_value, item_value,
spacing="2", spacing="2",
@ -181,8 +181,7 @@ class HighLevelRadioGroup(RadixThemesComponent):
as_="label", as_="label",
) )
items = Var.create(items) # type: ignore children = [rx.foreach(LiteralVar.create(items), radio_group_item)]
children = [rx.foreach(items, radio_group_item)]
return RadioGroupRoot.create( return RadioGroupRoot.create(
Flex.create( Flex.create(

View File

@ -10,6 +10,7 @@ from reflex.components.core.breakpoints import Responsive
from reflex.components.core.debounce import DebounceInput from reflex.components.core.debounce import DebounceInput
from reflex.components.el import elements from reflex.components.el import elements
from reflex.event import EventHandler from reflex.event import EventHandler
from reflex.ivars.base import LiteralVar
from reflex.style import Style, format_as_emotion from reflex.style import Style, format_as_emotion
from reflex.utils import console from reflex.utils import console
from reflex.vars import Var from reflex.vars import Var
@ -154,7 +155,7 @@ class TextFieldRoot(elements.Div, RadixThemesComponent):
style = Style(template.style) style = Style(template.style)
style.update(child.style) style.update(child.style)
child._get_style = lambda style=style: { child._get_style = lambda style=style: {
"css": Var.create(format_as_emotion(style)) "css": LiteralVar.create(format_as_emotion(style))
} }
for trigger in template.event_triggers: for trigger in template.event_triggers:
if trigger not in child.event_triggers: if trigger not in child.event_triggers:

View File

@ -684,6 +684,21 @@ class ImmutableVar(Var, Generic[VAR_TYPE]):
).to(ObjectVar) ).to(ObjectVar)
return refs[self] return refs[self]
def _type(self) -> StringVar:
"""Returns the type of the object.
This method uses the `typeof` function from the `FunctionStringVar` class
to determine the type of the object.
Returns:
StringVar: A string variable representing the type of the object.
"""
from .function import FunctionStringVar
from .sequence import StringVar
type_of = FunctionStringVar("typeof")
return type_of.call(self).to(StringVar)
OUTPUT = TypeVar("OUTPUT", bound=ImmutableVar) OUTPUT = TypeVar("OUTPUT", bound=ImmutableVar)