list.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """List components."""
  2. from __future__ import annotations
  3. from typing import Optional
  4. from reflex.components.chakra import ChakraComponent
  5. from reflex.components.component import Component
  6. from reflex.components.core.foreach import Foreach
  7. from reflex.vars import Var
  8. class List(ChakraComponent):
  9. """Display a list of items."""
  10. tag: str = "List"
  11. # The space between each list item
  12. spacing: Optional[Var[str]] = None
  13. # Shorthand prop for listStylePosition
  14. style_position: Optional[Var[str]] = None
  15. # Shorthand prop for listStyleType
  16. style_type: Optional[Var[str]] = None
  17. @classmethod
  18. def __class_getitem__(cls, item):
  19. """This method is just a hacky workaround to stop pydantic v2 from complaining.
  20. Args:
  21. item: The type of the list items.
  22. Returns:
  23. The list component.
  24. """
  25. return cls
  26. @classmethod
  27. def create(
  28. cls, *children, items: list | Var[list] | None = None, **props
  29. ) -> Component:
  30. """Create a list component.
  31. Args:
  32. *children: The children of the component.
  33. items: A list of items to add to the list.
  34. **props: The properties of the component.
  35. Returns:
  36. The list component.
  37. """
  38. if len(children) == 0:
  39. if isinstance(items, Var):
  40. children = [Foreach.create(items, ListItem.create)]
  41. else:
  42. children = [ListItem.create(item) for item in items or []]
  43. return super().create(*children, **props)
  44. class ListItem(ChakraComponent):
  45. """A single list item."""
  46. tag: str = "ListItem"
  47. class OrderedList(List):
  48. """An ordered list component with numbers."""
  49. tag: str = "OrderedList"
  50. class UnorderedList(List):
  51. """An unordered list component with bullets."""
  52. tag: str = "UnorderedList"