table.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. """Table components."""
  2. from typing import List, Tuple
  3. from reflex.components.component import Component
  4. from reflex.components.layout.foreach import Foreach
  5. from reflex.components.libs.chakra import ChakraComponent
  6. from reflex.utils import types
  7. from reflex.vars import Var
  8. class Table(ChakraComponent):
  9. """A table component."""
  10. tag = "Table"
  11. # The color scheme of the table
  12. color_scheme: Var[str]
  13. # The variant of the table style to use
  14. variant: Var[str]
  15. # The size of the table
  16. size: Var[str]
  17. # The placement of the table caption.
  18. placement: Var[str]
  19. @classmethod
  20. def create(
  21. cls, *children, caption=None, headers=None, rows=None, footers=None, **props
  22. ) -> Component:
  23. """Create a table component.
  24. Args:
  25. *children: The children of the component.
  26. caption: The caption of the table component.
  27. headers: The headers of the table component.
  28. rows: The rows of the table component.
  29. footers: The footers of the table component.
  30. **props: The properties of the component.
  31. Returns:
  32. The table component.
  33. """
  34. if len(children) == 0:
  35. children = []
  36. if caption is not None:
  37. children.append(TableCaption.create(caption))
  38. if headers is not None:
  39. children.append(Thead.create(headers=headers))
  40. if rows is not None:
  41. children.append(Tbody.create(rows=rows))
  42. if footers is not None:
  43. children.append(Tfoot.create(footers=footers))
  44. return super().create(*children, **props)
  45. class Thead(ChakraComponent):
  46. """A table header component."""
  47. tag = "Thead"
  48. # invalid children components
  49. invalid_children: List[str] = ["Tbody", "Thead", "Tfoot"]
  50. @classmethod
  51. def create(cls, *children, headers=None, **props) -> Component:
  52. """Create a table header component.
  53. Args:
  54. *children: The children of the component.
  55. headers (list, optional): List of headers. Defaults to None.
  56. **props: The properties of the component.
  57. Returns:
  58. The table header component.
  59. """
  60. if len(children) == 0:
  61. cls.validate_headers(headers)
  62. children = [Tr.create(cell_type="header", cells=headers)]
  63. return super().create(*children, **props)
  64. @staticmethod
  65. def validate_headers(headers):
  66. """Type checking for table headers.
  67. Args:
  68. headers: The table headers.
  69. Raises:
  70. TypeError: If headers are not of type list or type tuple.
  71. """
  72. allowed_types = (list, tuple)
  73. if (
  74. (
  75. isinstance(headers, Var)
  76. and not types.check_type_in_allowed_types(headers.type_, allowed_types)
  77. )
  78. or not isinstance(headers, Var)
  79. and not types.check_type_in_allowed_types(type(headers), allowed_types)
  80. ):
  81. raise TypeError("table headers should be a list or tuple")
  82. class Tbody(ChakraComponent):
  83. """A table body component."""
  84. tag = "Tbody"
  85. # invalid children components
  86. invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Td", "Th"]
  87. @classmethod
  88. def create(cls, *children, rows=None, **props) -> Component:
  89. """Create a table body component.
  90. Args:
  91. *children: The children of the component.
  92. rows (list[list], optional): The rows of the table body. Defaults to None.
  93. **props: The properties of the component.
  94. Returns:
  95. Component: The table body component
  96. """
  97. if len(children) == 0:
  98. cls.validate_rows(rows) if rows is not None else None
  99. if isinstance(rows, Var):
  100. children = [
  101. Foreach.create(
  102. rows, lambda row: Tr.create(cell_type="data", cells=row)
  103. )
  104. ]
  105. else:
  106. children = [
  107. Tr.create(cell_type="data", cells=row) for row in rows or []
  108. ]
  109. return super().create(*children, **props)
  110. @staticmethod
  111. def validate_rows(rows):
  112. """Type checking for table rows.
  113. Args:
  114. rows: Table rows.
  115. Raises:
  116. TypeError: If rows are not lists or tuples containing inner lists or tuples.
  117. """
  118. allowed_subclasses = (List, Tuple)
  119. if isinstance(rows, Var):
  120. outer_type = rows.type_
  121. inner_type = (
  122. outer_type.__args__[0] if hasattr(outer_type, "__args__") else None
  123. )
  124. # check that the outer container and inner container types are lists or tuples.
  125. if not (
  126. types._issubclass(types.get_base_class(outer_type), allowed_subclasses)
  127. and (
  128. inner_type is None
  129. or types._issubclass(
  130. types.get_base_class(inner_type), allowed_subclasses
  131. )
  132. )
  133. ):
  134. raise TypeError(
  135. f"table rows should be a list or tuple containing inner lists or tuples. Got {outer_type} instead"
  136. )
  137. elif not (
  138. types._issubclass(type(rows), allowed_subclasses)
  139. and (not rows or types._issubclass(type(rows[0]), allowed_subclasses))
  140. ):
  141. raise TypeError(
  142. "table rows should be a list or tuple containing inner lists or tuples."
  143. )
  144. class Tfoot(ChakraComponent):
  145. """A table footer component."""
  146. tag = "Tfoot"
  147. # invalid children components
  148. invalid_children: List[str] = ["Tbody", "Thead", "Td", "Th", "Tfoot"]
  149. @classmethod
  150. def create(cls, *children, footers=None, **props) -> Component:
  151. """Create a table footer component.
  152. Args:
  153. *children: The children of the component.
  154. footers (list, optional): List of footers. Defaults to None.
  155. **props: The properties of the component.
  156. Returns:
  157. The table footer component.
  158. """
  159. if len(children) == 0:
  160. cls.validate_footers(footers)
  161. children = [Tr.create(cell_type="header", cells=footers)]
  162. return super().create(*children, **props)
  163. @staticmethod
  164. def validate_footers(footers):
  165. """Type checking for table footers.
  166. Args:
  167. footers: Table rows.
  168. Raises:
  169. TypeError: If footers are not of type list.
  170. """
  171. allowed_types = (list, tuple)
  172. if (
  173. (
  174. isinstance(footers, Var)
  175. and not types.check_type_in_allowed_types(footers.type_, allowed_types)
  176. )
  177. or not isinstance(footers, Var)
  178. and not types.check_type_in_allowed_types(type(footers), allowed_types)
  179. ):
  180. raise TypeError("table headers should be a list or tuple")
  181. class Tr(ChakraComponent):
  182. """A table row component."""
  183. tag = "Tr"
  184. # invalid children components
  185. invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Tr"]
  186. @classmethod
  187. def create(cls, *children, cell_type: str = "", cells=None, **props) -> Component:
  188. """Create a table row component.
  189. Args:
  190. *children: The children of the component.
  191. cell_type: the type of cells in this table row. "header" or "data". Defaults to None.
  192. cells: The cells value to add in the table row. Defaults to None.
  193. **props: The properties of the component.
  194. Returns:
  195. The table row component
  196. """
  197. types = {"header": Th, "data": Td}
  198. cell_cls = types.get(cell_type)
  199. if len(children) == 0 and cell_cls:
  200. if isinstance(cells, Var):
  201. children = [Foreach.create(cells, cell_cls.create)]
  202. else:
  203. children = [cell_cls.create(cell) for cell in cells or []]
  204. return super().create(*children, **props)
  205. class Th(ChakraComponent):
  206. """A table header cell component."""
  207. tag = "Th"
  208. # invalid children components
  209. invalid_children: List[str] = ["Tbody", "Thead", "Tr", "Td", "Th"]
  210. # Aligns the cell content to the right.
  211. is_numeric: Var[bool]
  212. class Td(ChakraComponent):
  213. """A table data cell component."""
  214. tag = "Td"
  215. # invalid children components
  216. invalid_children: List[str] = ["Tbody", "Thead"]
  217. # Aligns the cell content to the right.
  218. is_numeric: Var[bool]
  219. class TableCaption(ChakraComponent):
  220. """A table caption component."""
  221. tag = "TableCaption"
  222. # The placement of the table caption. This sets the `caption-side` CSS attribute.
  223. placement: Var[str]
  224. class TableContainer(ChakraComponent):
  225. """The table container component renders a div that wraps the table component."""
  226. tag = "TableContainer"