alert.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Alert components."""
  2. from typing import Optional
  3. from reflex.components.chakra import (
  4. ChakraComponent,
  5. LiteralAlertVariant,
  6. LiteralStatus,
  7. )
  8. from reflex.components.component import Component
  9. from reflex.vars import Var
  10. class Alert(ChakraComponent):
  11. """An alert feedback box."""
  12. tag = "Alert"
  13. # The status of the alert ("success" | "info" | "warning" | "error")
  14. status: Optional[Var[LiteralStatus]] = None
  15. # "subtle" | "left-accent" | "top-accent" | "solid"
  16. variant: Optional[Var[LiteralAlertVariant]] = None
  17. @classmethod
  18. def create(
  19. cls, *children, icon=True, title="Alert title", desc=None, **props
  20. ) -> Component:
  21. """Create an alert component.
  22. Args:
  23. *children: The children of the component.
  24. icon: The icon of the alert.
  25. title: The title of the alert.
  26. desc: The description of the alert
  27. **props: The properties of the component.
  28. Returns:
  29. The alert component.
  30. """
  31. if len(children) == 0:
  32. children = []
  33. if icon:
  34. children.append(AlertIcon.create())
  35. children.append(AlertTitle.create(title))
  36. if desc:
  37. children.append(AlertDescription.create(desc))
  38. return super().create(*children, **props)
  39. class AlertIcon(ChakraComponent):
  40. """An icon displayed in the alert."""
  41. tag = "AlertIcon"
  42. class AlertTitle(ChakraComponent):
  43. """The title of the alert."""
  44. tag = "AlertTitle"
  45. class AlertDescription(ChakraComponent):
  46. """AlertDescription composes the Box component."""
  47. tag = "AlertDescription"