cond.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """Create a list of components from an iterable."""
  2. from __future__ import annotations
  3. from typing import Any, Dict, Optional, Union, overload
  4. from reflex.components.base.fragment import Fragment
  5. from reflex.components.component import BaseComponent, Component, MemoizationLeaf
  6. from reflex.components.tags import CondTag, Tag
  7. from reflex.constants import Dirs
  8. from reflex.constants.colors import Color
  9. from reflex.style import LIGHT_COLOR_MODE, color_mode
  10. from reflex.utils import format, imports
  11. from reflex.vars import Var, VarData
  12. _IS_TRUE_IMPORT = {
  13. f"/{Dirs.STATE_PATH}": [imports.ImportVar(tag="isTrue")],
  14. }
  15. class Cond(MemoizationLeaf):
  16. """Render one of two components based on a condition."""
  17. # The cond to determine which component to render.
  18. cond: Var[Any]
  19. # The component to render if the cond is true.
  20. comp1: BaseComponent = None # type: ignore
  21. # The component to render if the cond is false.
  22. comp2: BaseComponent = None # type: ignore
  23. @classmethod
  24. def create(
  25. cls,
  26. cond: Var,
  27. comp1: BaseComponent,
  28. comp2: Optional[BaseComponent] = None,
  29. ) -> Component:
  30. """Create a conditional component.
  31. Args:
  32. cond: The cond to determine which component to render.
  33. comp1: The component to render if the cond is true.
  34. comp2: The component to render if the cond is false.
  35. Returns:
  36. The conditional component.
  37. """
  38. # Wrap everything in fragments.
  39. if comp1.__class__.__name__ != "Fragment":
  40. comp1 = Fragment.create(comp1)
  41. if comp2 is None or comp2.__class__.__name__ != "Fragment":
  42. comp2 = Fragment.create(comp2) if comp2 else Fragment.create()
  43. return Fragment.create(
  44. cls(
  45. cond=cond,
  46. comp1=comp1,
  47. comp2=comp2,
  48. children=[comp1, comp2],
  49. )
  50. )
  51. def _get_props_imports(self):
  52. """Get the imports needed for component's props.
  53. Returns:
  54. The imports for the component's props of the component.
  55. """
  56. return []
  57. def _render(self) -> Tag:
  58. return CondTag(
  59. cond=self.cond,
  60. true_value=self.comp1.render(),
  61. false_value=self.comp2.render(),
  62. )
  63. def render(self) -> Dict:
  64. """Render the component.
  65. Returns:
  66. The dictionary for template of component.
  67. """
  68. tag = self._render()
  69. return dict(
  70. tag.add_props(
  71. **self.event_triggers,
  72. key=self.key,
  73. sx=self.style,
  74. id=self.id,
  75. class_name=self.class_name,
  76. ).set(
  77. props=tag.format_props(),
  78. ),
  79. cond_state=f"isTrue({self.cond._var_full_name})",
  80. )
  81. def _get_imports(self) -> imports.ImportDict:
  82. return imports.merge_imports(
  83. super()._get_imports(),
  84. getattr(self.cond._var_data, "imports", {}),
  85. _IS_TRUE_IMPORT,
  86. )
  87. @overload
  88. def cond(condition: Any, c1: Component, c2: Any) -> Component:
  89. ...
  90. @overload
  91. def cond(condition: Any, c1: Component) -> Component:
  92. ...
  93. @overload
  94. def cond(condition: Any, c1: Any, c2: Any) -> Var:
  95. ...
  96. def cond(condition: Any, c1: Any, c2: Any = None):
  97. """Create a conditional component or Prop.
  98. Args:
  99. condition: The cond to determine which component to render.
  100. c1: The component or prop to render if the cond_var is true.
  101. c2: The component or prop to render if the cond_var is false.
  102. Returns:
  103. The conditional component.
  104. Raises:
  105. ValueError: If the arguments are invalid.
  106. """
  107. var_datas: list[VarData | None] = [
  108. VarData( # type: ignore
  109. imports=_IS_TRUE_IMPORT,
  110. ),
  111. ]
  112. # Convert the condition to a Var.
  113. cond_var = Var.create(condition)
  114. assert cond_var is not None, "The condition must be set."
  115. # If the first component is a component, create a Cond component.
  116. if isinstance(c1, BaseComponent):
  117. assert c2 is None or isinstance(
  118. c2, BaseComponent
  119. ), "Both arguments must be components."
  120. return Cond.create(cond_var, c1, c2)
  121. if isinstance(c1, Var):
  122. var_datas.append(c1._var_data)
  123. # Otherwise, create a conditional Var.
  124. # Check that the second argument is valid.
  125. if isinstance(c2, BaseComponent):
  126. raise ValueError("Both arguments must be props.")
  127. if c2 is None:
  128. raise ValueError("For conditional vars, the second argument must be set.")
  129. if isinstance(c2, Var):
  130. var_datas.append(c2._var_data)
  131. def create_var(cond_part):
  132. return Var.create_safe(
  133. cond_part,
  134. _var_is_string=isinstance(cond_part, (str, Color)),
  135. )
  136. # convert the truth and false cond parts into vars so the _var_data can be obtained.
  137. c1 = create_var(c1)
  138. c2 = create_var(c2)
  139. var_datas.extend([c1._var_data, c2._var_data])
  140. c1_type = c1._var_type if isinstance(c1, Var) else type(c1)
  141. c2_type = c2._var_type if isinstance(c2, Var) else type(c2)
  142. var_type = c1_type if c1_type == c2_type else Union[c1_type, c2_type]
  143. # Create the conditional var.
  144. return cond_var._replace(
  145. _var_name=format.format_cond(
  146. cond=cond_var._var_full_name,
  147. true_value=c1,
  148. false_value=c2,
  149. is_prop=True,
  150. ),
  151. _var_type=var_type,
  152. _var_is_local=False,
  153. _var_full_name_needs_state_prefix=False,
  154. merge_var_data=VarData.merge(*var_datas),
  155. )
  156. def color_mode_cond(light: Any, dark: Any = None) -> Var | Component:
  157. """Create a component or Prop based on color_mode.
  158. Args:
  159. light: The component or prop to render if color_mode is default
  160. dark: The component or prop to render if color_mode is non-default
  161. Returns:
  162. The conditional component or prop.
  163. """
  164. return cond(
  165. color_mode == Var.create(LIGHT_COLOR_MODE, _var_is_string=True),
  166. light,
  167. dark,
  168. )