@lminii/material-react-table-mcp 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/README.md +68 -0
  2. package/data/MIGRATION.md +136 -0
  3. package/data/api.json +5379 -0
  4. package/data/examples/advanced.js +215 -0
  5. package/data/examples/advanced.tsx +309 -0
  6. package/data/examples/aggregation-and-grouping.js +116 -0
  7. package/data/examples/aggregation-and-grouping.tsx +153 -0
  8. package/data/examples/aggregation-multi.js +89 -0
  9. package/data/examples/aggregation-multi.tsx +105 -0
  10. package/data/examples/alternate-column-filtering.js +38 -0
  11. package/data/examples/alternate-column-filtering.tsx +49 -0
  12. package/data/examples/alternate-detail-panel.js +65 -0
  13. package/data/examples/alternate-detail-panel.tsx +81 -0
  14. package/data/examples/alternate-pagination.js +17 -0
  15. package/data/examples/alternate-pagination.tsx +23 -0
  16. package/data/examples/basic.js +86 -0
  17. package/data/examples/basic.tsx +109 -0
  18. package/data/examples/chart-detail-panel.js +69 -0
  19. package/data/examples/chart-detail-panel.tsx +89 -0
  20. package/data/examples/column-actions-space.js +34 -0
  21. package/data/examples/column-actions-space.tsx +44 -0
  22. package/data/examples/column-alignment.js +58 -0
  23. package/data/examples/column-alignment.tsx +70 -0
  24. package/data/examples/custom-column-actions.js +74 -0
  25. package/data/examples/custom-column-actions.tsx +109 -0
  26. package/data/examples/custom-column-filtering-ui.js +59 -0
  27. package/data/examples/custom-column-filtering-ui.tsx +89 -0
  28. package/data/examples/custom-headless.js +85 -0
  29. package/data/examples/custom-headless.tsx +125 -0
  30. package/data/examples/custom-top-toolbar.js +60 -0
  31. package/data/examples/custom-top-toolbar.tsx +88 -0
  32. package/data/examples/customize-display-columns.js +72 -0
  33. package/data/examples/customize-display-columns.tsx +85 -0
  34. package/data/examples/customize-filter-components.js +78 -0
  35. package/data/examples/customize-filter-components.tsx +101 -0
  36. package/data/examples/customize-filter-modes.js +59 -0
  37. package/data/examples/customize-filter-modes.tsx +73 -0
  38. package/data/examples/customize-filter-variants.js +98 -0
  39. package/data/examples/customize-filter-variants.tsx +116 -0
  40. package/data/examples/customize-global-filter-component.js +37 -0
  41. package/data/examples/customize-global-filter-component.tsx +52 -0
  42. package/data/examples/customize-remove-column-grouping.js +75 -0
  43. package/data/examples/customize-remove-column-grouping.tsx +94 -0
  44. package/data/examples/customize-row-selection.js +43 -0
  45. package/data/examples/customize-row-selection.tsx +54 -0
  46. package/data/examples/customize-table-styles.js +81 -0
  47. package/data/examples/customize-table-styles.tsx +100 -0
  48. package/data/examples/disable-column-actions.js +26 -0
  49. package/data/examples/disable-column-actions.tsx +37 -0
  50. package/data/examples/disable-column-hiding.js +74 -0
  51. package/data/examples/disable-column-hiding.tsx +90 -0
  52. package/data/examples/disable-density-toggle.js +37 -0
  53. package/data/examples/disable-density-toggle.tsx +49 -0
  54. package/data/examples/dynamic-columns.js +124 -0
  55. package/data/examples/dynamic-columns.tsx +189 -0
  56. package/data/examples/editing-crud-cell.js +288 -0
  57. package/data/examples/editing-crud-cell.tsx +377 -0
  58. package/data/examples/editing-crud-modal.js +275 -0
  59. package/data/examples/editing-crud-modal.tsx +370 -0
  60. package/data/examples/editing-crud-row.js +255 -0
  61. package/data/examples/editing-crud-row.tsx +333 -0
  62. package/data/examples/editing-crud-table.js +280 -0
  63. package/data/examples/editing-crud-table.tsx +369 -0
  64. package/data/examples/editing-crud-tree.js +328 -0
  65. package/data/examples/editing-crud-tree.tsx +416 -0
  66. package/data/examples/enable-cell-actions.js +52 -0
  67. package/data/examples/enable-cell-actions.tsx +76 -0
  68. package/data/examples/enable-click-to-copy.js +33 -0
  69. package/data/examples/enable-click-to-copy.tsx +39 -0
  70. package/data/examples/enable-column-grouping.js +69 -0
  71. package/data/examples/enable-column-grouping.tsx +120 -0
  72. package/data/examples/enable-column-ordering.js +37 -0
  73. package/data/examples/enable-column-ordering.tsx +48 -0
  74. package/data/examples/enable-column-pinning.js +58 -0
  75. package/data/examples/enable-column-pinning.tsx +69 -0
  76. package/data/examples/enable-column-resizing.js +45 -0
  77. package/data/examples/enable-column-resizing.tsx +56 -0
  78. package/data/examples/enable-column-virtualization.js +19 -0
  79. package/data/examples/enable-column-virtualization.tsx +27 -0
  80. package/data/examples/enable-detail-panel-conditionally.js +61 -0
  81. package/data/examples/enable-detail-panel-conditionally.tsx +78 -0
  82. package/data/examples/enable-detail-panel-virtualized.js +64 -0
  83. package/data/examples/enable-detail-panel-virtualized.tsx +80 -0
  84. package/data/examples/enable-expanding-tree.js +87 -0
  85. package/data/examples/enable-expanding-tree.tsx +110 -0
  86. package/data/examples/enable-filter-facet-values.js +52 -0
  87. package/data/examples/enable-filter-facet-values.tsx +65 -0
  88. package/data/examples/enable-row-dragging.js +102 -0
  89. package/data/examples/enable-row-dragging.tsx +137 -0
  90. package/data/examples/enable-row-numbers-original.js +37 -0
  91. package/data/examples/enable-row-numbers-original.tsx +48 -0
  92. package/data/examples/enable-row-numbers-static.js +37 -0
  93. package/data/examples/enable-row-numbers-static.tsx +48 -0
  94. package/data/examples/enable-row-ordering.js +40 -0
  95. package/data/examples/enable-row-ordering.tsx +57 -0
  96. package/data/examples/enable-row-pinning-select.js +64 -0
  97. package/data/examples/enable-row-pinning-select.tsx +76 -0
  98. package/data/examples/enable-row-pinning-static.js +39 -0
  99. package/data/examples/enable-row-pinning-static.tsx +50 -0
  100. package/data/examples/enable-row-pinning-sticky.js +57 -0
  101. package/data/examples/enable-row-pinning-sticky.tsx +69 -0
  102. package/data/examples/enable-row-selection.js +71 -0
  103. package/data/examples/enable-row-selection.tsx +96 -0
  104. package/data/examples/enable-row-virtualization.js +85 -0
  105. package/data/examples/enable-row-virtualization.tsx +101 -0
  106. package/data/examples/enable-sticky-header.js +47 -0
  107. package/data/examples/enable-sticky-header.tsx +59 -0
  108. package/data/examples/expanding-tree-expanded.js +98 -0
  109. package/data/examples/expanding-tree-expanded.tsx +121 -0
  110. package/data/examples/expanding-tree-flat-parse.js +102 -0
  111. package/data/examples/expanding-tree-flat-parse.tsx +125 -0
  112. package/data/examples/expanding-tree-root-expanded.js +120 -0
  113. package/data/examples/expanding-tree-root-expanded.tsx +151 -0
  114. package/data/examples/export-to-csv.js +84 -0
  115. package/data/examples/export-to-csv.tsx +114 -0
  116. package/data/examples/export-to-pdf.js +76 -0
  117. package/data/examples/export-to-pdf.tsx +104 -0
  118. package/data/examples/external-toolbar.js +82 -0
  119. package/data/examples/external-toolbar.tsx +108 -0
  120. package/data/examples/font-awesome-icons.js +62 -0
  121. package/data/examples/font-awesome-icons.tsx +106 -0
  122. package/data/examples/infinite-scrolling.js +137 -0
  123. package/data/examples/infinite-scrolling.tsx +208 -0
  124. package/data/examples/lazy-detail-panel.js +164 -0
  125. package/data/examples/lazy-detail-panel.tsx +262 -0
  126. package/data/examples/lazy-sub-rows.js +118 -0
  127. package/data/examples/lazy-sub-rows.tsx +190 -0
  128. package/data/examples/linear-progress.js +48 -0
  129. package/data/examples/linear-progress.tsx +66 -0
  130. package/data/examples/loading.js +34 -0
  131. package/data/examples/loading.tsx +50 -0
  132. package/data/examples/localization-i18n-ar.js +40 -0
  133. package/data/examples/localization-i18n-ar.tsx +65 -0
  134. package/data/examples/localization-i18n-az.js +38 -0
  135. package/data/examples/localization-i18n-az.tsx +60 -0
  136. package/data/examples/localization-i18n-bg.js +38 -0
  137. package/data/examples/localization-i18n-bg.tsx +60 -0
  138. package/data/examples/localization-i18n-cs.js +38 -0
  139. package/data/examples/localization-i18n-cs.tsx +60 -0
  140. package/data/examples/localization-i18n-da.js +38 -0
  141. package/data/examples/localization-i18n-da.tsx +60 -0
  142. package/data/examples/localization-i18n-de.js +38 -0
  143. package/data/examples/localization-i18n-de.tsx +60 -0
  144. package/data/examples/localization-i18n-el.js +38 -0
  145. package/data/examples/localization-i18n-el.tsx +60 -0
  146. package/data/examples/localization-i18n-en.js +38 -0
  147. package/data/examples/localization-i18n-en.tsx +60 -0
  148. package/data/examples/localization-i18n-es.js +38 -0
  149. package/data/examples/localization-i18n-es.tsx +60 -0
  150. package/data/examples/localization-i18n-et.js +38 -0
  151. package/data/examples/localization-i18n-et.tsx +60 -0
  152. package/data/examples/localization-i18n-fa.js +40 -0
  153. package/data/examples/localization-i18n-fa.tsx +66 -0
  154. package/data/examples/localization-i18n-fi.js +38 -0
  155. package/data/examples/localization-i18n-fi.tsx +60 -0
  156. package/data/examples/localization-i18n-fr.js +38 -0
  157. package/data/examples/localization-i18n-fr.tsx +60 -0
  158. package/data/examples/localization-i18n-he.js +40 -0
  159. package/data/examples/localization-i18n-he.tsx +66 -0
  160. package/data/examples/localization-i18n-hr.js +38 -0
  161. package/data/examples/localization-i18n-hr.tsx +60 -0
  162. package/data/examples/localization-i18n-hu.js +38 -0
  163. package/data/examples/localization-i18n-hu.tsx +60 -0
  164. package/data/examples/localization-i18n-hy.js +40 -0
  165. package/data/examples/localization-i18n-hy.tsx +65 -0
  166. package/data/examples/localization-i18n-id.js +38 -0
  167. package/data/examples/localization-i18n-id.tsx +60 -0
  168. package/data/examples/localization-i18n-it.js +38 -0
  169. package/data/examples/localization-i18n-it.tsx +60 -0
  170. package/data/examples/localization-i18n-ja.js +38 -0
  171. package/data/examples/localization-i18n-ja.tsx +60 -0
  172. package/data/examples/localization-i18n-ko.js +38 -0
  173. package/data/examples/localization-i18n-ko.tsx +60 -0
  174. package/data/examples/localization-i18n-nl.js +38 -0
  175. package/data/examples/localization-i18n-nl.tsx +60 -0
  176. package/data/examples/localization-i18n-no.js +38 -0
  177. package/data/examples/localization-i18n-no.tsx +60 -0
  178. package/data/examples/localization-i18n-np.js +38 -0
  179. package/data/examples/localization-i18n-np.tsx +60 -0
  180. package/data/examples/localization-i18n-pl.js +38 -0
  181. package/data/examples/localization-i18n-pl.tsx +60 -0
  182. package/data/examples/localization-i18n-pt-BR.js +38 -0
  183. package/data/examples/localization-i18n-pt-BR.tsx +60 -0
  184. package/data/examples/localization-i18n-pt.js +38 -0
  185. package/data/examples/localization-i18n-pt.tsx +60 -0
  186. package/data/examples/localization-i18n-ro.js +38 -0
  187. package/data/examples/localization-i18n-ro.tsx +60 -0
  188. package/data/examples/localization-i18n-ru.js +38 -0
  189. package/data/examples/localization-i18n-ru.tsx +60 -0
  190. package/data/examples/localization-i18n-sk.js +38 -0
  191. package/data/examples/localization-i18n-sk.tsx +60 -0
  192. package/data/examples/localization-i18n-sr-Cyrl-RS.js +38 -0
  193. package/data/examples/localization-i18n-sr-Cyrl-RS.tsx +60 -0
  194. package/data/examples/localization-i18n-sr-Latn-RS.js +38 -0
  195. package/data/examples/localization-i18n-sr-Latn-RS.tsx +60 -0
  196. package/data/examples/localization-i18n-sv.js +38 -0
  197. package/data/examples/localization-i18n-sv.tsx +60 -0
  198. package/data/examples/localization-i18n-tr.js +38 -0
  199. package/data/examples/localization-i18n-tr.tsx +60 -0
  200. package/data/examples/localization-i18n-uk.js +38 -0
  201. package/data/examples/localization-i18n-uk.tsx +60 -0
  202. package/data/examples/localization-i18n-vi.js +38 -0
  203. package/data/examples/localization-i18n-vi.tsx +60 -0
  204. package/data/examples/localization-i18n-zh-hans.js +38 -0
  205. package/data/examples/localization-i18n-zh-hans.tsx +60 -0
  206. package/data/examples/localization-i18n-zh-hant.js +38 -0
  207. package/data/examples/localization-i18n-zh-hant.tsx +60 -0
  208. package/data/examples/manual-selection.js +81 -0
  209. package/data/examples/manual-selection.tsx +105 -0
  210. package/data/examples/minimal.js +66 -0
  211. package/data/examples/minimal.tsx +78 -0
  212. package/data/examples/mui-theme.js +109 -0
  213. package/data/examples/mui-theme.tsx +136 -0
  214. package/data/examples/multi-sorting.js +48 -0
  215. package/data/examples/multi-sorting.tsx +58 -0
  216. package/data/examples/persistent-state.js +126 -0
  217. package/data/examples/persistent-state.tsx +184 -0
  218. package/data/examples/react-query.js +122 -0
  219. package/data/examples/react-query.tsx +182 -0
  220. package/data/examples/remote.js +113 -0
  221. package/data/examples/remote.tsx +150 -0
  222. package/data/examples/row-actions-buttons.js +54 -0
  223. package/data/examples/row-actions-buttons.tsx +89 -0
  224. package/data/examples/row-actions-menu-items.js +36 -0
  225. package/data/examples/row-actions-menu-items.tsx +64 -0
  226. package/data/examples/single-row-selection.js +74 -0
  227. package/data/examples/single-row-selection.tsx +98 -0
  228. package/data/examples/virtualized.js +122 -0
  229. package/data/examples/virtualized.tsx +138 -0
  230. package/data/guides/accessibility.md +119 -0
  231. package/data/guides/aggregation.md +210 -0
  232. package/data/guides/async-loading.md +132 -0
  233. package/data/guides/best-practices.md +363 -0
  234. package/data/guides/cell-actions.md +139 -0
  235. package/data/guides/click-to-copy.md +67 -0
  236. package/data/guides/column-actions.md +69 -0
  237. package/data/guides/column-filtering.md +366 -0
  238. package/data/guides/column-grouping.md +192 -0
  239. package/data/guides/column-hiding.md +133 -0
  240. package/data/guides/column-ordering-dnd.md +99 -0
  241. package/data/guides/column-pinning.md +72 -0
  242. package/data/guides/column-resizing.md +157 -0
  243. package/data/guides/column-size.md +162 -0
  244. package/data/guides/customize-components.md +395 -0
  245. package/data/guides/customize-icons.md +35 -0
  246. package/data/guides/data-columns.md +292 -0
  247. package/data/guides/density-toggle.md +46 -0
  248. package/data/guides/detail-panel.md +195 -0
  249. package/data/guides/display-columns.md +135 -0
  250. package/data/guides/editing.md +367 -0
  251. package/data/guides/expanding-sub-rows.md +194 -0
  252. package/data/guides/full-screen-toggle.md +63 -0
  253. package/data/guides/global-filtering.md +237 -0
  254. package/data/guides/localization.md +57 -0
  255. package/data/guides/memoization.md +157 -0
  256. package/data/guides/pagination.md +171 -0
  257. package/data/guides/row-actions.md +116 -0
  258. package/data/guides/row-numbers.md +24 -0
  259. package/data/guides/row-ordering-dnd.md +51 -0
  260. package/data/guides/row-pinning.md +96 -0
  261. package/data/guides/row-selection.md +237 -0
  262. package/data/guides/sorting.md +190 -0
  263. package/data/guides/state-management.md +165 -0
  264. package/data/guides/sticky-header.md +41 -0
  265. package/data/guides/table-event-listeners.md +103 -0
  266. package/data/guides/toolbar-customization.md +251 -0
  267. package/data/guides/virtualization.md +183 -0
  268. package/data/index.json +1930 -0
  269. package/data/reference/mrt-components.md +183 -0
  270. package/data/reference/mrt-hooks.md +125 -0
  271. package/data/skills/README.md +81 -0
  272. package/data/skills/composable-components/SKILL.md +189 -0
  273. package/data/skills/customization/SKILL.md +190 -0
  274. package/data/skills/drag-and-drop-ordering/SKILL.md +227 -0
  275. package/data/skills/editing/SKILL.md +243 -0
  276. package/data/skills/filtering/SKILL.md +213 -0
  277. package/data/skills/getting-started/SKILL.md +213 -0
  278. package/data/skills/localization/SKILL.md +182 -0
  279. package/data/skills/migrate-v3-to-v4/SKILL.md +161 -0
  280. package/data/skills/state-and-server-data/SKILL.md +200 -0
  281. package/data/skills/virtualization/SKILL.md +203 -0
  282. package/dist/data.js +159 -0
  283. package/dist/index.js +244 -0
  284. package/package.json +50 -0
@@ -0,0 +1,118 @@
1
+ import { lazy, Suspense, useMemo, useState } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { QueryClient, QueryClientProvider, keepPreviousData, useQuery, } from '@tanstack/react-query'; //note: this is TanStack React Query V5
4
+ const columns = [
5
+ //column definitions...
6
+ {
7
+ accessorKey: 'firstName',
8
+ header: 'First Name',
9
+ },
10
+ {
11
+ accessorKey: 'lastName',
12
+ header: 'Last Name',
13
+ },
14
+ {
15
+ accessorKey: 'email',
16
+ header: 'Email',
17
+ },
18
+ {
19
+ accessorKey: 'state',
20
+ header: 'State',
21
+ },
22
+ //end
23
+ ];
24
+ const Example = () => {
25
+ const [sorting, setSorting] = useState([]);
26
+ const [pagination, setPagination] = useState({
27
+ pageIndex: 0,
28
+ pageSize: 10,
29
+ });
30
+ const [expanded, setExpanded] = useState({}); //Record<string, boolean> | true
31
+ //which rows have sub-rows expanded and need their direct sub-rows to be included in the API call
32
+ const expandedRowIds = useMemo(() => expanded === true
33
+ ? 'all'
34
+ : Object.entries(expanded)
35
+ .filter(([_managerId, isExpanded]) => isExpanded)
36
+ .map(([managerId]) => managerId), [expanded]);
37
+ const { data: { data = [], meta } = {}, isError, isRefetching, isLoading, } = useFetchUsers({
38
+ pagination,
39
+ sorting,
40
+ expandedRowIds,
41
+ });
42
+ //get data for root rows only (top of the tree data)
43
+ const rootData = useMemo(() => data.filter((r) => !r.managerId), [data]);
44
+ const table = useMaterialReactTable({
45
+ columns,
46
+ data: rootData,
47
+ enableExpanding: true, //enable expanding column
48
+ enableFilters: false,
49
+ //tell MRT which rows have additional sub-rows that can be fetched
50
+ getRowCanExpand: (row) => !!row.original.subordinateIds.length, //just some type of boolean
51
+ //identify rows by the user's id
52
+ getRowId: (row) => row.id,
53
+ //if data is delivered in a flat array, MRT can convert it to a tree structure
54
+ //though it's usually better if the API can construct the nested structure before this point
55
+ getSubRows: (row) => data.filter((r) => r.managerId === row.id), //parse flat array into tree structure
56
+ // paginateExpandedRows: false, //the back-end in this example is acting as if this option is false
57
+ manualPagination: true, //turn off built-in client-side pagination
58
+ manualSorting: true, //turn off built-in client-side sorting
59
+ muiToolbarAlertBannerProps: isError
60
+ ? {
61
+ color: 'error',
62
+ children: 'Error loading data',
63
+ }
64
+ : undefined,
65
+ onExpandedChange: setExpanded,
66
+ onPaginationChange: setPagination,
67
+ onSortingChange: setSorting,
68
+ rowCount: meta?.totalRowCount ?? 0,
69
+ state: {
70
+ expanded,
71
+ isLoading,
72
+ pagination,
73
+ showAlertBanner: isError,
74
+ showProgressBars: isRefetching,
75
+ sorting,
76
+ },
77
+ });
78
+ return <MaterialReactTable table={table}/>;
79
+ };
80
+ //react query setup in App.tsx
81
+ const ReactQueryDevtoolsProduction = lazy(() => import('@tanstack/react-query-devtools/build/modern/production.js').then((d) => ({
82
+ default: d.ReactQueryDevtools,
83
+ })));
84
+ const queryClient = new QueryClient();
85
+ export default function App() {
86
+ return (<QueryClientProvider client={queryClient}>
87
+ <Example />
88
+ <Suspense fallback={null}>
89
+ <ReactQueryDevtoolsProduction />
90
+ </Suspense>
91
+ </QueryClientProvider>);
92
+ }
93
+ //fetch user hook
94
+ const useFetchUsers = ({ pagination, sorting, expandedRowIds, }) => {
95
+ return useQuery({
96
+ queryKey: [
97
+ 'users', //give a unique key for this query
98
+ {
99
+ pagination, //refetch when pagination changes
100
+ sorting, //refetch when sorting changes
101
+ expandedRowIds,
102
+ },
103
+ ],
104
+ queryFn: async () => {
105
+ const fetchURL = new URL('/api/treedata', location.origin); // nextjs api route
106
+ //read our state and pass it to the API as query params
107
+ fetchURL.searchParams.set('start', `${pagination.pageIndex * pagination.pageSize}`);
108
+ fetchURL.searchParams.set('size', `${pagination.pageSize}`);
109
+ fetchURL.searchParams.set('sorting', JSON.stringify(sorting ?? []));
110
+ fetchURL.searchParams.set('expandedRowIds', expandedRowIds === 'all' ? 'all' : JSON.stringify(expandedRowIds ?? []));
111
+ //use whatever fetch library you want, fetch, axios, etc
112
+ const response = await fetch(fetchURL.href);
113
+ const json = (await response.json());
114
+ return json;
115
+ },
116
+ placeholderData: keepPreviousData, //don't go to 0 rows when refetching or paginating to next page
117
+ });
118
+ };
@@ -0,0 +1,190 @@
1
+ import { lazy, Suspense, useMemo, useState } from 'react';
2
+ import {
3
+ MaterialReactTable,
4
+ useMaterialReactTable,
5
+ type MRT_ColumnDef,
6
+ type MRT_PaginationState,
7
+ type MRT_SortingState,
8
+ type MRT_ExpandedState,
9
+ } from 'material-react-table';
10
+ import {
11
+ QueryClient,
12
+ QueryClientProvider,
13
+ keepPreviousData,
14
+ useQuery,
15
+ } from '@tanstack/react-query'; //note: this is TanStack React Query V5
16
+
17
+ //Your API response shape will probably be different. Knowing a total row count is important though.
18
+ type UserApiResponse = {
19
+ data: Array<User>;
20
+ meta: {
21
+ totalRowCount: number;
22
+ };
23
+ };
24
+
25
+ type User = {
26
+ id: string;
27
+ firstName: string;
28
+ lastName: string;
29
+ email: string;
30
+ state: string;
31
+ managerId: string | null; //row's parent row id
32
+ subordinateIds: string[]; //or some type of boolean that indicates that there are sub-rows
33
+ };
34
+
35
+ const columns: MRT_ColumnDef<User>[] = [
36
+ //column definitions...
37
+ {
38
+ accessorKey: 'firstName',
39
+ header: 'First Name',
40
+ },
41
+ {
42
+ accessorKey: 'lastName',
43
+ header: 'Last Name',
44
+ },
45
+ {
46
+ accessorKey: 'email',
47
+ header: 'Email',
48
+ },
49
+ {
50
+ accessorKey: 'state',
51
+ header: 'State',
52
+ },
53
+ //end
54
+ ];
55
+
56
+ const Example = () => {
57
+ const [sorting, setSorting] = useState<MRT_SortingState>([]);
58
+ const [pagination, setPagination] = useState<MRT_PaginationState>({
59
+ pageIndex: 0,
60
+ pageSize: 10,
61
+ });
62
+ const [expanded, setExpanded] = useState<MRT_ExpandedState>({}); //Record<string, boolean> | true
63
+
64
+ //which rows have sub-rows expanded and need their direct sub-rows to be included in the API call
65
+ const expandedRowIds: string[] | 'all' = useMemo(
66
+ () =>
67
+ expanded === true
68
+ ? 'all'
69
+ : Object.entries(expanded)
70
+ .filter(([_managerId, isExpanded]) => isExpanded)
71
+ .map(([managerId]) => managerId),
72
+ [expanded],
73
+ );
74
+
75
+ const {
76
+ data: { data = [], meta } = {},
77
+ isError,
78
+ isRefetching,
79
+ isLoading,
80
+ } = useFetchUsers({
81
+ pagination,
82
+ sorting,
83
+ expandedRowIds,
84
+ });
85
+
86
+ //get data for root rows only (top of the tree data)
87
+ const rootData = useMemo(() => data.filter((r) => !r.managerId), [data]);
88
+
89
+ const table = useMaterialReactTable({
90
+ columns,
91
+ data: rootData,
92
+ enableExpanding: true, //enable expanding column
93
+ enableFilters: false,
94
+ //tell MRT which rows have additional sub-rows that can be fetched
95
+ getRowCanExpand: (row) => !!row.original.subordinateIds.length, //just some type of boolean
96
+ //identify rows by the user's id
97
+ getRowId: (row) => row.id,
98
+ //if data is delivered in a flat array, MRT can convert it to a tree structure
99
+ //though it's usually better if the API can construct the nested structure before this point
100
+ getSubRows: (row) => data.filter((r) => r.managerId === row.id), //parse flat array into tree structure
101
+ // paginateExpandedRows: false, //the back-end in this example is acting as if this option is false
102
+ manualPagination: true, //turn off built-in client-side pagination
103
+ manualSorting: true, //turn off built-in client-side sorting
104
+ muiToolbarAlertBannerProps: isError
105
+ ? {
106
+ color: 'error',
107
+ children: 'Error loading data',
108
+ }
109
+ : undefined,
110
+ onExpandedChange: setExpanded,
111
+ onPaginationChange: setPagination,
112
+ onSortingChange: setSorting,
113
+ rowCount: meta?.totalRowCount ?? 0,
114
+ state: {
115
+ expanded,
116
+ isLoading,
117
+ pagination,
118
+ showAlertBanner: isError,
119
+ showProgressBars: isRefetching,
120
+ sorting,
121
+ },
122
+ });
123
+
124
+ return <MaterialReactTable table={table} />;
125
+ };
126
+
127
+ //react query setup in App.tsx
128
+ const ReactQueryDevtoolsProduction = lazy(() =>
129
+ import('@tanstack/react-query-devtools/build/modern/production.js').then(
130
+ (d) => ({
131
+ default: d.ReactQueryDevtools,
132
+ }),
133
+ ),
134
+ );
135
+
136
+ const queryClient = new QueryClient();
137
+
138
+ export default function App() {
139
+ return (
140
+ <QueryClientProvider client={queryClient}>
141
+ <Example />
142
+ <Suspense fallback={null}>
143
+ <ReactQueryDevtoolsProduction />
144
+ </Suspense>
145
+ </QueryClientProvider>
146
+ );
147
+ }
148
+
149
+ //fetch user hook
150
+ const useFetchUsers = ({
151
+ pagination,
152
+ sorting,
153
+ expandedRowIds,
154
+ }: {
155
+ pagination: MRT_PaginationState;
156
+ sorting: MRT_SortingState;
157
+ expandedRowIds: string[] | 'all';
158
+ }) => {
159
+ return useQuery<UserApiResponse>({
160
+ queryKey: [
161
+ 'users', //give a unique key for this query
162
+ {
163
+ pagination, //refetch when pagination changes
164
+ sorting, //refetch when sorting changes
165
+ expandedRowIds,
166
+ },
167
+ ],
168
+ queryFn: async () => {
169
+ const fetchURL = new URL('/api/treedata', location.origin); // nextjs api route
170
+
171
+ //read our state and pass it to the API as query params
172
+ fetchURL.searchParams.set(
173
+ 'start',
174
+ `${pagination.pageIndex * pagination.pageSize}`,
175
+ );
176
+ fetchURL.searchParams.set('size', `${pagination.pageSize}`);
177
+ fetchURL.searchParams.set('sorting', JSON.stringify(sorting ?? []));
178
+ fetchURL.searchParams.set(
179
+ 'expandedRowIds',
180
+ expandedRowIds === 'all' ? 'all' : JSON.stringify(expandedRowIds ?? []),
181
+ );
182
+
183
+ //use whatever fetch library you want, fetch, axios, etc
184
+ const response = await fetch(fetchURL.href);
185
+ const json = (await response.json()) as UserApiResponse;
186
+ return json;
187
+ },
188
+ placeholderData: keepPreviousData, //don't go to 0 rows when refetching or paginating to next page
189
+ });
190
+ };
@@ -0,0 +1,48 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import { MaterialReactTable } from 'material-react-table';
3
+ import { data } from './makeData';
4
+ import { Button } from '@mui/material';
5
+ const Example = () => {
6
+ const columns = useMemo(
7
+ //column definitions...
8
+ () => [
9
+ {
10
+ accessorKey: 'firstName',
11
+ header: 'First Name',
12
+ },
13
+ {
14
+ accessorKey: 'lastName',
15
+ header: 'Last Name',
16
+ },
17
+ {
18
+ accessorKey: 'email',
19
+ header: 'Email',
20
+ },
21
+ {
22
+ accessorKey: 'city',
23
+ header: 'City',
24
+ },
25
+ ], []);
26
+ const [progress, setProgress] = useState(0);
27
+ //simulate random progress for demo purposes
28
+ useEffect(() => {
29
+ const interval = setInterval(() => {
30
+ setProgress((oldProgress) => {
31
+ const newProgress = Math.random() * 20;
32
+ return Math.min(oldProgress + newProgress, 100);
33
+ });
34
+ }, 1000);
35
+ return () => clearInterval(interval);
36
+ }, []);
37
+ return (<MaterialReactTable columns={columns} data={data} muiLinearProgressProps={({ isTopToolbar }) => ({
38
+ color: 'secondary',
39
+ variant: 'determinate', //if you want to show exact progress value
40
+ value: progress, //value between 0 and 100
41
+ sx: {
42
+ display: isTopToolbar ? 'block' : 'none', //hide bottom progress bar
43
+ },
44
+ })} renderTopToolbarCustomActions={() => (<Button onClick={() => setProgress(0)} variant="contained">
45
+ Reset
46
+ </Button>)} state={{ showProgressBars: true }}/>);
47
+ };
48
+ export default Example;
@@ -0,0 +1,66 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';
3
+ import { data, type Person } from './makeData';
4
+ import { Button } from '@mui/material';
5
+
6
+ const Example = () => {
7
+ const columns = useMemo<MRT_ColumnDef<Person>[]>(
8
+ //column definitions...
9
+ () => [
10
+ {
11
+ accessorKey: 'firstName',
12
+ header: 'First Name',
13
+ },
14
+ {
15
+ accessorKey: 'lastName',
16
+ header: 'Last Name',
17
+ },
18
+ {
19
+ accessorKey: 'email',
20
+ header: 'Email',
21
+ },
22
+ {
23
+ accessorKey: 'city',
24
+ header: 'City',
25
+ },
26
+ ],
27
+ [],
28
+ //end
29
+ );
30
+
31
+ const [progress, setProgress] = useState(0);
32
+
33
+ //simulate random progress for demo purposes
34
+ useEffect(() => {
35
+ const interval = setInterval(() => {
36
+ setProgress((oldProgress) => {
37
+ const newProgress = Math.random() * 20;
38
+ return Math.min(oldProgress + newProgress, 100);
39
+ });
40
+ }, 1000);
41
+ return () => clearInterval(interval);
42
+ }, []);
43
+
44
+ return (
45
+ <MaterialReactTable
46
+ columns={columns}
47
+ data={data}
48
+ muiLinearProgressProps={({ isTopToolbar }) => ({
49
+ color: 'secondary',
50
+ variant: 'determinate', //if you want to show exact progress value
51
+ value: progress, //value between 0 and 100
52
+ sx: {
53
+ display: isTopToolbar ? 'block' : 'none', //hide bottom progress bar
54
+ },
55
+ })}
56
+ renderTopToolbarCustomActions={() => (
57
+ <Button onClick={() => setProgress(0)} variant="contained">
58
+ Reset
59
+ </Button>
60
+ )}
61
+ state={{ showProgressBars: true }}
62
+ />
63
+ );
64
+ };
65
+
66
+ export default Example;
@@ -0,0 +1,34 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable } from 'material-react-table';
3
+ const data = [];
4
+ const Example = () => {
5
+ const columns = useMemo(
6
+ //column definitions...
7
+ () => [
8
+ {
9
+ accessorKey: 'firstName',
10
+ header: 'First Name',
11
+ },
12
+ {
13
+ accessorKey: 'lastName',
14
+ header: 'Last Name',
15
+ },
16
+ {
17
+ accessorKey: 'email',
18
+ header: 'Email',
19
+ },
20
+ {
21
+ accessorKey: 'city',
22
+ header: 'City',
23
+ },
24
+ ], []);
25
+ return (<MaterialReactTable columns={columns} data={data} state={{ isLoading: true }} muiCircularProgressProps={{
26
+ color: 'secondary',
27
+ thickness: 5,
28
+ size: 55,
29
+ }} muiSkeletonProps={{
30
+ animation: 'pulse',
31
+ height: 28,
32
+ }}/>);
33
+ };
34
+ export default Example;
@@ -0,0 +1,50 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';
3
+ import { type Person } from './makeData';
4
+
5
+ const data: Array<Person> = [];
6
+
7
+ const Example = () => {
8
+ const columns = useMemo<MRT_ColumnDef<Person>[]>(
9
+ //column definitions...
10
+ () => [
11
+ {
12
+ accessorKey: 'firstName',
13
+ header: 'First Name',
14
+ },
15
+ {
16
+ accessorKey: 'lastName',
17
+ header: 'Last Name',
18
+ },
19
+ {
20
+ accessorKey: 'email',
21
+ header: 'Email',
22
+ },
23
+ {
24
+ accessorKey: 'city',
25
+ header: 'City',
26
+ },
27
+ ],
28
+ [],
29
+ //end
30
+ );
31
+
32
+ return (
33
+ <MaterialReactTable
34
+ columns={columns}
35
+ data={data}
36
+ state={{ isLoading: true }}
37
+ muiCircularProgressProps={{
38
+ color: 'secondary',
39
+ thickness: 5,
40
+ size: 55,
41
+ }}
42
+ muiSkeletonProps={{
43
+ animation: 'pulse',
44
+ height: 28,
45
+ }}
46
+ />
47
+ );
48
+ };
49
+
50
+ export default Example;
@@ -0,0 +1,40 @@
1
+ //Import Material React Table and its Types
2
+ import { MaterialReactTable } from 'material-react-table';
3
+ //Import Material React Table Translations
4
+ import { MRT_Localization_AR } from 'material-react-table/locales/ar';
5
+ //mock data
6
+ import { data } from './makeData';
7
+ const columns = [
8
+ //column definitions...
9
+ {
10
+ accessorKey: 'firstName',
11
+ header: 'الاسم الأول',
12
+ },
13
+ {
14
+ accessorKey: 'lastName',
15
+ header: 'اسم العائلة',
16
+ enableClickToCopy: true,
17
+ },
18
+ {
19
+ accessorKey: 'age',
20
+ header: 'العمر',
21
+ },
22
+ //end
23
+ ];
24
+ const Example = () => {
25
+ return (<MaterialReactTable columns={columns} data={data} defaultColumn={{ size: 250 }} columnResizeDirection="rtl" enableColumnFilterModes enableColumnOrdering enableColumnResizing enableEditing enableColumnPinning enableRowActions enableRowSelection enableSelectAll={false} initialState={{ showColumnFilters: true, showGlobalFilter: true }} localization={MRT_Localization_AR}/>);
26
+ };
27
+ //App.tsx or similar
28
+ import { createTheme, ThemeProvider, useTheme } from '@mui/material';
29
+ import { arSA } from '@mui/material/locale';
30
+ const ExampleWithThemeProvider = () => {
31
+ const theme = useTheme(); //replace with your theme/createTheme
32
+ return (
33
+ //Setting Material UI locale as best practice to result in better accessibility
34
+ <ThemeProvider theme={createTheme({ ...theme, direction: 'rtl' }, arSA)}>
35
+ <div style={{ direction: 'rtl' }}>
36
+ <Example />
37
+ </div>
38
+ </ThemeProvider>);
39
+ };
40
+ export default ExampleWithThemeProvider;
@@ -0,0 +1,65 @@
1
+ //Import Material React Table and its Types
2
+ import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';
3
+
4
+ //Import Material React Table Translations
5
+ import { MRT_Localization_AR } from 'material-react-table/locales/ar';
6
+
7
+ //mock data
8
+ import { data, type Person } from './makeData';
9
+
10
+ const columns: MRT_ColumnDef<Person>[] = [
11
+ //column definitions...
12
+ {
13
+ accessorKey: 'firstName',
14
+ header: 'الاسم الأول',
15
+ },
16
+ {
17
+ accessorKey: 'lastName',
18
+ header: 'اسم العائلة',
19
+ enableClickToCopy: true,
20
+ },
21
+ {
22
+ accessorKey: 'age',
23
+ header: 'العمر',
24
+ },
25
+ //end
26
+ ];
27
+
28
+ const Example = () => {
29
+ return (
30
+ <MaterialReactTable
31
+ columns={columns}
32
+ data={data}
33
+ defaultColumn={{ size: 250 }}
34
+ columnResizeDirection="rtl"
35
+ enableColumnFilterModes
36
+ enableColumnOrdering
37
+ enableColumnResizing
38
+ enableEditing
39
+ enableColumnPinning
40
+ enableRowActions
41
+ enableRowSelection
42
+ enableSelectAll={false}
43
+ initialState={{ showColumnFilters: true, showGlobalFilter: true }}
44
+ localization={MRT_Localization_AR}
45
+ />
46
+ );
47
+ };
48
+
49
+ //App.tsx or similar
50
+ import { createTheme, ThemeProvider, useTheme } from '@mui/material';
51
+ import { arSA } from '@mui/material/locale';
52
+
53
+ const ExampleWithThemeProvider = () => {
54
+ const theme = useTheme(); //replace with your theme/createTheme
55
+ return (
56
+ //Setting Material UI locale as best practice to result in better accessibility
57
+ <ThemeProvider theme={createTheme({ ...theme, direction: 'rtl' }, arSA)}>
58
+ <div style={{ direction: 'rtl' }}>
59
+ <Example />
60
+ </div>
61
+ </ThemeProvider>
62
+ );
63
+ };
64
+
65
+ export default ExampleWithThemeProvider;
@@ -0,0 +1,38 @@
1
+ //Import Material React Table and its Types
2
+ import { MaterialReactTable } from 'material-react-table';
3
+ //Import Material React Table Translations
4
+ import { MRT_Localization_AZ } from 'material-react-table/locales/az';
5
+ //mock data
6
+ import { data } from './makeData';
7
+ const columns = [
8
+ //column definitions...
9
+ {
10
+ accessorKey: 'firstName',
11
+ header: 'Ad',
12
+ },
13
+ {
14
+ accessorKey: 'lastName',
15
+ header: 'Soyad',
16
+ enableClickToCopy: true,
17
+ },
18
+ {
19
+ accessorKey: 'age',
20
+ header: 'Yaş',
21
+ },
22
+ //end
23
+ ];
24
+ const Example = () => {
25
+ return (<MaterialReactTable columns={columns} data={data} enableColumnFilterModes enableColumnOrdering enableEditing enableColumnPinning enableRowActions enableRowSelection enableSelectAll={false} initialState={{ showColumnFilters: true, showGlobalFilter: true }} localization={MRT_Localization_AZ}/>);
26
+ };
27
+ //App.tsx or similar
28
+ import { createTheme, ThemeProvider, useTheme } from '@mui/material';
29
+ import { azAZ } from '@mui/material/locale';
30
+ const ExampleWithThemeProvider = () => {
31
+ const theme = useTheme(); //replace with your theme/createTheme
32
+ return (
33
+ //Setting Material UI locale as best practice to result in better accessibility
34
+ <ThemeProvider theme={createTheme(theme, azAZ)}>
35
+ <Example />
36
+ </ThemeProvider>);
37
+ };
38
+ export default ExampleWithThemeProvider;