cond.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Create a list of components from an iterable."""
  2. from __future__ import annotations
  3. from typing import Any, Optional
  4. from pynecone.components.component import Component
  5. from pynecone.components.layout.fragment import Fragment
  6. from pynecone.components.tags import CondTag, Tag
  7. from pynecone.utils import format
  8. from pynecone.var import Var
  9. class Cond(Component):
  10. """Render one of two components based on a condition."""
  11. # The cond to determine which component to render.
  12. cond: Var[Any]
  13. # The component to render if the cond is true.
  14. comp1: Component
  15. # The component to render if the cond is false.
  16. comp2: Component
  17. @classmethod
  18. def create(
  19. cls, cond: Var, comp1: Component, comp2: Optional[Component] = None
  20. ) -> Component:
  21. """Create a conditional component.
  22. Args:
  23. cond: The cond to determine which component to render.
  24. comp1: The component to render if the cond is true.
  25. comp2: The component to render if the cond is false.
  26. Returns:
  27. The conditional component.
  28. """
  29. # Wrap everything in fragments.
  30. comp1 = Fragment.create(comp1)
  31. comp2 = Fragment.create(comp2) if comp2 else Fragment.create()
  32. return Fragment.create(
  33. cls(
  34. cond=cond,
  35. comp1=comp1,
  36. comp2=comp2,
  37. children=[comp1, comp2],
  38. )
  39. )
  40. def _render(self) -> Tag:
  41. return CondTag(
  42. cond=self.cond,
  43. true_value=self.comp1.render(),
  44. false_value=self.comp2.render(),
  45. )
  46. def cond(condition: Any, c1: Any, c2: Any = None):
  47. """Create a conditional component or Prop.
  48. Args:
  49. condition: The cond to determine which component to render.
  50. c1: The component or prop to render if the cond_var is true.
  51. c2: The component or prop to render if the cond_var is false.
  52. Returns:
  53. The conditional component.
  54. Raises:
  55. ValueError: If the arguments are invalid.
  56. """
  57. # Import here to avoid circular imports.
  58. from pynecone.var import BaseVar, Var
  59. # Convert the condition to a Var.
  60. cond_var = Var.create(condition)
  61. assert cond_var is not None, "The condition must be set."
  62. # If the first component is a component, create a Cond component.
  63. if isinstance(c1, Component):
  64. assert c2 is None or isinstance(
  65. c2, Component
  66. ), "Both arguments must be components."
  67. return Cond.create(cond_var, c1, c2)
  68. # Otherwise, create a conditionl Var.
  69. # Check that the second argument is valid.
  70. if isinstance(c2, Component):
  71. raise ValueError("Both arguments must be props.")
  72. if c2 is None:
  73. raise ValueError("For conditional vars, the second argument must be set.")
  74. # Create the conditional var.
  75. return BaseVar(
  76. name=format.format_cond(
  77. cond=cond_var.full_name,
  78. true_value=c1,
  79. false_value=c2,
  80. is_prop=True,
  81. ),
  82. type_=c1.type_ if isinstance(c1, BaseVar) else type(c1),
  83. )