breadcrumb.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Breadcrumb components."""
  2. from typing import Optional
  3. from reflex.components.chakra import ChakraComponent
  4. from reflex.components.chakra.navigation.link import Link
  5. from reflex.components.component import Component
  6. from reflex.components.core.foreach import Foreach
  7. from reflex.vars import Var
  8. class Breadcrumb(ChakraComponent):
  9. """The parent container for breadcrumbs."""
  10. tag = "Breadcrumb"
  11. # The visual separator between each breadcrumb item
  12. separator: Optional[Var[str]] = None
  13. # The left and right margin applied to the separator
  14. separator_margin: Optional[Var[str]] = None
  15. @classmethod
  16. def create(cls, *children, items=None, **props) -> Component:
  17. """Create a breadcrumb component.
  18. If the kw-args `items` is provided and is a list, they will be added as children.
  19. Args:
  20. *children: The children of the component.
  21. items (list): The items of the breadcrumb: (label, link)
  22. **props: The properties of the component.
  23. Returns:
  24. The breadcrumb component.
  25. """
  26. if len(children) == 0:
  27. if isinstance(items, Var):
  28. children = [
  29. Foreach.create(
  30. items,
  31. lambda item: BreadcrumbItem.create(label=item[0], href=item[1]),
  32. ),
  33. ]
  34. else:
  35. children = []
  36. for label, link in items or []:
  37. children.append(BreadcrumbItem.create(label=label, href=link))
  38. return super().create(*children, **props)
  39. class BreadcrumbItem(ChakraComponent):
  40. """Individual breadcrumb element containing a link and a divider."""
  41. tag = "BreadcrumbItem"
  42. # Is the current page of the breadcrumb.
  43. is_current_page: Optional[Var[bool]] = None
  44. # Is the last child of the breadcrumb.
  45. is_last_child: Optional[Var[bool]] = None
  46. # The visual separator between each breadcrumb item
  47. separator: Optional[Var[str]] = None
  48. # The left and right margin applied to the separator
  49. spacing: Optional[Var[str]] = None
  50. @classmethod
  51. def create(cls, *children, label=None, href=None, **props):
  52. """Create a Breadcrumb Item component.
  53. Args:
  54. *children: The children of the component.
  55. label: The label used in the link. Defaults to None.
  56. href: The URL of the link. Defaults to None.
  57. **props: The properties of the component.
  58. Returns:
  59. The BreadcrumbItem component
  60. """
  61. if len(children) == 0:
  62. children = [BreadcrumbLink.create(label or "", href=href or "")] # type: ignore
  63. return super().create(*children, **props)
  64. class BreadcrumbSeparator(ChakraComponent):
  65. """The visual separator between each breadcrumb."""
  66. tag = "BreadcrumbSeparator"
  67. class BreadcrumbLink(Link):
  68. """The breadcrumb link."""
  69. tag = "BreadcrumbLink"
  70. # Is the current page of the breadcrumb.
  71. is_current_page: Optional[Var[bool]] = None