card.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Chakra Card component."""
  2. from typing import Optional
  3. from pynecone.components.component import Component
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.vars import Var
  6. class CardHeader(ChakraComponent):
  7. """The wrapper that contains a card's header."""
  8. tag = "CardHeader"
  9. class CardBody(ChakraComponent):
  10. """The wrapper that houses the card's main content."""
  11. tag = "CardBody"
  12. class CardFooter(ChakraComponent):
  13. """The footer that houses the card actions."""
  14. tag = "CardFooter"
  15. class Card(ChakraComponent):
  16. """The parent wrapper that provides context for its children."""
  17. tag = "Card"
  18. # [required] The flex alignment of the card
  19. align: Var[str]
  20. # [required] The flex direction of the card
  21. direction: Var[str]
  22. # [required] The flex distribution of the card
  23. justify: Var[str]
  24. # The visual color appearance of the component.
  25. # options: "whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" |
  26. # "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" |
  27. # "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
  28. # default: "gray"
  29. color_scheme: Var[str]
  30. # The size of the Card
  31. # options: "sm" | "md" | "lg"
  32. # default: "md"
  33. size: Var[str]
  34. # The variant of the Card
  35. # options: "elevated" | "outline" | "filled" | "unstyled"
  36. # default: "elevated"
  37. variant: Var[str]
  38. @classmethod
  39. def create(
  40. cls,
  41. body: Component,
  42. *,
  43. header: Optional[Component] = None,
  44. footer: Optional[Component] = None,
  45. **props
  46. ) -> Component:
  47. """Creates a Chakra Card with a body and optionally header and/or footer, and returns it.
  48. If header, body or footer are not already instances of Chead, Cbody or Cfoot respectively,
  49. they will be wrapped as such for layout purposes. If you want to modify their props,
  50. e.g. padding_left, you should wrap them yourself.
  51. Args:
  52. body (Component): The main content of the Card that will be created.
  53. header (Optional[Component]): The header of the Card.
  54. footer (Optional[Component]): The footer of the Card.
  55. props: The properties to be passed to the component.
  56. Returns:
  57. The `create()` method returns a Card object.
  58. """
  59. children = []
  60. param_to_component_class = (
  61. (header, CardHeader),
  62. (body, CardBody),
  63. (footer, CardFooter),
  64. )
  65. for param, component_class in param_to_component_class:
  66. if isinstance(param, component_class):
  67. children.append(param)
  68. elif param is not None:
  69. children.append(component_class.create(param))
  70. return super().create(*children, **props)