bare.py 1.3 KB

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