table.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. """Table components."""
  2. from pynecone.components.component import Component
  3. from pynecone.components.libs.chakra import ChakraComponent
  4. from pynecone.var import Var
  5. class Table(ChakraComponent):
  6. """A table component."""
  7. tag = "Table"
  8. # The color scheme of the table
  9. color_scheme: Var[str]
  10. # The variant of the table style to use
  11. variant: Var[str]
  12. # The size of the table
  13. size: Var[str]
  14. # The placement of the table caption.
  15. placement: Var[str]
  16. @classmethod
  17. def create(
  18. cls, *children, caption=None, headers=None, rows=None, footers=None, **props
  19. ) -> Component:
  20. """Create a table component.
  21. Args:
  22. children: The children of the component.
  23. caption: The caption of the table component.
  24. headers: The headers of the table component.
  25. rows: The rows of the table component.
  26. footers: The footers of the table component.
  27. props: The properties of the component.
  28. Returns:
  29. The table component.
  30. """
  31. if len(children) == 0:
  32. children = []
  33. if caption:
  34. children.append(TableCaption.create(caption))
  35. if headers:
  36. children.append(Thead.create(headers=headers))
  37. if rows:
  38. children.append(Tbody.create(rows=rows))
  39. if footers:
  40. children.append(Tfoot.create(footers=footers))
  41. return super().create(*children, **props)
  42. class Thead(ChakraComponent):
  43. """A table header component."""
  44. tag = "Thead"
  45. @classmethod
  46. def create(cls, *children, headers=None, **props) -> Component:
  47. """Create a table header component.
  48. Args:
  49. children: The children of the component.
  50. props: The properties of the component.
  51. headers (list, optional): List of headers. Defaults to None.
  52. Returns:
  53. The table header component.
  54. """
  55. if len(children) == 0:
  56. children = [Tr.create(cell_type="header", cells=headers)]
  57. return super().create(*children, **props)
  58. class Tbody(ChakraComponent):
  59. """A table body component."""
  60. tag = "Tbody"
  61. @classmethod
  62. def create(cls, *children, rows=None, **props) -> Component:
  63. """Create a table body component.
  64. Args:
  65. children: The children of the component.
  66. props: The properties of the component.
  67. rows (list[list], optional): The rows of the table body. Defaults to None.
  68. Returns:
  69. Component: _description_
  70. """
  71. if len(children) == 0:
  72. children = [Tr.create(cell_type="data", cells=row) for row in rows or []]
  73. return super().create(*children, **props)
  74. class Tfoot(ChakraComponent):
  75. """A table footer component."""
  76. tag = "Tfoot"
  77. @classmethod
  78. def create(cls, *children, footers=None, **props) -> Component:
  79. """Create a table footer component.
  80. Args:
  81. children: The children of the component.
  82. props: The properties of the component.
  83. footers (list, optional): List of footers. Defaults to None.
  84. Returns:
  85. The table footer component.
  86. """
  87. if len(children) == 0:
  88. children = [Tr.create(cell_type="header", cells=footers)]
  89. return super().create(*children, **props)
  90. class Tr(ChakraComponent):
  91. """A table row component."""
  92. tag = "Tr"
  93. @classmethod
  94. def create(cls, *children, cell_type: str = "", cells=None, **props) -> Component:
  95. """Create a table row component.
  96. Args:
  97. children: The children of the component.
  98. props: The properties of the component.
  99. cell_type (str): the type of cells in this table row. "header" or "data". Defaults to None.
  100. cells (list, optional): The cells value to add in the table row. Defaults to None.
  101. Returns:
  102. The table row component
  103. """
  104. types = {"header": Th, "data": Td}
  105. cell_cls = types.get(cell_type)
  106. if len(children) == 0 and cell_cls:
  107. children = [cell_cls.create(cell) for cell in cells or []]
  108. return super().create(*children, **props)
  109. class Th(ChakraComponent):
  110. """A table header cell component."""
  111. tag = "Th"
  112. # Aligns the cell content to the right.
  113. is_numeric: Var[bool]
  114. class Td(ChakraComponent):
  115. """A table data cell component."""
  116. tag = "Td"
  117. # Aligns the cell content to the right.
  118. is_numeric: Var[bool]
  119. class TableCaption(ChakraComponent):
  120. """A table caption component."""
  121. tag = "TableCaption"
  122. # The placement of the table caption. This sets the `caption-side` CSS attribute.
  123. placement: Var[str]
  124. class TableContainer(ChakraComponent):
  125. """The table container component renders a div that wraps the table component."""
  126. tag = "TableContainer"