1
0

cond.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Create a list of components from an iterable."""
  2. from __future__ import annotations
  3. from typing import Any, overload
  4. from reflex.components.base.fragment import Fragment
  5. from reflex.components.component import BaseComponent, Component
  6. from reflex.style import LIGHT_COLOR_MODE, resolved_color_mode
  7. from reflex.utils import types
  8. from reflex.utils.types import safe_issubclass
  9. from reflex.vars.base import LiteralVar, ReflexCallable, Var
  10. from reflex.vars.function import ArgsFunctionOperation
  11. from reflex.vars.number import ternary_operation
  12. @overload
  13. def cond(condition: Any, c1: Component, c2: Any = None) -> Component: ...
  14. @overload
  15. def cond(condition: Any, c1: Any, c2: Any) -> Var: ...
  16. def cond(condition: Any, c1: Any, c2: Any = None) -> Component | Var:
  17. """Create a conditional component or Prop.
  18. Args:
  19. condition: The cond to determine which component to render.
  20. c1: The component or prop to render if the cond_var is true.
  21. c2: The component or prop to render if the cond_var is false.
  22. Returns:
  23. The conditional component.
  24. Raises:
  25. ValueError: If the arguments are invalid.
  26. """
  27. # Convert the condition to a Var.
  28. cond_var = LiteralVar.create(condition)
  29. # If the first component is a component, create a Fragment if the second component is not set.
  30. if isinstance(c1, BaseComponent) or (
  31. isinstance(c1, Var)
  32. and (
  33. safe_issubclass(c1._var_type, BaseComponent)
  34. or types.safe_typehint_issubclass(c1._var_type, list[BaseComponent])
  35. )
  36. ):
  37. c2 = c2 if c2 is not None else Fragment.create()
  38. # Check that the second argument is valid.
  39. if c2 is None:
  40. raise ValueError("For conditional vars, the second argument must be set.")
  41. c1 = Var.create(c1)
  42. c2 = Var.create(c2)
  43. # Create the conditional var.
  44. return ternary_operation(
  45. cond_var.bool(),
  46. ArgsFunctionOperation.create(
  47. (),
  48. c1,
  49. _var_type=ReflexCallable[[], c1._var_type],
  50. ),
  51. ArgsFunctionOperation.create(
  52. (),
  53. c2,
  54. _var_type=ReflexCallable[[], c2._var_type],
  55. ),
  56. ).call()
  57. @overload
  58. def color_mode_cond(light: Component, dark: Component | None = None) -> Component: ... # type: ignore
  59. @overload
  60. def color_mode_cond(light: Any, dark: Any = None) -> Var: ...
  61. def color_mode_cond(light: Any, dark: Any = None) -> Var | Component:
  62. """Create a component or Prop based on color_mode.
  63. Args:
  64. light: The component or prop to render if color_mode is default
  65. dark: The component or prop to render if color_mode is non-default
  66. Returns:
  67. The conditional component or prop.
  68. """
  69. return cond(
  70. resolved_color_mode == LiteralVar.create(LIGHT_COLOR_MODE),
  71. light,
  72. dark,
  73. )
  74. class Cond:
  75. """Create a conditional component or Prop."""
  76. create = staticmethod(cond)