address comment on doing this only for optionals

This commit is contained in:
Elijah 2024-12-12 19:21:05 +00:00
parent 1cddb88030
commit 31462ad14d
2 changed files with 10 additions and 5 deletions

View File

@ -18,6 +18,7 @@ from reflex.event import (
prevent_default,
)
from reflex.utils.imports import ImportDict
from reflex.utils.types import is_optional
from reflex.vars import VarData
from reflex.vars.base import LiteralVar, Var
@ -400,9 +401,11 @@ class Input(BaseHTML):
value = props.get("value")
# React expects an empty string(instead of null) for uncontrolled inputs.
if value is not None:
if value is not None and is_optional(
(value_var := Var.create(value))._var_type
):
props["value"] = ternary_operation(
((value_var := Var.create(value)) != Var.create(None))
(value_var != Var.create(None))
& (value_var != Var(_js_expr="undefined")),
value,
"",

View File

@ -9,6 +9,7 @@ from reflex.components.core.breakpoints import Responsive
from reflex.components.core.debounce import DebounceInput
from reflex.components.el import elements
from reflex.event import EventHandler, input_event, key_event
from reflex.utils.types import is_optional
from reflex.vars.base import Var
from reflex.vars.number import ternary_operation
@ -100,10 +101,11 @@ class TextFieldRoot(elements.Div, RadixThemesComponent):
value = props.get("value")
# React expects an empty string(instead of null) for uncontrolled inputs.
if value is not None:
# props["value"] = Var(_js_expr=f"{value} ?? ''", _var_type=str)
if value is not None and is_optional(
(value_var := Var.create(value))._var_type
):
props["value"] = ternary_operation(
((value_var := Var.create(value)) != Var.create(None))
(value_var != Var.create(None))
& (value_var != Var(_js_expr="undefined")),
value,
"",