Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ClientState compatibility with rx.input #3490

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reflex/components/chakra/forms/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def create(cls, *children, **props) -> Component:
Returns:
The component.
"""
if props.get("value") is not None and props.get("on_change"):
if props.get("value") is not None and props.get("on_change") is not None:
# create a debounced input if the user requests full control to avoid typing jank
return DebounceInput.create(super().create(*children, **props))
return super().create(*children, **props)
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/chakra/forms/textarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def create(cls, *children, **props) -> Component:
Returns:
The component.
"""
if props.get("value") is not None and props.get("on_change"):
if props.get("value") is not None and props.get("on_change") is not None:
# create a debounced input if the user requests full control to avoid typing jank
return DebounceInput.create(super().create(*children, **props))
return super().create(*children, **props)
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/components/text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def create(cls, *children, **props) -> Component:
Returns:
The component.
"""
if props.get("value") is not None and props.get("on_change"):
if props.get("value") is not None and props.get("on_change") is not None:
# create a debounced input if the user requests full control to avoid typing jank
return DebounceInput.create(super().create(*children, **props))
return super().create(*children, **props)
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/components/text_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def create(cls, *children, **props) -> Component:
The component.
"""
component = super().create(*children, **props)
if props.get("value") is not None and props.get("on_change"):
if props.get("value") is not None and props.get("on_change") is not None:
# create a debounced input if the user requests full control to avoid typing jank
return DebounceInput.create(component)
return component
Expand Down
11 changes: 8 additions & 3 deletions reflex/experimental/client_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from reflex import constants
from reflex.event import EventChain, EventHandler, EventSpec, call_script
from reflex.utils.imports import ImportVar
from reflex.vars import Var, VarData
from reflex.vars import Var, VarData, get_unique_variable_name

NoValue = object()

Expand Down Expand Up @@ -74,7 +74,10 @@ def __hash__(self) -> int:

@classmethod
def create(
cls, var_name: str, default: Any = NoValue, global_ref: bool = True
cls,
var_name: str | None = None,
default: Any = NoValue,
global_ref: bool = True,
) -> "ClientStateVar":
"""Create a local_state Var that can be accessed and updated on the client.

Expand All @@ -101,6 +104,8 @@ def create(
Returns:
ClientStateVar
"""
if var_name is None:
var_name = get_unique_variable_name()
assert isinstance(var_name, str), "var_name must be a string."
if default is NoValue:
default_var = Var.create_safe("", _var_is_local=False, _var_is_string=False)
Expand Down Expand Up @@ -188,7 +193,7 @@ def set_value(self, value: Any = NoValue) -> Var:
# This is a hack to make it work like an EventSpec taking an arg
value = Var.create_safe(value, _var_is_string=isinstance(value, str))
if not value._var_is_string and value._var_full_name.startswith("_"):
arg = value._var_name_unwrapped
arg = value._var_name_unwrapped.partition(".")[0]
else:
arg = ""
setter = f"({arg}) => {setter}({value._var_name_unwrapped})"
Expand Down
12 changes: 5 additions & 7 deletions reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,18 +1792,16 @@ def _var_name_unwrapped(self) -> str:
"""
from reflex.style import Style

type_ = (
get_origin(self._var_type)
if types.is_generic_alias(self._var_type)
else self._var_type
)
generic_alias = types.is_generic_alias(self._var_type)

type_ = get_origin(self._var_type) if generic_alias else self._var_type
wrapped_var = str(self)

return (
wrapped_var
if not self._var_state
and types._issubclass(type_, dict)
or types._issubclass(type_, Style)
and not generic_alias
and (types._issubclass(type_, dict) or types._issubclass(type_, Style))
else wrapped_var.strip("{}")
)

Expand Down
Loading