@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.
- package/README.md +68 -0
- package/data/MIGRATION.md +136 -0
- package/data/api.json +5379 -0
- package/data/examples/advanced.js +215 -0
- package/data/examples/advanced.tsx +309 -0
- package/data/examples/aggregation-and-grouping.js +116 -0
- package/data/examples/aggregation-and-grouping.tsx +153 -0
- package/data/examples/aggregation-multi.js +89 -0
- package/data/examples/aggregation-multi.tsx +105 -0
- package/data/examples/alternate-column-filtering.js +38 -0
- package/data/examples/alternate-column-filtering.tsx +49 -0
- package/data/examples/alternate-detail-panel.js +65 -0
- package/data/examples/alternate-detail-panel.tsx +81 -0
- package/data/examples/alternate-pagination.js +17 -0
- package/data/examples/alternate-pagination.tsx +23 -0
- package/data/examples/basic.js +86 -0
- package/data/examples/basic.tsx +109 -0
- package/data/examples/chart-detail-panel.js +69 -0
- package/data/examples/chart-detail-panel.tsx +89 -0
- package/data/examples/column-actions-space.js +34 -0
- package/data/examples/column-actions-space.tsx +44 -0
- package/data/examples/column-alignment.js +58 -0
- package/data/examples/column-alignment.tsx +70 -0
- package/data/examples/custom-column-actions.js +74 -0
- package/data/examples/custom-column-actions.tsx +109 -0
- package/data/examples/custom-column-filtering-ui.js +59 -0
- package/data/examples/custom-column-filtering-ui.tsx +89 -0
- package/data/examples/custom-headless.js +85 -0
- package/data/examples/custom-headless.tsx +125 -0
- package/data/examples/custom-top-toolbar.js +60 -0
- package/data/examples/custom-top-toolbar.tsx +88 -0
- package/data/examples/customize-display-columns.js +72 -0
- package/data/examples/customize-display-columns.tsx +85 -0
- package/data/examples/customize-filter-components.js +78 -0
- package/data/examples/customize-filter-components.tsx +101 -0
- package/data/examples/customize-filter-modes.js +59 -0
- package/data/examples/customize-filter-modes.tsx +73 -0
- package/data/examples/customize-filter-variants.js +98 -0
- package/data/examples/customize-filter-variants.tsx +116 -0
- package/data/examples/customize-global-filter-component.js +37 -0
- package/data/examples/customize-global-filter-component.tsx +52 -0
- package/data/examples/customize-remove-column-grouping.js +75 -0
- package/data/examples/customize-remove-column-grouping.tsx +94 -0
- package/data/examples/customize-row-selection.js +43 -0
- package/data/examples/customize-row-selection.tsx +54 -0
- package/data/examples/customize-table-styles.js +81 -0
- package/data/examples/customize-table-styles.tsx +100 -0
- package/data/examples/disable-column-actions.js +26 -0
- package/data/examples/disable-column-actions.tsx +37 -0
- package/data/examples/disable-column-hiding.js +74 -0
- package/data/examples/disable-column-hiding.tsx +90 -0
- package/data/examples/disable-density-toggle.js +37 -0
- package/data/examples/disable-density-toggle.tsx +49 -0
- package/data/examples/dynamic-columns.js +124 -0
- package/data/examples/dynamic-columns.tsx +189 -0
- package/data/examples/editing-crud-cell.js +288 -0
- package/data/examples/editing-crud-cell.tsx +377 -0
- package/data/examples/editing-crud-modal.js +275 -0
- package/data/examples/editing-crud-modal.tsx +370 -0
- package/data/examples/editing-crud-row.js +255 -0
- package/data/examples/editing-crud-row.tsx +333 -0
- package/data/examples/editing-crud-table.js +280 -0
- package/data/examples/editing-crud-table.tsx +369 -0
- package/data/examples/editing-crud-tree.js +328 -0
- package/data/examples/editing-crud-tree.tsx +416 -0
- package/data/examples/enable-cell-actions.js +52 -0
- package/data/examples/enable-cell-actions.tsx +76 -0
- package/data/examples/enable-click-to-copy.js +33 -0
- package/data/examples/enable-click-to-copy.tsx +39 -0
- package/data/examples/enable-column-grouping.js +69 -0
- package/data/examples/enable-column-grouping.tsx +120 -0
- package/data/examples/enable-column-ordering.js +37 -0
- package/data/examples/enable-column-ordering.tsx +48 -0
- package/data/examples/enable-column-pinning.js +58 -0
- package/data/examples/enable-column-pinning.tsx +69 -0
- package/data/examples/enable-column-resizing.js +45 -0
- package/data/examples/enable-column-resizing.tsx +56 -0
- package/data/examples/enable-column-virtualization.js +19 -0
- package/data/examples/enable-column-virtualization.tsx +27 -0
- package/data/examples/enable-detail-panel-conditionally.js +61 -0
- package/data/examples/enable-detail-panel-conditionally.tsx +78 -0
- package/data/examples/enable-detail-panel-virtualized.js +64 -0
- package/data/examples/enable-detail-panel-virtualized.tsx +80 -0
- package/data/examples/enable-expanding-tree.js +87 -0
- package/data/examples/enable-expanding-tree.tsx +110 -0
- package/data/examples/enable-filter-facet-values.js +52 -0
- package/data/examples/enable-filter-facet-values.tsx +65 -0
- package/data/examples/enable-row-dragging.js +102 -0
- package/data/examples/enable-row-dragging.tsx +137 -0
- package/data/examples/enable-row-numbers-original.js +37 -0
- package/data/examples/enable-row-numbers-original.tsx +48 -0
- package/data/examples/enable-row-numbers-static.js +37 -0
- package/data/examples/enable-row-numbers-static.tsx +48 -0
- package/data/examples/enable-row-ordering.js +40 -0
- package/data/examples/enable-row-ordering.tsx +57 -0
- package/data/examples/enable-row-pinning-select.js +64 -0
- package/data/examples/enable-row-pinning-select.tsx +76 -0
- package/data/examples/enable-row-pinning-static.js +39 -0
- package/data/examples/enable-row-pinning-static.tsx +50 -0
- package/data/examples/enable-row-pinning-sticky.js +57 -0
- package/data/examples/enable-row-pinning-sticky.tsx +69 -0
- package/data/examples/enable-row-selection.js +71 -0
- package/data/examples/enable-row-selection.tsx +96 -0
- package/data/examples/enable-row-virtualization.js +85 -0
- package/data/examples/enable-row-virtualization.tsx +101 -0
- package/data/examples/enable-sticky-header.js +47 -0
- package/data/examples/enable-sticky-header.tsx +59 -0
- package/data/examples/expanding-tree-expanded.js +98 -0
- package/data/examples/expanding-tree-expanded.tsx +121 -0
- package/data/examples/expanding-tree-flat-parse.js +102 -0
- package/data/examples/expanding-tree-flat-parse.tsx +125 -0
- package/data/examples/expanding-tree-root-expanded.js +120 -0
- package/data/examples/expanding-tree-root-expanded.tsx +151 -0
- package/data/examples/export-to-csv.js +84 -0
- package/data/examples/export-to-csv.tsx +114 -0
- package/data/examples/export-to-pdf.js +76 -0
- package/data/examples/export-to-pdf.tsx +104 -0
- package/data/examples/external-toolbar.js +82 -0
- package/data/examples/external-toolbar.tsx +108 -0
- package/data/examples/font-awesome-icons.js +62 -0
- package/data/examples/font-awesome-icons.tsx +106 -0
- package/data/examples/infinite-scrolling.js +137 -0
- package/data/examples/infinite-scrolling.tsx +208 -0
- package/data/examples/lazy-detail-panel.js +164 -0
- package/data/examples/lazy-detail-panel.tsx +262 -0
- package/data/examples/lazy-sub-rows.js +118 -0
- package/data/examples/lazy-sub-rows.tsx +190 -0
- package/data/examples/linear-progress.js +48 -0
- package/data/examples/linear-progress.tsx +66 -0
- package/data/examples/loading.js +34 -0
- package/data/examples/loading.tsx +50 -0
- package/data/examples/localization-i18n-ar.js +40 -0
- package/data/examples/localization-i18n-ar.tsx +65 -0
- package/data/examples/localization-i18n-az.js +38 -0
- package/data/examples/localization-i18n-az.tsx +60 -0
- package/data/examples/localization-i18n-bg.js +38 -0
- package/data/examples/localization-i18n-bg.tsx +60 -0
- package/data/examples/localization-i18n-cs.js +38 -0
- package/data/examples/localization-i18n-cs.tsx +60 -0
- package/data/examples/localization-i18n-da.js +38 -0
- package/data/examples/localization-i18n-da.tsx +60 -0
- package/data/examples/localization-i18n-de.js +38 -0
- package/data/examples/localization-i18n-de.tsx +60 -0
- package/data/examples/localization-i18n-el.js +38 -0
- package/data/examples/localization-i18n-el.tsx +60 -0
- package/data/examples/localization-i18n-en.js +38 -0
- package/data/examples/localization-i18n-en.tsx +60 -0
- package/data/examples/localization-i18n-es.js +38 -0
- package/data/examples/localization-i18n-es.tsx +60 -0
- package/data/examples/localization-i18n-et.js +38 -0
- package/data/examples/localization-i18n-et.tsx +60 -0
- package/data/examples/localization-i18n-fa.js +40 -0
- package/data/examples/localization-i18n-fa.tsx +66 -0
- package/data/examples/localization-i18n-fi.js +38 -0
- package/data/examples/localization-i18n-fi.tsx +60 -0
- package/data/examples/localization-i18n-fr.js +38 -0
- package/data/examples/localization-i18n-fr.tsx +60 -0
- package/data/examples/localization-i18n-he.js +40 -0
- package/data/examples/localization-i18n-he.tsx +66 -0
- package/data/examples/localization-i18n-hr.js +38 -0
- package/data/examples/localization-i18n-hr.tsx +60 -0
- package/data/examples/localization-i18n-hu.js +38 -0
- package/data/examples/localization-i18n-hu.tsx +60 -0
- package/data/examples/localization-i18n-hy.js +40 -0
- package/data/examples/localization-i18n-hy.tsx +65 -0
- package/data/examples/localization-i18n-id.js +38 -0
- package/data/examples/localization-i18n-id.tsx +60 -0
- package/data/examples/localization-i18n-it.js +38 -0
- package/data/examples/localization-i18n-it.tsx +60 -0
- package/data/examples/localization-i18n-ja.js +38 -0
- package/data/examples/localization-i18n-ja.tsx +60 -0
- package/data/examples/localization-i18n-ko.js +38 -0
- package/data/examples/localization-i18n-ko.tsx +60 -0
- package/data/examples/localization-i18n-nl.js +38 -0
- package/data/examples/localization-i18n-nl.tsx +60 -0
- package/data/examples/localization-i18n-no.js +38 -0
- package/data/examples/localization-i18n-no.tsx +60 -0
- package/data/examples/localization-i18n-np.js +38 -0
- package/data/examples/localization-i18n-np.tsx +60 -0
- package/data/examples/localization-i18n-pl.js +38 -0
- package/data/examples/localization-i18n-pl.tsx +60 -0
- package/data/examples/localization-i18n-pt-BR.js +38 -0
- package/data/examples/localization-i18n-pt-BR.tsx +60 -0
- package/data/examples/localization-i18n-pt.js +38 -0
- package/data/examples/localization-i18n-pt.tsx +60 -0
- package/data/examples/localization-i18n-ro.js +38 -0
- package/data/examples/localization-i18n-ro.tsx +60 -0
- package/data/examples/localization-i18n-ru.js +38 -0
- package/data/examples/localization-i18n-ru.tsx +60 -0
- package/data/examples/localization-i18n-sk.js +38 -0
- package/data/examples/localization-i18n-sk.tsx +60 -0
- package/data/examples/localization-i18n-sr-Cyrl-RS.js +38 -0
- package/data/examples/localization-i18n-sr-Cyrl-RS.tsx +60 -0
- package/data/examples/localization-i18n-sr-Latn-RS.js +38 -0
- package/data/examples/localization-i18n-sr-Latn-RS.tsx +60 -0
- package/data/examples/localization-i18n-sv.js +38 -0
- package/data/examples/localization-i18n-sv.tsx +60 -0
- package/data/examples/localization-i18n-tr.js +38 -0
- package/data/examples/localization-i18n-tr.tsx +60 -0
- package/data/examples/localization-i18n-uk.js +38 -0
- package/data/examples/localization-i18n-uk.tsx +60 -0
- package/data/examples/localization-i18n-vi.js +38 -0
- package/data/examples/localization-i18n-vi.tsx +60 -0
- package/data/examples/localization-i18n-zh-hans.js +38 -0
- package/data/examples/localization-i18n-zh-hans.tsx +60 -0
- package/data/examples/localization-i18n-zh-hant.js +38 -0
- package/data/examples/localization-i18n-zh-hant.tsx +60 -0
- package/data/examples/manual-selection.js +81 -0
- package/data/examples/manual-selection.tsx +105 -0
- package/data/examples/minimal.js +66 -0
- package/data/examples/minimal.tsx +78 -0
- package/data/examples/mui-theme.js +109 -0
- package/data/examples/mui-theme.tsx +136 -0
- package/data/examples/multi-sorting.js +48 -0
- package/data/examples/multi-sorting.tsx +58 -0
- package/data/examples/persistent-state.js +126 -0
- package/data/examples/persistent-state.tsx +184 -0
- package/data/examples/react-query.js +122 -0
- package/data/examples/react-query.tsx +182 -0
- package/data/examples/remote.js +113 -0
- package/data/examples/remote.tsx +150 -0
- package/data/examples/row-actions-buttons.js +54 -0
- package/data/examples/row-actions-buttons.tsx +89 -0
- package/data/examples/row-actions-menu-items.js +36 -0
- package/data/examples/row-actions-menu-items.tsx +64 -0
- package/data/examples/single-row-selection.js +74 -0
- package/data/examples/single-row-selection.tsx +98 -0
- package/data/examples/virtualized.js +122 -0
- package/data/examples/virtualized.tsx +138 -0
- package/data/guides/accessibility.md +119 -0
- package/data/guides/aggregation.md +210 -0
- package/data/guides/async-loading.md +132 -0
- package/data/guides/best-practices.md +363 -0
- package/data/guides/cell-actions.md +139 -0
- package/data/guides/click-to-copy.md +67 -0
- package/data/guides/column-actions.md +69 -0
- package/data/guides/column-filtering.md +366 -0
- package/data/guides/column-grouping.md +192 -0
- package/data/guides/column-hiding.md +133 -0
- package/data/guides/column-ordering-dnd.md +99 -0
- package/data/guides/column-pinning.md +72 -0
- package/data/guides/column-resizing.md +157 -0
- package/data/guides/column-size.md +162 -0
- package/data/guides/customize-components.md +395 -0
- package/data/guides/customize-icons.md +35 -0
- package/data/guides/data-columns.md +292 -0
- package/data/guides/density-toggle.md +46 -0
- package/data/guides/detail-panel.md +195 -0
- package/data/guides/display-columns.md +135 -0
- package/data/guides/editing.md +367 -0
- package/data/guides/expanding-sub-rows.md +194 -0
- package/data/guides/full-screen-toggle.md +63 -0
- package/data/guides/global-filtering.md +237 -0
- package/data/guides/localization.md +57 -0
- package/data/guides/memoization.md +157 -0
- package/data/guides/pagination.md +171 -0
- package/data/guides/row-actions.md +116 -0
- package/data/guides/row-numbers.md +24 -0
- package/data/guides/row-ordering-dnd.md +51 -0
- package/data/guides/row-pinning.md +96 -0
- package/data/guides/row-selection.md +237 -0
- package/data/guides/sorting.md +190 -0
- package/data/guides/state-management.md +165 -0
- package/data/guides/sticky-header.md +41 -0
- package/data/guides/table-event-listeners.md +103 -0
- package/data/guides/toolbar-customization.md +251 -0
- package/data/guides/virtualization.md +183 -0
- package/data/index.json +1930 -0
- package/data/reference/mrt-components.md +183 -0
- package/data/reference/mrt-hooks.md +125 -0
- package/data/skills/README.md +81 -0
- package/data/skills/composable-components/SKILL.md +189 -0
- package/data/skills/customization/SKILL.md +190 -0
- package/data/skills/drag-and-drop-ordering/SKILL.md +227 -0
- package/data/skills/editing/SKILL.md +243 -0
- package/data/skills/filtering/SKILL.md +213 -0
- package/data/skills/getting-started/SKILL.md +213 -0
- package/data/skills/localization/SKILL.md +182 -0
- package/data/skills/migrate-v3-to-v4/SKILL.md +161 -0
- package/data/skills/state-and-server-data/SKILL.md +200 -0
- package/data/skills/virtualization/SKILL.md +203 -0
- package/dist/data.js +159 -0
- package/dist/index.js +244 -0
- package/package.json +50 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Button } from '@mui/material';
|
|
3
|
+
import {
|
|
4
|
+
MaterialReactTable,
|
|
5
|
+
type MRT_ColumnDef,
|
|
6
|
+
type MRT_ColumnFiltersState,
|
|
7
|
+
type MRT_DensityState,
|
|
8
|
+
type MRT_SortingState,
|
|
9
|
+
type MRT_VisibilityState,
|
|
10
|
+
} from 'material-react-table';
|
|
11
|
+
import { data, type Person } from './makeData';
|
|
12
|
+
|
|
13
|
+
//column definitions...
|
|
14
|
+
const columns: MRT_ColumnDef<Person>[] = [
|
|
15
|
+
{
|
|
16
|
+
accessorKey: 'firstName',
|
|
17
|
+
header: 'First Name',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
accessorKey: 'lastName',
|
|
21
|
+
header: 'Last Name',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
accessorKey: 'city',
|
|
25
|
+
header: 'City',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
accessorKey: 'state',
|
|
29
|
+
header: 'State',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
accessorKey: 'salary',
|
|
33
|
+
header: 'Salary',
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
//end
|
|
37
|
+
|
|
38
|
+
const Example = () => {
|
|
39
|
+
const isFirstRender = useRef(true);
|
|
40
|
+
|
|
41
|
+
const [columnFilters, setColumnFilters] = useState<MRT_ColumnFiltersState>(
|
|
42
|
+
[],
|
|
43
|
+
);
|
|
44
|
+
const [columnVisibility, setColumnVisibility] = useState<MRT_VisibilityState>(
|
|
45
|
+
{},
|
|
46
|
+
);
|
|
47
|
+
const [density, setDensity] = useState<MRT_DensityState>('comfortable');
|
|
48
|
+
const [globalFilter, setGlobalFilter] = useState<string | undefined>(
|
|
49
|
+
undefined,
|
|
50
|
+
);
|
|
51
|
+
const [showGlobalFilter, setShowGlobalFilter] = useState(false);
|
|
52
|
+
const [showColumnFilters, setShowColumnFilters] = useState(false);
|
|
53
|
+
const [sorting, setSorting] = useState<MRT_SortingState>([]);
|
|
54
|
+
|
|
55
|
+
//load state from local storage
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
const columnFilters = sessionStorage.getItem('mrt_columnFilters_table_1');
|
|
58
|
+
const columnVisibility = sessionStorage.getItem(
|
|
59
|
+
'mrt_columnVisibility_table_1',
|
|
60
|
+
);
|
|
61
|
+
const density = sessionStorage.getItem('mrt_density_table_1');
|
|
62
|
+
const globalFilter = sessionStorage.getItem('mrt_globalFilter_table_1');
|
|
63
|
+
const showGlobalFilter = sessionStorage.getItem(
|
|
64
|
+
'mrt_showGlobalFilter_table_1',
|
|
65
|
+
);
|
|
66
|
+
const showColumnFilters = sessionStorage.getItem(
|
|
67
|
+
'mrt_showColumnFilters_table_1',
|
|
68
|
+
);
|
|
69
|
+
const sorting = sessionStorage.getItem('mrt_sorting_table_1');
|
|
70
|
+
|
|
71
|
+
if (columnFilters) {
|
|
72
|
+
setColumnFilters(JSON.parse(columnFilters));
|
|
73
|
+
}
|
|
74
|
+
if (columnVisibility) {
|
|
75
|
+
setColumnVisibility(JSON.parse(columnVisibility));
|
|
76
|
+
}
|
|
77
|
+
if (density) {
|
|
78
|
+
setDensity(JSON.parse(density));
|
|
79
|
+
}
|
|
80
|
+
if (globalFilter) {
|
|
81
|
+
setGlobalFilter(JSON.parse(globalFilter) || undefined);
|
|
82
|
+
}
|
|
83
|
+
if (showGlobalFilter) {
|
|
84
|
+
setShowGlobalFilter(JSON.parse(showGlobalFilter));
|
|
85
|
+
}
|
|
86
|
+
if (showColumnFilters) {
|
|
87
|
+
setShowColumnFilters(JSON.parse(showColumnFilters));
|
|
88
|
+
}
|
|
89
|
+
if (sorting) {
|
|
90
|
+
setSorting(JSON.parse(sorting));
|
|
91
|
+
}
|
|
92
|
+
isFirstRender.current = false;
|
|
93
|
+
}, []);
|
|
94
|
+
|
|
95
|
+
//save states to local storage
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
if (isFirstRender.current) return;
|
|
98
|
+
sessionStorage.setItem(
|
|
99
|
+
'mrt_columnFilters_table_1',
|
|
100
|
+
JSON.stringify(columnFilters),
|
|
101
|
+
);
|
|
102
|
+
}, [columnFilters]);
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (isFirstRender.current) return;
|
|
106
|
+
sessionStorage.setItem(
|
|
107
|
+
'mrt_columnVisibility_table_1',
|
|
108
|
+
JSON.stringify(columnVisibility),
|
|
109
|
+
);
|
|
110
|
+
}, [columnVisibility]);
|
|
111
|
+
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (isFirstRender.current) return;
|
|
114
|
+
sessionStorage.setItem('mrt_density_table_1', JSON.stringify(density));
|
|
115
|
+
}, [density]);
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (isFirstRender.current) return;
|
|
119
|
+
sessionStorage.setItem(
|
|
120
|
+
'mrt_globalFilter_table_1',
|
|
121
|
+
JSON.stringify(globalFilter ?? ''),
|
|
122
|
+
);
|
|
123
|
+
}, [globalFilter]);
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (isFirstRender.current) return;
|
|
127
|
+
sessionStorage.setItem(
|
|
128
|
+
'mrt_showGlobalFilter_table_1',
|
|
129
|
+
JSON.stringify(showGlobalFilter),
|
|
130
|
+
);
|
|
131
|
+
}, [showGlobalFilter]);
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (isFirstRender.current) return;
|
|
135
|
+
sessionStorage.setItem(
|
|
136
|
+
'mrt_showColumnFilters_table_1',
|
|
137
|
+
JSON.stringify(showColumnFilters),
|
|
138
|
+
);
|
|
139
|
+
}, [showColumnFilters]);
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
if (isFirstRender.current) return;
|
|
143
|
+
sessionStorage.setItem('mrt_sorting_table_1', JSON.stringify(sorting));
|
|
144
|
+
}, [sorting]);
|
|
145
|
+
|
|
146
|
+
const resetState = () => {
|
|
147
|
+
sessionStorage.removeItem('mrt_columnFilters_table_1');
|
|
148
|
+
sessionStorage.removeItem('mrt_columnVisibility_table_1');
|
|
149
|
+
sessionStorage.removeItem('mrt_density_table_1');
|
|
150
|
+
sessionStorage.removeItem('mrt_globalFilter_table_1');
|
|
151
|
+
sessionStorage.removeItem('mrt_showGlobalFilter_table_1');
|
|
152
|
+
sessionStorage.removeItem('mrt_showColumnFilters_table_1');
|
|
153
|
+
sessionStorage.removeItem('mrt_sorting_table_1');
|
|
154
|
+
window.location.reload();
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<MaterialReactTable
|
|
159
|
+
columns={columns}
|
|
160
|
+
data={data}
|
|
161
|
+
onColumnFiltersChange={setColumnFilters}
|
|
162
|
+
onColumnVisibilityChange={setColumnVisibility}
|
|
163
|
+
onDensityChange={setDensity}
|
|
164
|
+
onGlobalFilterChange={setGlobalFilter}
|
|
165
|
+
onShowColumnFiltersChange={setShowColumnFilters}
|
|
166
|
+
onShowGlobalFilterChange={setShowGlobalFilter}
|
|
167
|
+
onSortingChange={setSorting}
|
|
168
|
+
state={{
|
|
169
|
+
columnFilters,
|
|
170
|
+
columnVisibility,
|
|
171
|
+
density,
|
|
172
|
+
globalFilter,
|
|
173
|
+
showColumnFilters,
|
|
174
|
+
showGlobalFilter,
|
|
175
|
+
sorting,
|
|
176
|
+
}}
|
|
177
|
+
renderTopToolbarCustomActions={() => (
|
|
178
|
+
<Button onClick={resetState}>Reset State</Button>
|
|
179
|
+
)}
|
|
180
|
+
/>
|
|
181
|
+
);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export default Example;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { lazy, Suspense, useMemo, useState } from 'react';
|
|
2
|
+
import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
|
|
3
|
+
import { IconButton, Tooltip } from '@mui/material';
|
|
4
|
+
import RefreshIcon from '@mui/icons-material/Refresh';
|
|
5
|
+
import { QueryClient, QueryClientProvider, keepPreviousData, useQuery, } from '@tanstack/react-query'; //note: this is TanStack React Query V5
|
|
6
|
+
const Example = () => {
|
|
7
|
+
//manage our own state for stuff we want to pass to the API
|
|
8
|
+
const [columnFilters, setColumnFilters] = useState([]);
|
|
9
|
+
const [globalFilter, setGlobalFilter] = useState('');
|
|
10
|
+
const [sorting, setSorting] = useState([]);
|
|
11
|
+
const [pagination, setPagination] = useState({
|
|
12
|
+
pageIndex: 0,
|
|
13
|
+
pageSize: 10,
|
|
14
|
+
});
|
|
15
|
+
//consider storing this code in a custom hook (i.e useFetchUsers)
|
|
16
|
+
const { data: { data = [], meta } = {}, //your data and api response will probably be different
|
|
17
|
+
isError, isRefetching, isLoading, refetch, } = useQuery({
|
|
18
|
+
queryKey: [
|
|
19
|
+
'users-list',
|
|
20
|
+
{
|
|
21
|
+
columnFilters, //refetch when columnFilters changes
|
|
22
|
+
globalFilter, //refetch when globalFilter changes
|
|
23
|
+
pagination, //refetch when pagination changes
|
|
24
|
+
sorting, //refetch when sorting changes
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
queryFn: async () => {
|
|
28
|
+
const fetchURL = new URL('/api/data', location.origin);
|
|
29
|
+
//read our state and pass it to the API as query params
|
|
30
|
+
fetchURL.searchParams.set('start', `${pagination.pageIndex * pagination.pageSize}`);
|
|
31
|
+
fetchURL.searchParams.set('size', `${pagination.pageSize}`);
|
|
32
|
+
fetchURL.searchParams.set('filters', JSON.stringify(columnFilters ?? []));
|
|
33
|
+
fetchURL.searchParams.set('globalFilter', globalFilter ?? '');
|
|
34
|
+
fetchURL.searchParams.set('sorting', JSON.stringify(sorting ?? []));
|
|
35
|
+
//use whatever fetch library you want, fetch, axios, etc
|
|
36
|
+
const response = await fetch(fetchURL.href);
|
|
37
|
+
const json = (await response.json());
|
|
38
|
+
return json;
|
|
39
|
+
},
|
|
40
|
+
placeholderData: keepPreviousData, //don't go to 0 rows when refetching or paginating to next page
|
|
41
|
+
});
|
|
42
|
+
const columns = useMemo(
|
|
43
|
+
//column definitions...
|
|
44
|
+
() => [
|
|
45
|
+
{
|
|
46
|
+
accessorKey: 'firstName',
|
|
47
|
+
header: 'First Name',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
accessorKey: 'lastName',
|
|
51
|
+
header: 'Last Name',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
accessorKey: 'address',
|
|
55
|
+
header: 'Address',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
accessorKey: 'state',
|
|
59
|
+
header: 'State',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
accessorKey: 'phoneNumber',
|
|
63
|
+
header: 'Phone Number',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
accessorFn: (row) => new Date(row.lastLogin),
|
|
67
|
+
id: 'lastLogin',
|
|
68
|
+
header: 'Last Login',
|
|
69
|
+
Cell: ({ cell }) => new Date(cell.getValue()).toLocaleString(),
|
|
70
|
+
filterFn: 'greaterThan',
|
|
71
|
+
filterVariant: 'date',
|
|
72
|
+
enableGlobalFilter: false,
|
|
73
|
+
},
|
|
74
|
+
], []);
|
|
75
|
+
const table = useMaterialReactTable({
|
|
76
|
+
columns,
|
|
77
|
+
data,
|
|
78
|
+
initialState: { showColumnFilters: true },
|
|
79
|
+
manualFiltering: true, //turn off built-in client-side filtering
|
|
80
|
+
manualPagination: true, //turn off built-in client-side pagination
|
|
81
|
+
manualSorting: true, //turn off built-in client-side sorting
|
|
82
|
+
muiToolbarAlertBannerProps: isError
|
|
83
|
+
? {
|
|
84
|
+
color: 'error',
|
|
85
|
+
children: 'Error loading data',
|
|
86
|
+
}
|
|
87
|
+
: undefined,
|
|
88
|
+
onColumnFiltersChange: setColumnFilters,
|
|
89
|
+
onGlobalFilterChange: setGlobalFilter,
|
|
90
|
+
onPaginationChange: setPagination,
|
|
91
|
+
onSortingChange: setSorting,
|
|
92
|
+
renderTopToolbarCustomActions: () => (<Tooltip arrow title="Refresh Data">
|
|
93
|
+
<IconButton onClick={() => refetch()}>
|
|
94
|
+
<RefreshIcon />
|
|
95
|
+
</IconButton>
|
|
96
|
+
</Tooltip>),
|
|
97
|
+
rowCount: meta?.totalRowCount ?? 0,
|
|
98
|
+
state: {
|
|
99
|
+
columnFilters,
|
|
100
|
+
globalFilter,
|
|
101
|
+
isLoading,
|
|
102
|
+
pagination,
|
|
103
|
+
showAlertBanner: isError,
|
|
104
|
+
showProgressBars: isRefetching,
|
|
105
|
+
sorting,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
return <MaterialReactTable table={table}/>;
|
|
109
|
+
};
|
|
110
|
+
//react query setup in App.tsx
|
|
111
|
+
const ReactQueryDevtoolsProduction = lazy(() => import('@tanstack/react-query-devtools/build/modern/production.js').then((d) => ({
|
|
112
|
+
default: d.ReactQueryDevtools,
|
|
113
|
+
})));
|
|
114
|
+
const queryClient = new QueryClient();
|
|
115
|
+
export default function App() {
|
|
116
|
+
return (<QueryClientProvider client={queryClient}>
|
|
117
|
+
<Example />
|
|
118
|
+
<Suspense fallback={null}>
|
|
119
|
+
<ReactQueryDevtoolsProduction />
|
|
120
|
+
</Suspense>
|
|
121
|
+
</QueryClientProvider>);
|
|
122
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { lazy, Suspense, useMemo, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
MaterialReactTable,
|
|
4
|
+
useMaterialReactTable,
|
|
5
|
+
type MRT_ColumnDef,
|
|
6
|
+
type MRT_ColumnFiltersState,
|
|
7
|
+
type MRT_PaginationState,
|
|
8
|
+
type MRT_SortingState,
|
|
9
|
+
} from 'material-react-table';
|
|
10
|
+
import { IconButton, Tooltip } from '@mui/material';
|
|
11
|
+
import RefreshIcon from '@mui/icons-material/Refresh';
|
|
12
|
+
import {
|
|
13
|
+
QueryClient,
|
|
14
|
+
QueryClientProvider,
|
|
15
|
+
keepPreviousData,
|
|
16
|
+
useQuery,
|
|
17
|
+
} from '@tanstack/react-query'; //note: this is TanStack React Query V5
|
|
18
|
+
|
|
19
|
+
//Your API response shape will probably be different. Knowing a total row count is important though.
|
|
20
|
+
type UserApiResponse = {
|
|
21
|
+
data: Array<User>;
|
|
22
|
+
meta: {
|
|
23
|
+
totalRowCount: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type User = {
|
|
28
|
+
firstName: string;
|
|
29
|
+
lastName: string;
|
|
30
|
+
address: string;
|
|
31
|
+
state: string;
|
|
32
|
+
phoneNumber: string;
|
|
33
|
+
lastLogin: Date;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const Example = () => {
|
|
37
|
+
//manage our own state for stuff we want to pass to the API
|
|
38
|
+
const [columnFilters, setColumnFilters] = useState<MRT_ColumnFiltersState>(
|
|
39
|
+
[],
|
|
40
|
+
);
|
|
41
|
+
const [globalFilter, setGlobalFilter] = useState('');
|
|
42
|
+
const [sorting, setSorting] = useState<MRT_SortingState>([]);
|
|
43
|
+
const [pagination, setPagination] = useState<MRT_PaginationState>({
|
|
44
|
+
pageIndex: 0,
|
|
45
|
+
pageSize: 10,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
//consider storing this code in a custom hook (i.e useFetchUsers)
|
|
49
|
+
const {
|
|
50
|
+
data: { data = [], meta } = {}, //your data and api response will probably be different
|
|
51
|
+
isError,
|
|
52
|
+
isRefetching,
|
|
53
|
+
isLoading,
|
|
54
|
+
refetch,
|
|
55
|
+
} = useQuery<UserApiResponse>({
|
|
56
|
+
queryKey: [
|
|
57
|
+
'users-list',
|
|
58
|
+
{
|
|
59
|
+
columnFilters, //refetch when columnFilters changes
|
|
60
|
+
globalFilter, //refetch when globalFilter changes
|
|
61
|
+
pagination, //refetch when pagination changes
|
|
62
|
+
sorting, //refetch when sorting changes
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
queryFn: async () => {
|
|
66
|
+
const fetchURL = new URL('/api/data', location.origin);
|
|
67
|
+
|
|
68
|
+
//read our state and pass it to the API as query params
|
|
69
|
+
fetchURL.searchParams.set(
|
|
70
|
+
'start',
|
|
71
|
+
`${pagination.pageIndex * pagination.pageSize}`,
|
|
72
|
+
);
|
|
73
|
+
fetchURL.searchParams.set('size', `${pagination.pageSize}`);
|
|
74
|
+
fetchURL.searchParams.set('filters', JSON.stringify(columnFilters ?? []));
|
|
75
|
+
fetchURL.searchParams.set('globalFilter', globalFilter ?? '');
|
|
76
|
+
fetchURL.searchParams.set('sorting', JSON.stringify(sorting ?? []));
|
|
77
|
+
|
|
78
|
+
//use whatever fetch library you want, fetch, axios, etc
|
|
79
|
+
const response = await fetch(fetchURL.href);
|
|
80
|
+
const json = (await response.json()) as UserApiResponse;
|
|
81
|
+
return json;
|
|
82
|
+
},
|
|
83
|
+
placeholderData: keepPreviousData, //don't go to 0 rows when refetching or paginating to next page
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const columns = useMemo<MRT_ColumnDef<User>[]>(
|
|
87
|
+
//column definitions...
|
|
88
|
+
() => [
|
|
89
|
+
{
|
|
90
|
+
accessorKey: 'firstName',
|
|
91
|
+
header: 'First Name',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
accessorKey: 'lastName',
|
|
95
|
+
header: 'Last Name',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
accessorKey: 'address',
|
|
99
|
+
header: 'Address',
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
accessorKey: 'state',
|
|
103
|
+
header: 'State',
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
accessorKey: 'phoneNumber',
|
|
107
|
+
header: 'Phone Number',
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
accessorFn: (row) => new Date(row.lastLogin),
|
|
111
|
+
id: 'lastLogin',
|
|
112
|
+
header: 'Last Login',
|
|
113
|
+
Cell: ({ cell }) => new Date(cell.getValue<Date>()).toLocaleString(),
|
|
114
|
+
filterFn: 'greaterThan',
|
|
115
|
+
filterVariant: 'date',
|
|
116
|
+
enableGlobalFilter: false,
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
[],
|
|
120
|
+
//end
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const table = useMaterialReactTable({
|
|
124
|
+
columns,
|
|
125
|
+
data,
|
|
126
|
+
initialState: { showColumnFilters: true },
|
|
127
|
+
manualFiltering: true, //turn off built-in client-side filtering
|
|
128
|
+
manualPagination: true, //turn off built-in client-side pagination
|
|
129
|
+
manualSorting: true, //turn off built-in client-side sorting
|
|
130
|
+
muiToolbarAlertBannerProps: isError
|
|
131
|
+
? {
|
|
132
|
+
color: 'error',
|
|
133
|
+
children: 'Error loading data',
|
|
134
|
+
}
|
|
135
|
+
: undefined,
|
|
136
|
+
onColumnFiltersChange: setColumnFilters,
|
|
137
|
+
onGlobalFilterChange: setGlobalFilter,
|
|
138
|
+
onPaginationChange: setPagination,
|
|
139
|
+
onSortingChange: setSorting,
|
|
140
|
+
renderTopToolbarCustomActions: () => (
|
|
141
|
+
<Tooltip arrow title="Refresh Data">
|
|
142
|
+
<IconButton onClick={() => refetch()}>
|
|
143
|
+
<RefreshIcon />
|
|
144
|
+
</IconButton>
|
|
145
|
+
</Tooltip>
|
|
146
|
+
),
|
|
147
|
+
rowCount: meta?.totalRowCount ?? 0,
|
|
148
|
+
state: {
|
|
149
|
+
columnFilters,
|
|
150
|
+
globalFilter,
|
|
151
|
+
isLoading,
|
|
152
|
+
pagination,
|
|
153
|
+
showAlertBanner: isError,
|
|
154
|
+
showProgressBars: isRefetching,
|
|
155
|
+
sorting,
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return <MaterialReactTable table={table} />;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
//react query setup in App.tsx
|
|
163
|
+
const ReactQueryDevtoolsProduction = lazy(() =>
|
|
164
|
+
import('@tanstack/react-query-devtools/build/modern/production.js').then(
|
|
165
|
+
(d) => ({
|
|
166
|
+
default: d.ReactQueryDevtools,
|
|
167
|
+
}),
|
|
168
|
+
),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const queryClient = new QueryClient();
|
|
172
|
+
|
|
173
|
+
export default function App() {
|
|
174
|
+
return (
|
|
175
|
+
<QueryClientProvider client={queryClient}>
|
|
176
|
+
<Example />
|
|
177
|
+
<Suspense fallback={null}>
|
|
178
|
+
<ReactQueryDevtoolsProduction />
|
|
179
|
+
</Suspense>
|
|
180
|
+
</QueryClientProvider>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
|
|
3
|
+
const Example = () => {
|
|
4
|
+
//data and fetching state
|
|
5
|
+
const [data, setData] = useState([]);
|
|
6
|
+
const [isError, setIsError] = useState(false);
|
|
7
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
8
|
+
const [isRefetching, setIsRefetching] = useState(false);
|
|
9
|
+
const [rowCount, setRowCount] = useState(0);
|
|
10
|
+
//table state
|
|
11
|
+
const [columnFilters, setColumnFilters] = useState([]);
|
|
12
|
+
const [globalFilter, setGlobalFilter] = useState('');
|
|
13
|
+
const [sorting, setSorting] = useState([]);
|
|
14
|
+
const [pagination, setPagination] = useState({
|
|
15
|
+
pageIndex: 0,
|
|
16
|
+
pageSize: 10,
|
|
17
|
+
});
|
|
18
|
+
//if you want to avoid useEffect, look at the React Query example instead
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const fetchData = async () => {
|
|
21
|
+
if (!data.length) {
|
|
22
|
+
setIsLoading(true);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
setIsRefetching(true);
|
|
26
|
+
}
|
|
27
|
+
const url = new URL('/api/data', location.origin);
|
|
28
|
+
url.searchParams.set('start', `${pagination.pageIndex * pagination.pageSize}`);
|
|
29
|
+
url.searchParams.set('size', `${pagination.pageSize}`);
|
|
30
|
+
url.searchParams.set('filters', JSON.stringify(columnFilters ?? []));
|
|
31
|
+
url.searchParams.set('globalFilter', globalFilter ?? '');
|
|
32
|
+
url.searchParams.set('sorting', JSON.stringify(sorting ?? []));
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch(url.href);
|
|
35
|
+
const json = (await response.json());
|
|
36
|
+
setData(json.data);
|
|
37
|
+
setRowCount(json.meta.totalRowCount);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
setIsError(true);
|
|
41
|
+
console.error(error);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
setIsError(false);
|
|
45
|
+
setIsLoading(false);
|
|
46
|
+
setIsRefetching(false);
|
|
47
|
+
};
|
|
48
|
+
fetchData();
|
|
49
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
50
|
+
}, [
|
|
51
|
+
columnFilters, //re-fetch when column filters change
|
|
52
|
+
globalFilter, //re-fetch when global filter changes
|
|
53
|
+
pagination.pageIndex, //re-fetch when page index changes
|
|
54
|
+
pagination.pageSize, //re-fetch when page size changes
|
|
55
|
+
sorting, //re-fetch when sorting changes
|
|
56
|
+
]);
|
|
57
|
+
const columns = useMemo(() => [
|
|
58
|
+
{
|
|
59
|
+
accessorKey: 'firstName',
|
|
60
|
+
header: 'First Name',
|
|
61
|
+
},
|
|
62
|
+
//column definitions...
|
|
63
|
+
{
|
|
64
|
+
accessorKey: 'lastName',
|
|
65
|
+
header: 'Last Name',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
accessorKey: 'address',
|
|
69
|
+
header: 'Address',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
accessorKey: 'state',
|
|
73
|
+
header: 'State',
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
accessorKey: 'phoneNumber',
|
|
77
|
+
header: 'Phone Number',
|
|
78
|
+
},
|
|
79
|
+
//end
|
|
80
|
+
], []);
|
|
81
|
+
const table = useMaterialReactTable({
|
|
82
|
+
columns,
|
|
83
|
+
data,
|
|
84
|
+
enableRowSelection: true,
|
|
85
|
+
getRowId: (row) => row.phoneNumber,
|
|
86
|
+
initialState: { showColumnFilters: true },
|
|
87
|
+
manualFiltering: true,
|
|
88
|
+
manualPagination: true,
|
|
89
|
+
manualSorting: true,
|
|
90
|
+
muiToolbarAlertBannerProps: isError
|
|
91
|
+
? {
|
|
92
|
+
color: 'error',
|
|
93
|
+
children: 'Error loading data',
|
|
94
|
+
}
|
|
95
|
+
: undefined,
|
|
96
|
+
onColumnFiltersChange: setColumnFilters,
|
|
97
|
+
onGlobalFilterChange: setGlobalFilter,
|
|
98
|
+
onPaginationChange: setPagination,
|
|
99
|
+
onSortingChange: setSorting,
|
|
100
|
+
rowCount,
|
|
101
|
+
state: {
|
|
102
|
+
columnFilters,
|
|
103
|
+
globalFilter,
|
|
104
|
+
isLoading,
|
|
105
|
+
pagination,
|
|
106
|
+
showAlertBanner: isError,
|
|
107
|
+
showProgressBars: isRefetching,
|
|
108
|
+
sorting,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
return <MaterialReactTable table={table}/>;
|
|
112
|
+
};
|
|
113
|
+
export default Example;
|