-
-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathTS.tsx
More file actions
310 lines (296 loc) · 8.8 KB
/
Copy pathTS.tsx
File metadata and controls
310 lines (296 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { useMemo } from 'react';
//MRT Imports
import {
MaterialReactTable,
useMaterialReactTable,
type MRT_ColumnDef,
MRT_GlobalFilterTextField,
MRT_ToggleFiltersButton,
} from 'material-react-table';
//Material UI Imports
import {
Box,
Button,
ListItemIcon,
MenuItem,
Typography,
lighten,
} from '@mui/material';
//Icons Imports
import AccountCircle from '@mui/icons-material/AccountCircle';
import Send from '@mui/icons-material/Send';
//Mock Data
import { data } from './makeData';
export type Employee = {
firstName: string;
lastName: string;
email: string;
jobTitle: string;
salary: number;
startDate: string;
signatureCatchPhrase: string;
avatar: string;
};
const Example = () => {
const columns = useMemo<MRT_ColumnDef<Employee>[]>(
() => [
{
id: 'employee', //id used to define `group` column
header: 'Employee',
columns: [
{
accessorFn: (row) => `${row.firstName} ${row.lastName}`, //accessorFn used to join multiple data into a single cell
id: 'name', //id is still required when using accessorFn instead of accessorKey
header: 'Name',
size: 250,
Cell: ({ renderedCellValue, row }) => (
<Box
sx={{
display: 'flex',
alignItems: 'center',
gap: '1rem',
}}
>
<img
alt="avatar"
height={30}
src={row.original.avatar}
loading="lazy"
style={{ borderRadius: '50%' }}
/>
{/* using renderedCellValue instead of cell.getValue() preserves filter match highlighting */}
<span>{renderedCellValue}</span>
</Box>
),
},
{
accessorKey: 'email', //accessorKey used to define `data` column. `id` gets set to accessorKey automatically
enableClickToCopy: true,
filterVariant: 'autocomplete',
header: 'Email',
size: 300,
},
],
},
{
id: 'id',
header: 'Job Info',
columns: [
{
accessorKey: 'salary',
// filterVariant: 'range', //if not using filter modes feature, use this instead of filterFn
filterFn: 'between',
header: 'Salary',
size: 200,
//custom conditional format and styling
Cell: ({ cell }) => (
<Box
component="span"
sx={(theme) => ({
backgroundColor:
cell.getValue<number>() < 50_000
? theme.palette.error.dark
: cell.getValue<number>() >= 50_000 &&
cell.getValue<number>() < 75_000
? theme.palette.warning.dark
: theme.palette.success.dark,
borderRadius: '0.25rem',
color: '#fff',
maxWidth: '9ch',
p: '0.25rem',
})}
>
{cell.getValue<number>()?.toLocaleString?.('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
})}
</Box>
),
},
{
accessorKey: 'jobTitle', //hey a simple column for once
header: 'Job Title',
size: 350,
},
{
accessorFn: (row) => new Date(row.startDate), //convert to Date for sorting and filtering
id: 'startDate',
header: 'Start Date',
filterVariant: 'date',
filterFn: 'lessThan',
sortingFn: 'datetime',
Cell: ({ cell }) => cell.getValue<Date>()?.toLocaleDateString(), //render Date as a string
Header: ({ column }) => <em>{column.columnDef.header}</em>, //custom header markup
muiFilterTextFieldProps: {
sx: {
minWidth: '250px',
},
},
},
],
},
],
[],
);
const table = useMaterialReactTable({
columns,
data, //data must be memoized or stable (useState, useMemo, defined outside of this component, etc.)
enableColumnFilterModes: true,
enableColumnOrdering: true,
enableGrouping: true,
enableColumnPinning: true,
enableFacetedValues: true,
enableRowActions: true,
enableRowSelection: true,
initialState: {
showColumnFilters: true,
showGlobalFilter: true,
columnPinning: {
left: ['mrt-row-expand', 'mrt-row-select'],
right: ['mrt-row-actions'],
},
},
paginationDisplayMode: 'pages',
positionToolbarAlertBanner: 'bottom',
muiSearchTextFieldProps: {
size: 'small',
variant: 'outlined',
},
muiPaginationProps: {
color: 'secondary',
rowsPerPageOptions: [10, 20, 30],
shape: 'rounded',
variant: 'outlined',
},
renderDetailPanel: ({ row }) => (
<Box
sx={{
alignItems: 'center',
display: 'flex',
justifyContent: 'space-around',
left: '30px',
maxWidth: '1000px',
position: 'sticky',
width: '100%',
}}
>
<img
alt="avatar"
height={200}
src={row.original.avatar}
loading="lazy"
style={{ borderRadius: '50%' }}
/>
<Box sx={{ textAlign: 'center' }}>
<Typography variant="h4">Signature Catch Phrase:</Typography>
<Typography variant="h1">
"{row.original.signatureCatchPhrase}"
</Typography>
</Box>
</Box>
),
renderRowActionMenuItems: ({ closeMenu }) => [
<MenuItem
key={0}
onClick={() => {
// View profile logic...
closeMenu();
}}
sx={{ m: 0 }}
>
<ListItemIcon>
<AccountCircle />
</ListItemIcon>
View Profile
</MenuItem>,
<MenuItem
key={1}
onClick={() => {
// Send email logic...
closeMenu();
}}
sx={{ m: 0 }}
>
<ListItemIcon>
<Send />
</ListItemIcon>
Send Email
</MenuItem>,
],
renderTopToolbar: ({ table }) => {
const handleDeactivate = () => {
table.getSelectedRowModel().flatRows.map((row) => {
alert('deactivating ' + row.getValue('name'));
});
};
const handleActivate = () => {
table.getSelectedRowModel().flatRows.map((row) => {
alert('activating ' + row.getValue('name'));
});
};
const handleContact = () => {
table.getSelectedRowModel().flatRows.map((row) => {
alert('contact ' + row.getValue('name'));
});
};
return (
<Box
sx={(theme) => ({
backgroundColor: lighten(theme.palette.background.default, 0.05),
display: 'flex',
gap: '0.5rem',
p: '8px',
justifyContent: 'space-between',
})}
>
<Box sx={{ display: 'flex', gap: '0.5rem', alignItems: 'center' }}>
{/* import MRT sub-components */}
<MRT_GlobalFilterTextField table={table} />
<MRT_ToggleFiltersButton table={table} />
</Box>
<Box>
<Box sx={{ display: 'flex', gap: '0.5rem' }}>
<Button
color="error"
disabled={!table.getIsSomeRowsSelected()}
onClick={handleDeactivate}
variant="contained"
>
Deactivate
</Button>
<Button
color="success"
disabled={!table.getIsSomeRowsSelected()}
onClick={handleActivate}
variant="contained"
>
Activate
</Button>
<Button
color="info"
disabled={!table.getIsSomeRowsSelected()}
onClick={handleContact}
variant="contained"
>
Contact
</Button>
</Box>
</Box>
</Box>
);
},
});
return <MaterialReactTable table={table} />;
};
//Date Picker Imports - these should just be in your Context Provider
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
const ExampleWithLocalizationProvider = () => (
//App.tsx or AppProviders file
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Example />
</LocalizationProvider>
);
export default ExampleWithLocalizationProvider;