list.py 1.5 KB

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