@mini_7/material-react-table-mcp 4.0.0 → 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 (117) hide show
  1. package/README.md +1 -1
  2. package/data/examples/advanced.js +215 -0
  3. package/data/examples/aggregation-and-grouping.js +116 -0
  4. package/data/examples/aggregation-multi.js +89 -0
  5. package/data/examples/alternate-column-filtering.js +38 -0
  6. package/data/examples/alternate-detail-panel.js +65 -0
  7. package/data/examples/alternate-pagination.js +17 -0
  8. package/data/examples/basic.js +86 -0
  9. package/data/examples/chart-detail-panel.js +69 -0
  10. package/data/examples/column-actions-space.js +34 -0
  11. package/data/examples/column-alignment.js +58 -0
  12. package/data/examples/custom-column-actions.js +74 -0
  13. package/data/examples/custom-column-filtering-ui.js +59 -0
  14. package/data/examples/custom-headless.js +85 -0
  15. package/data/examples/custom-top-toolbar.js +60 -0
  16. package/data/examples/customize-display-columns.js +72 -0
  17. package/data/examples/customize-filter-components.js +78 -0
  18. package/data/examples/customize-filter-modes.js +59 -0
  19. package/data/examples/customize-filter-variants.js +98 -0
  20. package/data/examples/customize-global-filter-component.js +37 -0
  21. package/data/examples/customize-remove-column-grouping.js +75 -0
  22. package/data/examples/customize-row-selection.js +43 -0
  23. package/data/examples/customize-table-styles.js +81 -0
  24. package/data/examples/disable-column-actions.js +26 -0
  25. package/data/examples/disable-column-hiding.js +74 -0
  26. package/data/examples/disable-density-toggle.js +37 -0
  27. package/data/examples/dynamic-columns.js +124 -0
  28. package/data/examples/editing-crud-cell.js +288 -0
  29. package/data/examples/editing-crud-modal.js +275 -0
  30. package/data/examples/editing-crud-row.js +255 -0
  31. package/data/examples/editing-crud-table.js +280 -0
  32. package/data/examples/editing-crud-tree.js +328 -0
  33. package/data/examples/enable-cell-actions.js +52 -0
  34. package/data/examples/enable-click-to-copy.js +33 -0
  35. package/data/examples/enable-column-grouping.js +69 -0
  36. package/data/examples/enable-column-ordering.js +37 -0
  37. package/data/examples/enable-column-pinning.js +58 -0
  38. package/data/examples/enable-column-resizing.js +45 -0
  39. package/data/examples/enable-column-virtualization.js +19 -0
  40. package/data/examples/enable-detail-panel-conditionally.js +61 -0
  41. package/data/examples/enable-detail-panel-virtualized.js +64 -0
  42. package/data/examples/enable-expanding-tree.js +87 -0
  43. package/data/examples/enable-filter-facet-values.js +52 -0
  44. package/data/examples/enable-row-dragging.js +102 -0
  45. package/data/examples/enable-row-numbers-original.js +37 -0
  46. package/data/examples/enable-row-numbers-static.js +37 -0
  47. package/data/examples/enable-row-ordering.js +40 -0
  48. package/data/examples/enable-row-pinning-select.js +64 -0
  49. package/data/examples/enable-row-pinning-static.js +39 -0
  50. package/data/examples/enable-row-pinning-sticky.js +57 -0
  51. package/data/examples/enable-row-selection.js +71 -0
  52. package/data/examples/enable-row-virtualization.js +85 -0
  53. package/data/examples/enable-sticky-header.js +47 -0
  54. package/data/examples/expanding-tree-expanded.js +98 -0
  55. package/data/examples/expanding-tree-flat-parse.js +102 -0
  56. package/data/examples/expanding-tree-root-expanded.js +120 -0
  57. package/data/examples/export-to-csv.js +84 -0
  58. package/data/examples/export-to-pdf.js +76 -0
  59. package/data/examples/external-toolbar.js +82 -0
  60. package/data/examples/font-awesome-icons.js +62 -0
  61. package/data/examples/infinite-scrolling.js +137 -0
  62. package/data/examples/lazy-detail-panel.js +164 -0
  63. package/data/examples/lazy-sub-rows.js +118 -0
  64. package/data/examples/linear-progress.js +48 -0
  65. package/data/examples/loading.js +34 -0
  66. package/data/examples/localization-i18n-ar.js +40 -0
  67. package/data/examples/localization-i18n-az.js +38 -0
  68. package/data/examples/localization-i18n-bg.js +38 -0
  69. package/data/examples/localization-i18n-cs.js +38 -0
  70. package/data/examples/localization-i18n-da.js +38 -0
  71. package/data/examples/localization-i18n-de.js +38 -0
  72. package/data/examples/localization-i18n-el.js +38 -0
  73. package/data/examples/localization-i18n-en.js +38 -0
  74. package/data/examples/localization-i18n-es.js +38 -0
  75. package/data/examples/localization-i18n-et.js +38 -0
  76. package/data/examples/localization-i18n-fa.js +40 -0
  77. package/data/examples/localization-i18n-fi.js +38 -0
  78. package/data/examples/localization-i18n-fr.js +38 -0
  79. package/data/examples/localization-i18n-he.js +40 -0
  80. package/data/examples/localization-i18n-hr.js +38 -0
  81. package/data/examples/localization-i18n-hu.js +38 -0
  82. package/data/examples/localization-i18n-hy.js +40 -0
  83. package/data/examples/localization-i18n-id.js +38 -0
  84. package/data/examples/localization-i18n-it.js +38 -0
  85. package/data/examples/localization-i18n-ja.js +38 -0
  86. package/data/examples/localization-i18n-ko.js +38 -0
  87. package/data/examples/localization-i18n-nl.js +38 -0
  88. package/data/examples/localization-i18n-no.js +38 -0
  89. package/data/examples/localization-i18n-np.js +38 -0
  90. package/data/examples/localization-i18n-pl.js +38 -0
  91. package/data/examples/localization-i18n-pt-BR.js +38 -0
  92. package/data/examples/localization-i18n-pt.js +38 -0
  93. package/data/examples/localization-i18n-ro.js +38 -0
  94. package/data/examples/localization-i18n-ru.js +38 -0
  95. package/data/examples/localization-i18n-sk.js +38 -0
  96. package/data/examples/localization-i18n-sr-Cyrl-RS.js +38 -0
  97. package/data/examples/localization-i18n-sr-Latn-RS.js +38 -0
  98. package/data/examples/localization-i18n-sv.js +38 -0
  99. package/data/examples/localization-i18n-tr.js +38 -0
  100. package/data/examples/localization-i18n-uk.js +38 -0
  101. package/data/examples/localization-i18n-vi.js +38 -0
  102. package/data/examples/localization-i18n-zh-hans.js +38 -0
  103. package/data/examples/localization-i18n-zh-hant.js +38 -0
  104. package/data/examples/manual-selection.js +81 -0
  105. package/data/examples/minimal.js +66 -0
  106. package/data/examples/mui-theme.js +109 -0
  107. package/data/examples/multi-sorting.js +48 -0
  108. package/data/examples/persistent-state.js +126 -0
  109. package/data/examples/react-query.js +122 -0
  110. package/data/examples/remote.js +113 -0
  111. package/data/examples/row-actions-buttons.js +54 -0
  112. package/data/examples/row-actions-menu-items.js +36 -0
  113. package/data/examples/single-row-selection.js +74 -0
  114. package/data/examples/virtualized.js +122 -0
  115. package/dist/data.js +31 -9
  116. package/dist/index.js +18 -6
  117. package/package.json +1 -1
@@ -0,0 +1,98 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { citiesList, data, usStateList } from './makeData';
4
+ const Example = () => {
5
+ const columns = useMemo(() => [
6
+ {
7
+ header: 'Status',
8
+ accessorFn: (originalRow) => (originalRow.isActive ? 'true' : 'false'), //must be strings
9
+ id: 'isActive',
10
+ filterVariant: 'checkbox',
11
+ Cell: ({ cell }) => cell.getValue() === 'true' ? 'Active' : 'Inactive',
12
+ size: 170,
13
+ },
14
+ {
15
+ accessorKey: 'name',
16
+ header: 'Name',
17
+ filterVariant: 'text', // default
18
+ size: 200,
19
+ },
20
+ {
21
+ accessorKey: 'salary',
22
+ header: 'Salary',
23
+ Cell: ({ cell }) => cell.getValue().toLocaleString('en-US', {
24
+ style: 'currency',
25
+ currency: 'USD',
26
+ }),
27
+ filterVariant: 'range-slider',
28
+ filterFn: 'betweenInclusive', // default (or between)
29
+ muiFilterSliderProps: {
30
+ marks: true,
31
+ max: 200000, //custom max (as opposed to faceted max)
32
+ min: 30000, //custom min (as opposed to faceted min)
33
+ step: 10000,
34
+ valueLabelFormat: (value) => value.toLocaleString('en-US', {
35
+ style: 'currency',
36
+ currency: 'USD',
37
+ }),
38
+ },
39
+ },
40
+ {
41
+ accessorKey: 'age',
42
+ header: 'Age',
43
+ filterVariant: 'range',
44
+ filterFn: 'between',
45
+ size: 80,
46
+ },
47
+ {
48
+ accessorKey: 'city',
49
+ header: 'City',
50
+ filterVariant: 'select',
51
+ filterSelectOptions: citiesList, //custom options list (as opposed to faceted list)
52
+ },
53
+ {
54
+ accessorKey: 'state',
55
+ header: 'State',
56
+ filterVariant: 'multi-select',
57
+ filterSelectOptions: usStateList, //custom options list (as opposed to faceted list)
58
+ },
59
+ {
60
+ accessorFn: (originalRow) => new Date(originalRow.hireDate), //convert to date for sorting and filtering
61
+ id: 'hireDate',
62
+ header: 'Hire Date',
63
+ filterVariant: 'date-range',
64
+ Cell: ({ cell }) => cell.getValue().toLocaleDateString(), // convert back to string for display
65
+ },
66
+ {
67
+ accessorFn: (originalRow) => new Date(originalRow.arrivalTime), //convert to date for sorting and filtering
68
+ id: 'arrivalTime',
69
+ header: 'Arrival Time',
70
+ filterVariant: 'datetime-range',
71
+ Cell: ({ cell }) => `${cell.getValue().toLocaleDateString()} ${cell
72
+ .getValue()
73
+ .toLocaleTimeString()}`, // convert back to string for display
74
+ },
75
+ {
76
+ accessorFn: (originalRow) => new Date(originalRow.departureTime), //convert to date for sorting and filtering
77
+ id: 'departureTime',
78
+ header: 'Departure Time',
79
+ filterVariant: 'time-range',
80
+ Cell: ({ cell }) => cell.getValue().toLocaleTimeString(), // convert back to string for display
81
+ },
82
+ ], []);
83
+ const table = useMaterialReactTable({
84
+ columns,
85
+ data,
86
+ initialState: { showColumnFilters: true },
87
+ });
88
+ return <MaterialReactTable table={table}/>;
89
+ };
90
+ //Date Picker Imports - these should just be in your Context Provider
91
+ import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
92
+ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
93
+ const ExampleWithLocalizationProvider = () => (
94
+ //App.tsx or AppProviders file
95
+ <LocalizationProvider dateAdapter={AdapterDayjs}>
96
+ <Example />
97
+ </LocalizationProvider>);
98
+ export default ExampleWithLocalizationProvider;
@@ -0,0 +1,37 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable } from 'material-react-table';
3
+ import { data } from './makeData';
4
+ const Example = () => {
5
+ const columns = useMemo(
6
+ //column definitions...
7
+ () => [
8
+ {
9
+ accessorKey: 'id',
10
+ header: 'ID',
11
+ },
12
+ {
13
+ accessorKey: 'firstName',
14
+ header: 'First Name',
15
+ },
16
+ {
17
+ accessorKey: 'middleName',
18
+ header: 'Middle Name',
19
+ },
20
+ {
21
+ accessorKey: 'lastName',
22
+ header: 'Last Name',
23
+ },
24
+ {
25
+ accessorKey: 'age',
26
+ header: 'Age',
27
+ },
28
+ ], []);
29
+ return (<MaterialReactTable columns={columns} data={data} enableGlobalFilterModes initialState={{
30
+ showGlobalFilter: true,
31
+ }} positionGlobalFilter="left" muiSearchTextFieldProps={{
32
+ placeholder: `Search ${data.length} rows`,
33
+ sx: { minWidth: '300px' },
34
+ variant: 'outlined',
35
+ }}/>);
36
+ };
37
+ export default Example;
@@ -0,0 +1,75 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, MRT_ExpandAllButton, } from 'material-react-table';
3
+ import { data } from './makeData';
4
+ import { Box, Stack } from '@mui/material';
5
+ const Example = () => {
6
+ const columns = useMemo(
7
+ //column definitions...
8
+ () => [
9
+ {
10
+ header: 'First Name',
11
+ accessorKey: 'firstName',
12
+ },
13
+ {
14
+ header: 'Last Name',
15
+ accessorKey: 'lastName',
16
+ },
17
+ {
18
+ header: 'Age',
19
+ accessorKey: 'age',
20
+ },
21
+ {
22
+ header: 'Gender',
23
+ accessorKey: 'gender',
24
+ },
25
+ {
26
+ header: 'State',
27
+ accessorKey: 'state',
28
+ },
29
+ {
30
+ header: 'Salary',
31
+ accessorKey: 'salary',
32
+ },
33
+ ], []);
34
+ const table = useMaterialReactTable({
35
+ columns,
36
+ data,
37
+ displayColumnDefOptions: {
38
+ 'mrt-row-expand': {
39
+ Header: () => (<Stack direction="row" sx={{
40
+ alignItems: 'center',
41
+ }}>
42
+ <MRT_ExpandAllButton table={table}/>
43
+ <Box>Groups</Box>
44
+ </Stack>),
45
+ GroupedCell: ({ row, table }) => {
46
+ const { grouping } = table.getState();
47
+ return row.getValue(grouping[grouping.length - 1]);
48
+ },
49
+ enableResizing: true,
50
+ muiTableBodyCellProps: ({ row }) => ({
51
+ sx: (theme) => ({
52
+ color: row.depth === 0
53
+ ? theme.palette.primary.main
54
+ : row.depth === 1
55
+ ? theme.palette.secondary.main
56
+ : undefined,
57
+ }),
58
+ }),
59
+ size: 200,
60
+ },
61
+ },
62
+ enableGrouping: true,
63
+ enableColumnResizing: true,
64
+ groupedColumnMode: 'remove',
65
+ initialState: {
66
+ density: 'compact',
67
+ expanded: true, //expand all groups by default
68
+ grouping: ['state', 'gender'], //an array of columns to group by by default (can be multiple)
69
+ pagination: { pageIndex: 0, pageSize: 20 },
70
+ sorting: [{ id: 'state', desc: false }],
71
+ },
72
+ });
73
+ return <MaterialReactTable table={table}/>;
74
+ };
75
+ export default Example;
@@ -0,0 +1,43 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { data } from './makeData';
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: 'age',
18
+ header: 'Age',
19
+ },
20
+ {
21
+ accessorKey: 'address',
22
+ header: 'Address',
23
+ },
24
+ {
25
+ accessorKey: 'city',
26
+ header: 'City',
27
+ },
28
+ {
29
+ accessorKey: 'state',
30
+ header: 'State',
31
+ },
32
+ ], []);
33
+ const table = useMaterialReactTable({
34
+ columns,
35
+ data,
36
+ enableSelectAll: false,
37
+ enableRowSelection: (row) => row.original.age >= 21, //enable row selection conditionally per row
38
+ muiSelectCheckboxProps: { color: 'secondary' },
39
+ positionToolbarAlertBanner: 'head-overlay',
40
+ });
41
+ return <MaterialReactTable table={table}/>;
42
+ };
43
+ export default Example;
@@ -0,0 +1,81 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { data } from './makeData';
4
+ import { darken, lighten, useTheme } from '@mui/material';
5
+ const Example = () => {
6
+ const theme = useTheme();
7
+ //light or dark green
8
+ const baseBackgroundColor = theme.palette.mode === 'dark'
9
+ ? 'rgba(3, 44, 43, 1)'
10
+ : 'rgba(244, 255, 233, 1)';
11
+ const columns = useMemo(
12
+ //column definitions...
13
+ () => [
14
+ {
15
+ accessorKey: 'id',
16
+ header: 'ID',
17
+ size: 100,
18
+ enableColumnFilter: false,
19
+ },
20
+ {
21
+ accessorKey: 'firstName',
22
+ header: 'First Name',
23
+ },
24
+ {
25
+ accessorKey: 'middleName',
26
+ header: 'Middle Name',
27
+ },
28
+ {
29
+ accessorKey: 'lastName',
30
+ header: 'Last Name',
31
+ },
32
+ {
33
+ accessorKey: 'address',
34
+ header: 'Address',
35
+ size: 300,
36
+ },
37
+ {
38
+ accessorKey: 'city',
39
+ header: 'City',
40
+ },
41
+ {
42
+ accessorKey: 'state',
43
+ header: 'State',
44
+ },
45
+ ], []);
46
+ const table = useMaterialReactTable({
47
+ columns,
48
+ data,
49
+ enableColumnResizing: true,
50
+ enableRowPinning: true,
51
+ enableRowSelection: true,
52
+ muiTablePaperProps: {
53
+ elevation: 0,
54
+ sx: {
55
+ borderRadius: '0',
56
+ },
57
+ },
58
+ muiTableBodyProps: {
59
+ sx: (theme) => ({
60
+ '& tr:nth-of-type(odd):not([data-selected="true"]):not([data-pinned="true"]) > td': {
61
+ backgroundColor: darken(baseBackgroundColor, 0.1),
62
+ },
63
+ '& tr:nth-of-type(odd):not([data-selected="true"]):not([data-pinned="true"]):hover > td': {
64
+ backgroundColor: darken(baseBackgroundColor, 0.2),
65
+ },
66
+ '& tr:nth-of-type(even):not([data-selected="true"]):not([data-pinned="true"]) > td': {
67
+ backgroundColor: lighten(baseBackgroundColor, 0.1),
68
+ },
69
+ '& tr:nth-of-type(even):not([data-selected="true"]):not([data-pinned="true"]):hover > td': {
70
+ backgroundColor: darken(baseBackgroundColor, 0.2),
71
+ },
72
+ }),
73
+ },
74
+ mrtTheme: (theme) => ({
75
+ baseBackgroundColor: baseBackgroundColor,
76
+ draggingBorderColor: theme.palette.secondary.main,
77
+ }),
78
+ });
79
+ return <MaterialReactTable table={table}/>;
80
+ };
81
+ export default Example;
@@ -0,0 +1,26 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { data } from './makeData';
4
+ const Example = () => {
5
+ const columns = useMemo(() => [
6
+ {
7
+ accessorKey: 'id',
8
+ enableColumnActions: false,
9
+ header: 'ID',
10
+ },
11
+ {
12
+ accessorKey: 'firstName',
13
+ header: 'First Name',
14
+ },
15
+ {
16
+ accessorKey: 'lastName',
17
+ header: 'Last Name',
18
+ },
19
+ ], []);
20
+ const table = useMaterialReactTable({
21
+ columns,
22
+ data,
23
+ });
24
+ return <MaterialReactTable table={table}/>;
25
+ };
26
+ export default Example;
@@ -0,0 +1,74 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ const Example = () => {
4
+ const columns = useMemo(() => [
5
+ {
6
+ accessorKey: 'firstName',
7
+ enableHiding: false,
8
+ header: 'First Name',
9
+ },
10
+ {
11
+ accessorKey: 'lastName',
12
+ enableHiding: false,
13
+ header: 'Last Name',
14
+ },
15
+ {
16
+ accessorKey: 'address',
17
+ header: 'Address',
18
+ },
19
+ {
20
+ accessorKey: 'city',
21
+ header: 'City',
22
+ },
23
+ {
24
+ accessorKey: 'state',
25
+ header: 'State',
26
+ },
27
+ ], []);
28
+ const data = useMemo(() => [
29
+ //data definitions...
30
+ {
31
+ firstName: 'Dylan',
32
+ lastName: 'Murray',
33
+ address: '261 Erdman Ford',
34
+ city: 'East Daphne',
35
+ state: 'Kentucky',
36
+ },
37
+ {
38
+ firstName: 'Raquel',
39
+ lastName: 'Kohler',
40
+ address: '769 Dominic Grove',
41
+ city: 'Columbus',
42
+ state: 'Ohio',
43
+ },
44
+ {
45
+ firstName: 'Ervin',
46
+ lastName: 'Reinger',
47
+ address: '566 Brakus Inlet',
48
+ city: 'South Linda',
49
+ state: 'West Virginia',
50
+ },
51
+ {
52
+ firstName: 'Brittany',
53
+ lastName: 'McCullough',
54
+ address: '722 Emie Stream',
55
+ city: 'Lincoln',
56
+ state: 'Nebraska',
57
+ },
58
+ {
59
+ firstName: 'Branson',
60
+ lastName: 'Frami',
61
+ address: '32188 Larkin Turnpike',
62
+ city: 'Charleston',
63
+ state: 'South Carolina',
64
+ },
65
+ //end
66
+ ], []);
67
+ const table = useMaterialReactTable({
68
+ columns,
69
+ data,
70
+ initialState: { columnVisibility: { address: false } },
71
+ });
72
+ return <MaterialReactTable table={table}/>;
73
+ };
74
+ export default Example;
@@ -0,0 +1,37 @@
1
+ import { useMemo } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { data } from './makeData';
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: 'address',
18
+ header: 'Address',
19
+ },
20
+ {
21
+ accessorKey: 'city',
22
+ header: 'City',
23
+ },
24
+ {
25
+ accessorKey: 'state',
26
+ header: 'State',
27
+ },
28
+ ], []);
29
+ const table = useMaterialReactTable({
30
+ columns,
31
+ data,
32
+ enableDensityToggle: false,
33
+ initialState: { density: 'compact' },
34
+ });
35
+ return <MaterialReactTable table={table}/>;
36
+ };
37
+ export default Example;
@@ -0,0 +1,124 @@
1
+ import { lazy, Suspense, useMemo, useState } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable,
3
+ // type MRT_ColumnOrderState,
4
+ } from 'material-react-table';
5
+ import { IconButton, Tooltip } from '@mui/material';
6
+ import RefreshIcon from '@mui/icons-material/Refresh';
7
+ import { keepPreviousData, QueryClient, QueryClientProvider, useQuery, } from '@tanstack/react-query'; //note: this is TanStack React Query V5
8
+ const columnNames = {
9
+ firstName: 'First Name',
10
+ lastName: 'Last Name',
11
+ address: 'Address',
12
+ state: 'State',
13
+ phoneNumber: 'Phone Number',
14
+ lastLogin: 'Last Login',
15
+ };
16
+ const Example = () => {
17
+ //manage our own state for stuff we want to pass to the API
18
+ const [columnFilters, setColumnFilters] = useState([]);
19
+ const [globalFilter, setGlobalFilter] = useState('');
20
+ const [sorting, setSorting] = useState([]);
21
+ const [pagination, setPagination] = useState({
22
+ pageIndex: 0,
23
+ pageSize: 10,
24
+ });
25
+ //if using dynamic columns that are loaded after table instance creation, we will need to manage the column order state ourselves
26
+ //UPDATE: No longer needed as of v2.10.0
27
+ // const [columnOrder, setColumnOrder] = useState<MRT_ColumnOrderState>([]);
28
+ //consider storing this code in a custom hook (i.e useFetchUsers)
29
+ const { data: { data = [], meta } = {}, //your data and api response will probably be different
30
+ isError, isRefetching, isLoading, refetch, } = useQuery({
31
+ queryKey: [
32
+ 'users-list',
33
+ {
34
+ columnFilters, //refetch when columnFilters changes
35
+ globalFilter, //refetch when globalFilter changes
36
+ pagination, //refetch when pagination changes
37
+ sorting, //refetch when sorting changes
38
+ },
39
+ ],
40
+ queryFn: async () => {
41
+ const fetchURL = new URL('/api/data', location.origin); // nextjs api route
42
+ //read our state and pass it to the API as query params
43
+ fetchURL.searchParams.set('start', `${pagination.pageIndex * pagination.pageSize}`);
44
+ fetchURL.searchParams.set('size', `${pagination.pageSize}`);
45
+ fetchURL.searchParams.set('filters', JSON.stringify(columnFilters ?? []));
46
+ fetchURL.searchParams.set('globalFilter', globalFilter ?? '');
47
+ fetchURL.searchParams.set('sorting', JSON.stringify(sorting ?? []));
48
+ //use whatever fetch library you want, fetch, axios, etc
49
+ const response = await fetch(fetchURL.href);
50
+ const json = (await response.json());
51
+ return json;
52
+ },
53
+ placeholderData: keepPreviousData, //don't go to 0 rows when refetching or paginating to next page
54
+ });
55
+ //create columns from data
56
+ const columns = useMemo(() => data.length
57
+ ? Object.keys(data[0]).map((columnId) => ({
58
+ header: columnNames[columnId] ?? columnId,
59
+ accessorKey: columnId,
60
+ id: columnId,
61
+ }))
62
+ : [], [data]);
63
+ //UPDATE: No longer needed as of v2.10.0
64
+ // useEffect(() => {
65
+ // //if using dynamic columns that are loaded after table instance creation, we will need to set the column order state ourselves
66
+ // setColumnOrder(columns.map((column) => column.id!));
67
+ // }, [columns]);
68
+ const table = useMaterialReactTable({
69
+ columns,
70
+ data,
71
+ enableRowSelection: true,
72
+ initialState: { showColumnFilters: true },
73
+ manualFiltering: true, //turn off built-in client-side filtering
74
+ manualPagination: true, //turn off built-in client-side pagination
75
+ manualSorting: true, //turn off built-in client-side sorting
76
+ //give loading spinner somewhere to go while loading
77
+ muiTableBodyProps: {
78
+ children: isLoading ? (<tr style={{ height: '200px' }}>
79
+ <td />
80
+ </tr>) : undefined,
81
+ },
82
+ muiToolbarAlertBannerProps: isError
83
+ ? {
84
+ color: 'error',
85
+ children: 'Error loading data',
86
+ }
87
+ : undefined,
88
+ onColumnFiltersChange: setColumnFilters,
89
+ // onColumnOrderChange: setColumnOrder,
90
+ onGlobalFilterChange: setGlobalFilter,
91
+ onPaginationChange: setPagination,
92
+ onSortingChange: setSorting,
93
+ renderTopToolbarCustomActions: () => (<Tooltip arrow title="Refresh Data">
94
+ <IconButton onClick={() => refetch()}>
95
+ <RefreshIcon />
96
+ </IconButton>
97
+ </Tooltip>),
98
+ rowCount: meta?.totalRowCount ?? 0,
99
+ state: {
100
+ columnFilters,
101
+ // columnOrder,
102
+ globalFilter,
103
+ isLoading,
104
+ pagination,
105
+ showAlertBanner: isError,
106
+ showProgressBars: isRefetching,
107
+ sorting,
108
+ },
109
+ });
110
+ return <MaterialReactTable table={table}/>;
111
+ };
112
+ //react query setup in App.tsx
113
+ const ReactQueryDevtoolsProduction = lazy(() => import('@tanstack/react-query-devtools/build/modern/production.js').then((d) => ({
114
+ default: d.ReactQueryDevtools,
115
+ })));
116
+ const queryClient = new QueryClient();
117
+ export default function App() {
118
+ return (<QueryClientProvider client={queryClient}>
119
+ <Example />
120
+ <Suspense fallback={null}>
121
+ <ReactQueryDevtoolsProduction />
122
+ </Suspense>
123
+ </QueryClientProvider>);
124
+ }