breadcrumb.py 2.9 KB

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