link.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """A link component."""
  2. from reflex.components.chakra import ChakraComponent
  3. from reflex.components.component import Component
  4. from reflex.components.next.link import NextLink
  5. from reflex.utils.imports import ImportDict
  6. from reflex.vars import BaseVar, Var
  7. next_link = NextLink.create()
  8. class Link(ChakraComponent):
  9. """Link to another page."""
  10. tag = "Link"
  11. # The rel.
  12. rel: Var[str]
  13. # The page to link to.
  14. href: Var[str]
  15. # The text to display.
  16. text: Var[str]
  17. # What the link renders to.
  18. as_: Var[str] = BaseVar.create(
  19. value="{NextLink}", _var_is_local=False, _var_is_string=False
  20. ) # type: ignore
  21. # If true, the link will open in new tab.
  22. is_external: Var[bool]
  23. def add_imports(self) -> ImportDict:
  24. """Add imports for the link component.
  25. Returns:
  26. The import dict.
  27. """
  28. return next_link._get_imports() # type: ignore
  29. @classmethod
  30. def create(cls, *children, **props) -> Component:
  31. """Create a Link component.
  32. Args:
  33. *children: The children of the component.
  34. **props: The props of the component.
  35. Raises:
  36. ValueError: in case of missing children
  37. Returns:
  38. Component: The link component
  39. """
  40. if props.get("href") is not None:
  41. if not len(children):
  42. raise ValueError("Link without a child will not display")
  43. else:
  44. # Don't use a NextLink if there is no href.
  45. props["as_"] = ""
  46. return super().create(*children, **props)