table.py 5.0 KB

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