-
-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathTS.tsx
More file actions
105 lines (100 loc) · 3.06 KB
/
TS.tsx
File metadata and controls
105 lines (100 loc) · 3.06 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
import { useMemo } from 'react';
import { Box } from '@mui/material';
import {
MaterialReactTable,
useMaterialReactTable,
type MRT_ColumnDef,
} from '@glebcha/material-react-table';
import { data, type Person } from './makeData';
const localeStringOptions = {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
} as const;
const Example = () => {
const columns = useMemo<MRT_ColumnDef<Person>[]>(
() => [
{
header: 'First Name',
accessorKey: 'firstName',
},
{
header: 'Last Name',
accessorKey: 'lastName',
},
{
header: 'Gender',
accessorKey: 'gender',
},
{
header: 'State',
accessorKey: 'state',
},
{
header: 'Salary',
accessorKey: 'salary',
aggregationFn: ['count', 'mean', 'median', 'min', 'max'],
//required to render an aggregated cell, show the average salary in the group
AggregatedCell: ({ cell }) => (
<>
Count:{' '}
<Box sx={{ color: 'success.main', fontWeight: 'bold' }}>
{cell.getValue<Array<number>>()?.[0]}
</Box>
Average:{' '}
<Box sx={{ color: 'success.main', fontWeight: 'bold' }}>
{cell
.getValue<Array<number>>()?.[1]
?.toLocaleString?.('en-US', localeStringOptions)}
</Box>
Median:{' '}
<Box sx={{ color: 'success.main', fontWeight: 'bold' }}>
{cell
.getValue<Array<number>>()?.[2]
?.toLocaleString?.('en-US', localeStringOptions)}
</Box>
Min:{' '}
<Box sx={{ color: 'success.main', fontWeight: 'bold' }}>
{cell
.getValue<Array<number>>()?.[3]
?.toLocaleString?.('en-US', localeStringOptions)}
</Box>
Max:{' '}
<Box sx={{ color: 'success.main', fontWeight: 'bold' }}>
{cell
.getValue<Array<number>>()?.[4]
?.toLocaleString?.('en-US', localeStringOptions)}
</Box>
</>
),
//customize normal cell render on normal non-aggregated rows
Cell: ({ cell }) => (
<>
{cell
.getValue<number>()
?.toLocaleString?.('en-US', localeStringOptions)}
</>
),
},
],
[],
);
const table = useMaterialReactTable({
columns,
data,
enableGrouping: true,
enableStickyHeader: true,
initialState: {
density: 'compact',
expanded: true, //expand all groups by default
grouping: ['state'], //an array of columns to group by by default (can be multiple)
pagination: { pageIndex: 0, pageSize: 20 },
sorting: [{ id: 'state', desc: false }], //sort by state by default
},
muiToolbarAlertBannerChipProps: { color: 'primary' },
muiTableContainerProps: { sx: { maxHeight: 700 } },
});
return <MaterialReactTable table={table} />;
};
export default Example;