1
0

bare.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """A bare component."""
  2. from __future__ import annotations
  3. from typing import Any, Iterator
  4. from reflex.components.component import Component, LiteralComponentVar
  5. from reflex.components.tags import Tag
  6. from reflex.components.tags.tagless import Tagless
  7. from reflex.utils.imports import ParsedImportDict
  8. from reflex.vars import BooleanVar, ObjectVar, Var
  9. from reflex.vars.base import VarData
  10. class Bare(Component):
  11. """A component with no tag."""
  12. contents: Var[Any]
  13. @classmethod
  14. def create(cls, contents: Any) -> Component:
  15. """Create a Bare component, with no tag.
  16. Args:
  17. contents: The contents of the component.
  18. Returns:
  19. The component.
  20. """
  21. if isinstance(contents, Var):
  22. return cls(contents=contents)
  23. else:
  24. contents = str(contents) if contents is not None else ""
  25. return cls(contents=contents) # type: ignore
  26. def _get_all_hooks_internal(self) -> dict[str, VarData | None]:
  27. """Include the hooks for the component.
  28. Returns:
  29. The hooks for the component.
  30. """
  31. hooks = super()._get_all_hooks_internal()
  32. if isinstance(self.contents, LiteralComponentVar):
  33. hooks |= self.contents._var_value._get_all_hooks_internal()
  34. return hooks
  35. def _get_all_hooks(self) -> dict[str, VarData | None]:
  36. """Include the hooks for the component.
  37. Returns:
  38. The hooks for the component.
  39. """
  40. hooks = super()._get_all_hooks()
  41. if isinstance(self.contents, LiteralComponentVar):
  42. hooks |= self.contents._var_value._get_all_hooks()
  43. return hooks
  44. def _get_all_imports(self) -> ParsedImportDict:
  45. """Include the imports for the component.
  46. Returns:
  47. The imports for the component.
  48. """
  49. imports = super()._get_all_imports()
  50. if isinstance(self.contents, LiteralComponentVar):
  51. var_data = self.contents._get_all_var_data()
  52. if var_data:
  53. imports |= {k: list(v) for k, v in var_data.imports}
  54. return imports
  55. def _get_all_dynamic_imports(self) -> set[str]:
  56. """Get dynamic imports for the component.
  57. Returns:
  58. The dynamic imports.
  59. """
  60. dynamic_imports = super()._get_all_dynamic_imports()
  61. if isinstance(self.contents, LiteralComponentVar):
  62. dynamic_imports |= self.contents._var_value._get_all_dynamic_imports()
  63. return dynamic_imports
  64. def _get_all_custom_code(self) -> set[str]:
  65. """Get custom code for the component.
  66. Returns:
  67. The custom code.
  68. """
  69. custom_code = super()._get_all_custom_code()
  70. if isinstance(self.contents, LiteralComponentVar):
  71. custom_code |= self.contents._var_value._get_all_custom_code()
  72. return custom_code
  73. def _get_all_refs(self) -> set[str]:
  74. """Get the refs for the children of the component.
  75. Returns:
  76. The refs for the children.
  77. """
  78. refs = super()._get_all_refs()
  79. if isinstance(self.contents, LiteralComponentVar):
  80. refs |= self.contents._var_value._get_all_refs()
  81. return refs
  82. def _render(self) -> Tag:
  83. if isinstance(self.contents, Var):
  84. if isinstance(self.contents, (BooleanVar, ObjectVar)):
  85. return Tagless(contents=f"{{{self.contents.to_string()!s}}}")
  86. return Tagless(contents=f"{{{self.contents!s}}}")
  87. return Tagless(contents=str(self.contents))
  88. def _get_vars(
  89. self, include_children: bool = False, ignore_ids: set[int] | None = None
  90. ) -> Iterator[Var]:
  91. """Walk all Vars used in this component.
  92. Args:
  93. include_children: Whether to include Vars from children.
  94. ignore_ids: The ids to ignore.
  95. Yields:
  96. The contents if it is a Var, otherwise nothing.
  97. """
  98. yield self.contents