bare.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """A bare component."""
  2. from __future__ import annotations
  3. from typing import Any, Iterator
  4. from reflex.components.component import Component
  5. from reflex.components.tags import Tag
  6. from reflex.components.tags.tagless import Tagless
  7. from reflex.ivars.base import ImmutableVar
  8. from reflex.vars import Var
  9. class Bare(Component):
  10. """A component with no tag."""
  11. contents: Var[Any]
  12. @classmethod
  13. def create(cls, contents: Any) -> Component:
  14. """Create a Bare component, with no tag.
  15. Args:
  16. contents: The contents of the component.
  17. Returns:
  18. The component.
  19. """
  20. if isinstance(contents, ImmutableVar):
  21. return cls(contents=contents)
  22. else:
  23. contents = str(contents) if contents is not None else ""
  24. return cls(contents=contents) # type: ignore
  25. def _render(self) -> Tag:
  26. if isinstance(self.contents, ImmutableVar):
  27. return Tagless(contents=f"{{{str(self.contents)}}}")
  28. return Tagless(contents=str(self.contents))
  29. def _get_vars(self, include_children: bool = False) -> Iterator[ImmutableVar]:
  30. """Walk all Vars used in this component.
  31. Args:
  32. include_children: Whether to include Vars from children.
  33. Yields:
  34. The contents if it is a Var, otherwise nothing.
  35. """
  36. yield self.contents