table.py 9.1 KB

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