-
-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathColumnOptionsTable.tsx
More file actions
184 lines (176 loc) · 4.91 KB
/
ColumnOptionsTable.tsx
File metadata and controls
184 lines (176 loc) · 4.91 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
import { useEffect, useMemo, useState } from 'react';
import Link from 'next/link';
import { MaterialReactTable, type MRT_ColumnDef } from '@glebcha/material-react-table';
import {
Link as MuiLink,
Typography,
useMediaQuery,
useTheme,
} from '@mui/material';
import { SampleCodeSnippet } from '../mdx/SampleCodeSnippet';
import { type ColumnOption, columnOptions } from './columnOptions';
interface Props {
onlyOptions?: Set<keyof MRT_ColumnDef<ColumnOption>>;
}
const ColumnOptionsTable = ({ onlyOptions }: Props) => {
const theme = useTheme();
const isDesktop = useMediaQuery('(min-width: 1200px)');
const columns = useMemo<MRT_ColumnDef<ColumnOption>[]>(
() => [
{
accessorKey: 'columnOption',
enableClickToCopy: true,
header: 'Column Option',
muiCopyButtonProps: ({ cell }) => ({
className: 'column-option',
id: `${cell.getValue<string>()}-column-option`,
}),
Cell: ({ renderedCellValue, row }) =>
row.original?.required ? (
<strong style={{ color: theme.palette.primary.dark }}>
{renderedCellValue}*
</strong>
) : (
renderedCellValue
),
},
{
accessorKey: 'type',
header: 'Type',
enableGlobalFilter: false,
Cell: ({ cell }) => (
<SampleCodeSnippet
className="language-ts"
enableCopyButton={false}
style={{
backgroundColor: 'transparent',
fontSize: '0.9rem',
margin: 0,
padding: 0,
minHeight: 'unset',
}}
>
{cell.getValue<string>()}
</SampleCodeSnippet>
),
},
{
accessorKey: 'defaultValue',
enableGlobalFilter: false,
header: 'Default Value',
Cell: ({ cell }) => (
<SampleCodeSnippet
className="language-js"
enableCopyButton={false}
style={{
backgroundColor: 'transparent',
fontSize: '0.9rem',
margin: 0,
padding: 0,
minHeight: 'unset',
}}
>
{cell.getValue<string>()}
</SampleCodeSnippet>
),
},
{
accessorKey: 'description',
enableGlobalFilter: false,
header: 'Description',
},
{
accessorKey: 'link',
disableFilters: true,
enableGlobalFilter: false,
header: 'More Info Links',
Cell: ({ cell, row }) => (
<Link href={cell.getValue<string>()} passHref legacyBehavior>
<MuiLink
target={
cell.getValue<string>().startsWith('http')
? '_blank'
: undefined
}
rel="noopener"
>
{row.original?.linkText}
</MuiLink>
</Link>
),
},
],
[theme],
);
const [columnPinning, setColumnPinning] = useState({});
useEffect(() => {
if (typeof window !== 'undefined') {
if (isDesktop) {
setColumnPinning({
left: ['mrt-row-expand', 'mrt-row-numbers', 'columnOption'],
right: ['link'],
});
} else {
setColumnPinning({});
}
}
}, [isDesktop]);
const data = useMemo(() => {
if (onlyOptions) {
return columnOptions.filter(({ columnOption }) =>
onlyOptions.has(columnOption),
);
}
return columnOptions;
}, [onlyOptions]);
return (
<MaterialReactTable
columns={columns}
data={data}
displayColumnDefOptions={{
'mrt-row-numbers': {
size: 10,
},
'mrt-row-expand': {
size: 10,
},
}}
enableColumnActions={!onlyOptions}
enableColumnFilterModes
enablePagination={false}
enableColumnPinning
enableRowNumbers
enableBottomToolbar={false}
enableTopToolbar={!onlyOptions}
initialState={{
columnVisibility: { description: false },
density: 'compact',
showGlobalFilter: true,
sorting: [{ id: 'columnOption', desc: false }],
}}
muiSearchTextFieldProps={{
placeholder: 'Search Column Options',
sx: { minWidth: '18rem' },
variant: 'outlined',
}}
muiTablePaperProps={{
sx: { mb: '1.5rem' },
id: onlyOptions
? 'relevant-column-options-table'
: 'column-options-table',
}}
positionGlobalFilter="left"
renderDetailPanel={({ row }) => (
<Typography
color={row.original.description ? 'secondary.main' : 'text.secondary'}
>
{row.original.description || 'No Description Provided... Yet...'}
</Typography>
)}
rowNumberDisplayMode="static"
onColumnPinningChange={setColumnPinning}
state={{ columnPinning }}
/>
);
};
export default ColumnOptionsTable;