use is true (#3946)

This commit is contained in:
Khaleel Al-Adhami 2024-09-18 11:32:47 -07:00 committed by GitHub
parent abb884c156
commit d81faf7dad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -1983,17 +1983,23 @@ class CustomVarOperationReturn(Var[RETURN]):
def var_operation_return( def var_operation_return(
js_expression: str, js_expression: str,
var_type: Type[RETURN] | None = None, var_type: Type[RETURN] | None = None,
var_data: VarData | None = None,
) -> CustomVarOperationReturn[RETURN]: ) -> CustomVarOperationReturn[RETURN]:
"""Shortcut for creating a CustomVarOperationReturn. """Shortcut for creating a CustomVarOperationReturn.
Args: Args:
js_expression: The JavaScript expression to evaluate. js_expression: The JavaScript expression to evaluate.
var_type: The type of the var. var_type: The type of the var.
var_data: Additional hooks and imports associated with the Var.
Returns: Returns:
The CustomVarOperationReturn. The CustomVarOperationReturn.
""" """
return CustomVarOperationReturn.create(js_expression, var_type) return CustomVarOperationReturn.create(
js_expression,
var_type,
var_data,
)
@dataclasses.dataclass( @dataclasses.dataclass(

View File

@ -17,7 +17,9 @@ from typing import (
overload, overload,
) )
from reflex.constants.base import Dirs
from reflex.utils.exceptions import VarTypeError from reflex.utils.exceptions import VarTypeError
from reflex.utils.imports import ImportDict, ImportVar
from .base import ( from .base import (
CustomVarOperationReturn, CustomVarOperationReturn,
@ -1098,6 +1100,11 @@ class ToBooleanVarOperation(ToOperation, BooleanVar):
_default_var_type: ClassVar[Type] = bool _default_var_type: ClassVar[Type] = bool
_IS_TRUE_IMPORT: ImportDict = {
f"/{Dirs.STATE_PATH}": [ImportVar(tag="isTrue")],
}
@var_operation @var_operation
def boolify(value: Var): def boolify(value: Var):
"""Convert the value to a boolean. """Convert the value to a boolean.
@ -1109,8 +1116,9 @@ def boolify(value: Var):
The boolean value. The boolean value.
""" """
return var_operation_return( return var_operation_return(
js_expression=f"Boolean({value})", js_expression=f"isTrue({value})",
var_type=bool, var_type=bool,
var_data=VarData(imports=_IS_TRUE_IMPORT),
) )