@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,288 @@
1
+ import { lazy, Suspense, useMemo, useState } from 'react';
2
+ import { MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { Box, Button, CircularProgress, IconButton, Tooltip, Typography, } from '@mui/material';
4
+ import { QueryClient, QueryClientProvider, useMutation, useQuery, useQueryClient, } from '@tanstack/react-query';
5
+ import { fakeData, usStates } from './makeData';
6
+ import DeleteIcon from '@mui/icons-material/Delete';
7
+ const Example = () => {
8
+ const [validationErrors, setValidationErrors] = useState({});
9
+ //keep track of rows that have been edited
10
+ const [editedUsers, setEditedUsers] = useState({});
11
+ const columns = useMemo(() => [
12
+ {
13
+ accessorKey: 'id',
14
+ header: 'Id',
15
+ enableEditing: false,
16
+ size: 80,
17
+ },
18
+ {
19
+ accessorKey: 'firstName',
20
+ header: 'First Name',
21
+ muiEditTextFieldProps: ({ cell, row }) => ({
22
+ type: 'text',
23
+ required: true,
24
+ error: !!validationErrors?.[cell.id],
25
+ helperText: validationErrors?.[cell.id],
26
+ //store edited user in state to be saved later
27
+ onBlur: (event) => {
28
+ const validationError = !validateRequired(event.currentTarget.value)
29
+ ? 'Required'
30
+ : undefined;
31
+ setValidationErrors({
32
+ ...validationErrors,
33
+ [cell.id]: validationError,
34
+ });
35
+ setEditedUsers({ ...editedUsers, [row.id]: row.original });
36
+ },
37
+ }),
38
+ },
39
+ {
40
+ accessorKey: 'lastName',
41
+ header: 'Last Name',
42
+ muiEditTextFieldProps: ({ cell, row }) => ({
43
+ type: 'text',
44
+ required: true,
45
+ error: !!validationErrors?.[cell.id],
46
+ helperText: validationErrors?.[cell.id],
47
+ //store edited user in state to be saved later
48
+ onBlur: (event) => {
49
+ const validationError = !validateRequired(event.currentTarget.value)
50
+ ? 'Required'
51
+ : undefined;
52
+ setValidationErrors({
53
+ ...validationErrors,
54
+ [cell.id]: validationError,
55
+ });
56
+ setEditedUsers({ ...editedUsers, [row.id]: row.original });
57
+ },
58
+ }),
59
+ },
60
+ {
61
+ accessorKey: 'email',
62
+ header: 'Email',
63
+ muiEditTextFieldProps: ({ cell, row }) => ({
64
+ type: 'email',
65
+ required: true,
66
+ error: !!validationErrors?.[cell.id],
67
+ helperText: validationErrors?.[cell.id],
68
+ //store edited user in state to be saved later
69
+ onBlur: (event) => {
70
+ const validationError = !validateEmail(event.currentTarget.value)
71
+ ? 'Incorrect Email Format'
72
+ : undefined;
73
+ setValidationErrors({
74
+ ...validationErrors,
75
+ [cell.id]: validationError,
76
+ });
77
+ setEditedUsers({ ...editedUsers, [row.id]: row.original });
78
+ },
79
+ }),
80
+ },
81
+ {
82
+ accessorKey: 'state',
83
+ header: 'State',
84
+ editVariant: 'select',
85
+ editSelectOptions: usStates,
86
+ muiEditTextFieldProps: ({ row }) => ({
87
+ select: true,
88
+ error: !!validationErrors?.state,
89
+ helperText: validationErrors?.state,
90
+ onChange: (event) => setEditedUsers({
91
+ ...editedUsers,
92
+ [row.id]: { ...row.original, state: event.target.value },
93
+ }),
94
+ }),
95
+ },
96
+ ], [editedUsers, validationErrors]);
97
+ //call CREATE hook
98
+ const { mutateAsync: createUser, isPending: isCreatingUser } = useCreateUser();
99
+ //call READ hook
100
+ const { data: fetchedUsers = [], isError: isLoadingUsersError, isFetching: isFetchingUsers, isLoading: isLoadingUsers, } = useGetUsers();
101
+ //call UPDATE hook
102
+ const { mutateAsync: updateUsers, isPending: isUpdatingUsers } = useUpdateUsers();
103
+ //call DELETE hook
104
+ const { mutateAsync: deleteUser, isPending: isDeletingUser } = useDeleteUser();
105
+ //CREATE action
106
+ const handleCreateUser = async ({ values, table, }) => {
107
+ const newValidationErrors = validateUser(values);
108
+ if (Object.values(newValidationErrors).some((error) => error)) {
109
+ setValidationErrors(newValidationErrors);
110
+ return;
111
+ }
112
+ setValidationErrors({});
113
+ await createUser(values);
114
+ table.setCreatingRow(null); //exit creating mode
115
+ };
116
+ //UPDATE action
117
+ const handleSaveUsers = async () => {
118
+ if (Object.values(validationErrors).some((error) => !!error))
119
+ return;
120
+ await updateUsers(Object.values(editedUsers));
121
+ setEditedUsers({});
122
+ };
123
+ //DELETE action
124
+ const openDeleteConfirmModal = (row) => {
125
+ if (window.confirm('Are you sure you want to delete this user?')) {
126
+ deleteUser(row.original.id);
127
+ }
128
+ };
129
+ const table = useMaterialReactTable({
130
+ columns,
131
+ data: fetchedUsers,
132
+ createDisplayMode: 'row', // ('modal', and 'custom' are also available)
133
+ editDisplayMode: 'cell', // ('modal', 'row', 'table', and 'custom' are also available)
134
+ enableCellActions: true,
135
+ enableClickToCopy: 'context-menu',
136
+ enableColumnPinning: true,
137
+ enableEditing: true,
138
+ enableRowActions: true,
139
+ getRowId: (row) => row.id,
140
+ muiToolbarAlertBannerProps: isLoadingUsersError
141
+ ? {
142
+ color: 'error',
143
+ children: 'Error loading data',
144
+ }
145
+ : undefined,
146
+ muiTableContainerProps: {
147
+ sx: {
148
+ minHeight: '500px',
149
+ },
150
+ },
151
+ onCreatingRowCancel: () => setValidationErrors({}),
152
+ onCreatingRowSave: handleCreateUser,
153
+ renderRowActions: ({ row }) => (<Box sx={{ display: 'flex', gap: '1rem' }}>
154
+ <Tooltip title="Delete">
155
+ <IconButton color="error" onClick={() => openDeleteConfirmModal(row)}>
156
+ <DeleteIcon />
157
+ </IconButton>
158
+ </Tooltip>
159
+ </Box>),
160
+ renderBottomToolbarCustomActions: () => (<Box sx={{ display: 'flex', gap: '1rem', alignItems: 'center' }}>
161
+ <Button color="success" variant="contained" onClick={handleSaveUsers} disabled={Object.keys(editedUsers).length === 0 ||
162
+ Object.values(validationErrors).some((error) => !!error)}>
163
+ {isUpdatingUsers ? <CircularProgress size={25}/> : 'Save'}
164
+ </Button>
165
+ {Object.values(validationErrors).some((error) => !!error) && (<Typography color="error">Fix errors before submitting</Typography>)}
166
+ </Box>),
167
+ renderTopToolbarCustomActions: ({ table }) => (<Button variant="contained" onClick={() => {
168
+ table.setCreatingRow(true); //simplest way to open the create row modal with no default values
169
+ //or you can pass in a row object to set default values with the `createRow` helper function
170
+ // table.setCreatingRow(
171
+ // createRow(table, {
172
+ // //optionally pass in default values for the new row, useful for nested data or other complex scenarios
173
+ // }),
174
+ // );
175
+ }}>
176
+ Create New User
177
+ </Button>),
178
+ initialState: {
179
+ columnPinning: {
180
+ end: ['mrt-row-actions'],
181
+ start: [],
182
+ },
183
+ },
184
+ state: {
185
+ isLoading: isLoadingUsers,
186
+ isSaving: isCreatingUser || isUpdatingUsers || isDeletingUser,
187
+ showAlertBanner: isLoadingUsersError,
188
+ showProgressBars: isFetchingUsers,
189
+ },
190
+ });
191
+ return <MaterialReactTable table={table}/>;
192
+ };
193
+ //CREATE hook (post new user to api)
194
+ function useCreateUser() {
195
+ const queryClient = useQueryClient();
196
+ return useMutation({
197
+ mutationFn: async (user) => {
198
+ //send api update request here
199
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
200
+ return Promise.resolve();
201
+ },
202
+ //client side optimistic update
203
+ onMutate: (newUserInfo) => {
204
+ queryClient.setQueryData(['users'], (prevUsers) => [
205
+ ...prevUsers,
206
+ {
207
+ ...newUserInfo,
208
+ id: (Math.random() + 1).toString(36).substring(7),
209
+ },
210
+ ]);
211
+ },
212
+ // onSettled: () => queryClient.invalidateQueries({ queryKey: ['users'] }), //refetch users after mutation, disabled for demo
213
+ });
214
+ }
215
+ //READ hook (get users from api)
216
+ function useGetUsers() {
217
+ return useQuery({
218
+ queryKey: ['users'],
219
+ queryFn: async () => {
220
+ //send api request here
221
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
222
+ return Promise.resolve(fakeData);
223
+ },
224
+ refetchOnWindowFocus: false,
225
+ });
226
+ }
227
+ //UPDATE hook (put user in api)
228
+ function useUpdateUsers() {
229
+ const queryClient = useQueryClient();
230
+ return useMutation({
231
+ mutationFn: async (users) => {
232
+ //send api update request here
233
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
234
+ return Promise.resolve();
235
+ },
236
+ //client side optimistic update
237
+ onMutate: (newUsers) => {
238
+ queryClient.setQueryData(['users'], (prevUsers) => prevUsers?.map((user) => {
239
+ const newUser = newUsers.find((u) => u.id === user.id);
240
+ return newUser ? newUser : user;
241
+ }));
242
+ },
243
+ // onSettled: () => queryClient.invalidateQueries({ queryKey: ['users'] }), //refetch users after mutation, disabled for demo
244
+ });
245
+ }
246
+ //DELETE hook (delete user in api)
247
+ function useDeleteUser() {
248
+ const queryClient = useQueryClient();
249
+ return useMutation({
250
+ mutationFn: async (userId) => {
251
+ //send api update request here
252
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
253
+ return Promise.resolve();
254
+ },
255
+ //client side optimistic update
256
+ onMutate: (userId) => {
257
+ queryClient.setQueryData(['users'], (prevUsers) => prevUsers?.filter((user) => user.id !== userId));
258
+ },
259
+ // onSettled: () => queryClient.invalidateQueries({ queryKey: ['users'] }), //refetch users after mutation, disabled for demo
260
+ });
261
+ }
262
+ //react query setup in App.tsx
263
+ const ReactQueryDevtoolsProduction = lazy(() => import('@tanstack/react-query-devtools/build/modern/production.js').then((d) => ({
264
+ default: d.ReactQueryDevtools,
265
+ })));
266
+ const queryClient = new QueryClient();
267
+ export default function App() {
268
+ return (<QueryClientProvider client={queryClient}>
269
+ <Example />
270
+ <Suspense fallback={null}>
271
+ <ReactQueryDevtoolsProduction />
272
+ </Suspense>
273
+ </QueryClientProvider>);
274
+ }
275
+ const validateRequired = (value) => !!value.length;
276
+ const validateEmail = (email) => !!email.length &&
277
+ email
278
+ .toLowerCase()
279
+ .match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
280
+ function validateUser(user) {
281
+ return {
282
+ firstName: !validateRequired(user.firstName)
283
+ ? 'First Name is Required'
284
+ : '',
285
+ lastName: !validateRequired(user.lastName) ? 'Last Name is Required' : '',
286
+ email: !validateEmail(user.email) ? 'Incorrect Email Format' : '',
287
+ };
288
+ }
@@ -0,0 +1,275 @@
1
+ import { lazy, Suspense, useMemo, useState } from 'react';
2
+ import { MRT_EditActionButtons, MaterialReactTable, useMaterialReactTable, } from 'material-react-table';
3
+ import { Box, Button, DialogActions, DialogContent, DialogTitle, IconButton, Tooltip, } from '@mui/material';
4
+ import { QueryClient, QueryClientProvider, useMutation, useQuery, useQueryClient, } from '@tanstack/react-query';
5
+ import { fakeData, usStates } from './makeData';
6
+ import EditIcon from '@mui/icons-material/Edit';
7
+ import DeleteIcon from '@mui/icons-material/Delete';
8
+ const Example = () => {
9
+ const [validationErrors, setValidationErrors] = useState({});
10
+ const columns = useMemo(() => [
11
+ {
12
+ accessorKey: 'id',
13
+ header: 'Id',
14
+ enableEditing: false,
15
+ size: 80,
16
+ },
17
+ {
18
+ accessorKey: 'firstName',
19
+ header: 'First Name',
20
+ muiEditTextFieldProps: {
21
+ required: true,
22
+ error: !!validationErrors?.firstName,
23
+ helperText: validationErrors?.firstName,
24
+ //remove any previous validation errors when user focuses on the input
25
+ onFocus: () => setValidationErrors({
26
+ ...validationErrors,
27
+ firstName: undefined,
28
+ }),
29
+ //optionally add validation checking for onBlur or onChange
30
+ },
31
+ },
32
+ {
33
+ accessorKey: 'lastName',
34
+ header: 'Last Name',
35
+ muiEditTextFieldProps: {
36
+ required: true,
37
+ error: !!validationErrors?.lastName,
38
+ helperText: validationErrors?.lastName,
39
+ //remove any previous validation errors when user focuses on the input
40
+ onFocus: () => setValidationErrors({
41
+ ...validationErrors,
42
+ lastName: undefined,
43
+ }),
44
+ },
45
+ },
46
+ {
47
+ accessorKey: 'email',
48
+ header: 'Email',
49
+ muiEditTextFieldProps: {
50
+ type: 'email',
51
+ required: true,
52
+ error: !!validationErrors?.email,
53
+ helperText: validationErrors?.email,
54
+ //remove any previous validation errors when user focuses on the input
55
+ onFocus: () => setValidationErrors({
56
+ ...validationErrors,
57
+ email: undefined,
58
+ }),
59
+ },
60
+ },
61
+ {
62
+ accessorKey: 'state',
63
+ header: 'State',
64
+ editVariant: 'select',
65
+ editSelectOptions: usStates,
66
+ muiEditTextFieldProps: {
67
+ select: true,
68
+ error: !!validationErrors?.state,
69
+ helperText: validationErrors?.state,
70
+ },
71
+ },
72
+ ], [validationErrors]);
73
+ //call CREATE hook
74
+ const { mutateAsync: createUser, isPending: isCreatingUser } = useCreateUser();
75
+ //call READ hook
76
+ const { data: fetchedUsers = [], isError: isLoadingUsersError, isFetching: isFetchingUsers, isLoading: isLoadingUsers, } = useGetUsers();
77
+ //call UPDATE hook
78
+ const { mutateAsync: updateUser, isPending: isUpdatingUser } = useUpdateUser();
79
+ //call DELETE hook
80
+ const { mutateAsync: deleteUser, isPending: isDeletingUser } = useDeleteUser();
81
+ //CREATE action
82
+ const handleCreateUser = async ({ values, table, }) => {
83
+ const newValidationErrors = validateUser(values);
84
+ if (Object.values(newValidationErrors).some((error) => error)) {
85
+ setValidationErrors(newValidationErrors);
86
+ return;
87
+ }
88
+ setValidationErrors({});
89
+ await createUser(values);
90
+ table.setCreatingRow(null); //exit creating mode
91
+ };
92
+ //UPDATE action
93
+ const handleSaveUser = async ({ values, table, }) => {
94
+ const newValidationErrors = validateUser(values);
95
+ if (Object.values(newValidationErrors).some((error) => error)) {
96
+ setValidationErrors(newValidationErrors);
97
+ return;
98
+ }
99
+ setValidationErrors({});
100
+ await updateUser(values);
101
+ table.setEditingRow(null); //exit editing mode
102
+ };
103
+ //DELETE action
104
+ const openDeleteConfirmModal = (row) => {
105
+ if (window.confirm('Are you sure you want to delete this user?')) {
106
+ deleteUser(row.original.id);
107
+ }
108
+ };
109
+ const table = useMaterialReactTable({
110
+ columns,
111
+ data: fetchedUsers,
112
+ createDisplayMode: 'modal', //default ('row', and 'custom' are also available)
113
+ editDisplayMode: 'modal', //default ('row', 'cell', 'table', and 'custom' are also available)
114
+ enableEditing: true,
115
+ getRowId: (row) => row.id,
116
+ muiToolbarAlertBannerProps: isLoadingUsersError
117
+ ? {
118
+ color: 'error',
119
+ children: 'Error loading data',
120
+ }
121
+ : undefined,
122
+ muiTableContainerProps: {
123
+ sx: {
124
+ minHeight: '500px',
125
+ },
126
+ },
127
+ onCreatingRowCancel: () => setValidationErrors({}),
128
+ onCreatingRowSave: handleCreateUser,
129
+ onEditingRowCancel: () => setValidationErrors({}),
130
+ onEditingRowSave: handleSaveUser,
131
+ //optionally customize modal content
132
+ renderCreateRowDialogContent: ({ table, row, internalEditComponents }) => (<>
133
+ <DialogTitle variant="h3">Create New User</DialogTitle>
134
+ <DialogContent sx={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
135
+ {internalEditComponents} {/* or render custom edit components here */}
136
+ </DialogContent>
137
+ <DialogActions>
138
+ <MRT_EditActionButtons variant="text" table={table} row={row}/>
139
+ </DialogActions>
140
+ </>),
141
+ //optionally customize modal content
142
+ renderEditRowDialogContent: ({ table, row, internalEditComponents }) => (<>
143
+ <DialogTitle variant="h3">Edit User</DialogTitle>
144
+ <DialogContent sx={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
145
+ {internalEditComponents} {/* or render custom edit components here */}
146
+ </DialogContent>
147
+ <DialogActions>
148
+ <MRT_EditActionButtons variant="text" table={table} row={row}/>
149
+ </DialogActions>
150
+ </>),
151
+ renderRowActions: ({ row, table }) => (<Box sx={{ display: 'flex', gap: '1rem' }}>
152
+ <Tooltip title="Edit">
153
+ <IconButton onClick={() => table.setEditingRow(row)}>
154
+ <EditIcon />
155
+ </IconButton>
156
+ </Tooltip>
157
+ <Tooltip title="Delete">
158
+ <IconButton color="error" onClick={() => openDeleteConfirmModal(row)}>
159
+ <DeleteIcon />
160
+ </IconButton>
161
+ </Tooltip>
162
+ </Box>),
163
+ renderTopToolbarCustomActions: ({ table }) => (<Button variant="contained" onClick={() => {
164
+ table.setCreatingRow(true); //simplest way to open the create row modal with no default values
165
+ //or you can pass in a row object to set default values with the `createRow` helper function
166
+ // table.setCreatingRow(
167
+ // createRow(table, {
168
+ // //optionally pass in default values for the new row, useful for nested data or other complex scenarios
169
+ // }),
170
+ // );
171
+ }}>
172
+ Create New User
173
+ </Button>),
174
+ state: {
175
+ isLoading: isLoadingUsers,
176
+ isSaving: isCreatingUser || isUpdatingUser || isDeletingUser,
177
+ showAlertBanner: isLoadingUsersError,
178
+ showProgressBars: isFetchingUsers,
179
+ },
180
+ });
181
+ return <MaterialReactTable table={table}/>;
182
+ };
183
+ //CREATE hook (post new user to api)
184
+ function useCreateUser() {
185
+ const queryClient = useQueryClient();
186
+ return useMutation({
187
+ mutationFn: async (user) => {
188
+ //send api update request here
189
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
190
+ return Promise.resolve();
191
+ },
192
+ //client side optimistic update
193
+ onMutate: (newUserInfo) => {
194
+ queryClient.setQueryData(['users'], (prevUsers) => [
195
+ ...prevUsers,
196
+ {
197
+ ...newUserInfo,
198
+ id: (Math.random() + 1).toString(36).substring(7),
199
+ },
200
+ ]);
201
+ },
202
+ // onSettled: () => queryClient.invalidateQueries({ queryKey: ['users'] }), //refetch users after mutation, disabled for demo
203
+ });
204
+ }
205
+ //READ hook (get users from api)
206
+ function useGetUsers() {
207
+ return useQuery({
208
+ queryKey: ['users'],
209
+ queryFn: async () => {
210
+ //send api request here
211
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
212
+ return Promise.resolve(fakeData);
213
+ },
214
+ refetchOnWindowFocus: false,
215
+ });
216
+ }
217
+ //UPDATE hook (put user in api)
218
+ function useUpdateUser() {
219
+ const queryClient = useQueryClient();
220
+ return useMutation({
221
+ mutationFn: async (user) => {
222
+ //send api update request here
223
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
224
+ return Promise.resolve();
225
+ },
226
+ //client side optimistic update
227
+ onMutate: (newUserInfo) => {
228
+ queryClient.setQueryData(['users'], (prevUsers) => prevUsers?.map((prevUser) => prevUser.id === newUserInfo.id ? newUserInfo : prevUser));
229
+ },
230
+ // onSettled: () => queryClient.invalidateQueries({ queryKey: ['users'] }), //refetch users after mutation, disabled for demo
231
+ });
232
+ }
233
+ //DELETE hook (delete user in api)
234
+ function useDeleteUser() {
235
+ const queryClient = useQueryClient();
236
+ return useMutation({
237
+ mutationFn: async (userId) => {
238
+ //send api update request here
239
+ await new Promise((resolve) => setTimeout(resolve, 1000)); //fake api call
240
+ return Promise.resolve();
241
+ },
242
+ //client side optimistic update
243
+ onMutate: (userId) => {
244
+ queryClient.setQueryData(['users'], (prevUsers) => prevUsers?.filter((user) => user.id !== userId));
245
+ },
246
+ // onSettled: () => queryClient.invalidateQueries({ queryKey: ['users'] }), //refetch users after mutation, disabled for demo
247
+ });
248
+ }
249
+ //react query setup in App.tsx
250
+ const ReactQueryDevtoolsProduction = lazy(() => import('@tanstack/react-query-devtools/build/modern/production.js').then((d) => ({
251
+ default: d.ReactQueryDevtools,
252
+ })));
253
+ const queryClient = new QueryClient();
254
+ export default function App() {
255
+ return (<QueryClientProvider client={queryClient}>
256
+ <Example />
257
+ <Suspense fallback={null}>
258
+ <ReactQueryDevtoolsProduction />
259
+ </Suspense>
260
+ </QueryClientProvider>);
261
+ }
262
+ const validateRequired = (value) => !!value.length;
263
+ const validateEmail = (email) => !!email.length &&
264
+ email
265
+ .toLowerCase()
266
+ .match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
267
+ function validateUser(user) {
268
+ return {
269
+ firstName: !validateRequired(user.firstName)
270
+ ? 'First Name is Required'
271
+ : '',
272
+ lastName: !validateRequired(user.lastName) ? 'Last Name is Required' : '',
273
+ email: !validateEmail(user.email) ? 'Incorrect Email Format' : '',
274
+ };
275
+ }