bare.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 import Var
  8. class Bare(Component):
  9. """A component with no tag."""
  10. contents: Var[str]
  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) and contents._var_data:
  20. contents = contents.to(str)
  21. else:
  22. contents = str(contents)
  23. return cls(contents=contents) # type: ignore
  24. def _render(self) -> Tag:
  25. return Tagless(contents=str(self.contents))
  26. def _get_vars(self) -> Iterator[Var]:
  27. """Walk all Vars used in this component.
  28. Yields:
  29. The contents if it is a Var, otherwise nothing.
  30. """
  31. if isinstance(self.contents, Var):
  32. # Fast path for Bare text components.
  33. yield self.contents