material-react-table 0.5.3 → 0.5.4
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/dist/MaterialReactTable.d.ts +5 -0
- package/dist/filtersFNs.d.ts +67 -0
- package/dist/localization.d.ts +10 -1
- package/dist/material-react-table.cjs.development.js +367 -69
- package/dist/material-react-table.cjs.development.js.map +1 -1
- package/dist/material-react-table.cjs.production.min.js +1 -1
- package/dist/material-react-table.cjs.production.min.js.map +1 -1
- package/dist/material-react-table.esm.js +368 -70
- package/dist/material-react-table.esm.js.map +1 -1
- package/dist/menus/MRT_FilterMenu.d.ts +10 -0
- package/dist/useMRT.d.ts +13 -9
- package/package.json +6 -2
- package/src/MaterialReactTable.tsx +15 -1
- package/src/buttons/MRT_EditActionButtons.tsx +3 -1
- package/src/buttons/MRT_ToggleColumnActionMenuButton.tsx +4 -3
- package/src/buttons/MRT_ToggleFiltersButton.tsx +3 -1
- package/src/buttons/MRT_ToggleSearchButton.tsx +5 -2
- package/src/filtersFNs.ts +112 -0
- package/src/head/MRT_TableHeadCell.tsx +45 -44
- package/src/index.tsx +0 -2
- package/src/inputs/MRT_FilterTextField.tsx +117 -52
- package/src/inputs/MRT_SearchTextField.tsx +2 -1
- package/src/inputs/MRT_SelectCheckbox.tsx +5 -7
- package/src/localization.ts +20 -2
- package/src/menus/MRT_ColumnActionMenu.tsx +125 -85
- package/src/menus/MRT_FilterMenu.tsx +109 -0
- package/src/useMRT.tsx +67 -13
package/src/localization.ts
CHANGED
|
@@ -11,6 +11,15 @@ export interface MRT_Localization {
|
|
|
11
11
|
columnShowHideMenuShowAll: string;
|
|
12
12
|
expandAllButtonTitle: string;
|
|
13
13
|
expandButtonTitle: string;
|
|
14
|
+
filterMenuItemContains: string;
|
|
15
|
+
filterMenuItemEmpty: string;
|
|
16
|
+
filterMenuItemEndsWith: string;
|
|
17
|
+
filterMenuItemEquals: string;
|
|
18
|
+
filterMenuItemFuzzy: string;
|
|
19
|
+
filterMenuItemNotEmpty: string;
|
|
20
|
+
filterMenuItemNotEquals: string;
|
|
21
|
+
filterMenuItemStartsWith: string;
|
|
22
|
+
filterMenuTitle: string;
|
|
14
23
|
filterTextFieldClearButtonTitle: string;
|
|
15
24
|
filterTextFieldPlaceholder: string;
|
|
16
25
|
rowActionButtonCancel: string;
|
|
@@ -27,9 +36,9 @@ export interface MRT_Localization {
|
|
|
27
36
|
toggleFilterButtonTitle: string;
|
|
28
37
|
toggleFullScreenButtonTitle: string;
|
|
29
38
|
toggleSearchButtonTitle: string;
|
|
30
|
-
toolbarAlertSelectionMessage: string;
|
|
31
39
|
toolbarAlertGroupedByMessage: string;
|
|
32
40
|
toolbarAlertGroupedThenByMessage: string;
|
|
41
|
+
toolbarAlertSelectionMessage: string;
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
export const MRT_DefaultLocalization_EN: MRT_Localization = {
|
|
@@ -45,6 +54,15 @@ export const MRT_DefaultLocalization_EN: MRT_Localization = {
|
|
|
45
54
|
columnShowHideMenuShowAll: 'Show all',
|
|
46
55
|
expandAllButtonTitle: 'Expand all',
|
|
47
56
|
expandButtonTitle: 'Expand',
|
|
57
|
+
filterMenuItemContains: 'Contains Exact',
|
|
58
|
+
filterMenuItemEmpty: 'Empty',
|
|
59
|
+
filterMenuItemEndsWith: 'Ends With',
|
|
60
|
+
filterMenuItemEquals: 'Equals',
|
|
61
|
+
filterMenuItemFuzzy: 'Fuzzy Match',
|
|
62
|
+
filterMenuItemNotEmpty: 'Not Empty',
|
|
63
|
+
filterMenuItemNotEquals: 'Not Equals',
|
|
64
|
+
filterMenuItemStartsWith: 'Starts With',
|
|
65
|
+
filterMenuTitle: 'Filter Mode',
|
|
48
66
|
filterTextFieldClearButtonTitle: 'Clear filter',
|
|
49
67
|
filterTextFieldPlaceholder: 'Filter by {column}',
|
|
50
68
|
rowActionButtonCancel: 'Cancel',
|
|
@@ -61,7 +79,7 @@ export const MRT_DefaultLocalization_EN: MRT_Localization = {
|
|
|
61
79
|
toggleFilterButtonTitle: 'Toggle filters',
|
|
62
80
|
toggleFullScreenButtonTitle: 'Toggle full screen',
|
|
63
81
|
toggleSearchButtonTitle: 'Toggle search',
|
|
64
|
-
toolbarAlertSelectionMessage: '{selectedCount} of {rowCount} row(s) selected',
|
|
65
82
|
toolbarAlertGroupedByMessage: 'Grouped by ',
|
|
66
83
|
toolbarAlertGroupedThenByMessage: ', then by ',
|
|
84
|
+
toolbarAlertSelectionMessage: '{selectedCount} of {rowCount} row(s) selected',
|
|
67
85
|
};
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import React, { FC } from 'react';
|
|
2
|
-
import {
|
|
1
|
+
import React, { FC, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Divider,
|
|
4
|
+
IconButton,
|
|
5
|
+
ListItemIcon,
|
|
6
|
+
ListItemText,
|
|
7
|
+
Menu,
|
|
8
|
+
MenuItem,
|
|
9
|
+
MenuList,
|
|
10
|
+
} from '@mui/material';
|
|
3
11
|
import { useMRT } from '../useMRT';
|
|
4
12
|
import { MRT_HeaderGroup } from '..';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
display: 'flex',
|
|
8
|
-
gap: '0.75rem',
|
|
9
|
-
};
|
|
13
|
+
import { MRT_FilterMenu } from './MRT_FilterMenu';
|
|
14
|
+
import ArrowRightIcon from '@mui/icons-material/ArrowRight';
|
|
10
15
|
|
|
11
16
|
interface Props {
|
|
12
17
|
anchorEl: HTMLElement | null;
|
|
@@ -24,17 +29,21 @@ export const MRT_ColumnActionMenu: FC<Props> = ({
|
|
|
24
29
|
disableFilters,
|
|
25
30
|
disableSortBy,
|
|
26
31
|
enableColumnGrouping,
|
|
27
|
-
localization,
|
|
28
|
-
setShowFilters,
|
|
29
32
|
icons: {
|
|
30
|
-
FilterListIcon,
|
|
31
|
-
SortIcon,
|
|
32
33
|
ClearAllIcon,
|
|
33
34
|
DynamicFeedIcon,
|
|
35
|
+
FilterListIcon,
|
|
36
|
+
SortIcon,
|
|
34
37
|
VisibilityOffIcon,
|
|
35
38
|
},
|
|
39
|
+
idPrefix,
|
|
40
|
+
localization,
|
|
41
|
+
setShowFilters,
|
|
36
42
|
} = useMRT();
|
|
37
43
|
|
|
44
|
+
const [filterMenuAnchorEl, setFilterMenuAnchorEl] =
|
|
45
|
+
useState<null | HTMLElement>(null);
|
|
46
|
+
|
|
38
47
|
const handleClearSort = () => {
|
|
39
48
|
column.clearSortBy();
|
|
40
49
|
setAnchorEl(null);
|
|
@@ -68,7 +77,7 @@ export const MRT_ColumnActionMenu: FC<Props> = ({
|
|
|
68
77
|
.getElementById(
|
|
69
78
|
// @ts-ignore
|
|
70
79
|
column.muiTableHeadCellFilterTextFieldProps?.id ??
|
|
71
|
-
`
|
|
80
|
+
`mrt-${idPrefix}-${column.id}-filter-text-field`,
|
|
72
81
|
)
|
|
73
82
|
?.focus(),
|
|
74
83
|
200,
|
|
@@ -76,88 +85,119 @@ export const MRT_ColumnActionMenu: FC<Props> = ({
|
|
|
76
85
|
setAnchorEl(null);
|
|
77
86
|
};
|
|
78
87
|
|
|
88
|
+
const handleOpenFilterModeMenu = (event: React.MouseEvent<HTMLElement>) => {
|
|
89
|
+
event.stopPropagation();
|
|
90
|
+
setFilterMenuAnchorEl(event.currentTarget);
|
|
91
|
+
};
|
|
92
|
+
|
|
79
93
|
return (
|
|
80
94
|
<Menu
|
|
81
95
|
anchorEl={anchorEl}
|
|
82
96
|
open={!!anchorEl}
|
|
83
97
|
onClose={() => setAnchorEl(null)}
|
|
84
98
|
>
|
|
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
|
-
|
|
99
|
+
<MenuList>
|
|
100
|
+
{!disableSortBy &&
|
|
101
|
+
column.canSort && [
|
|
102
|
+
<MenuItem
|
|
103
|
+
key={1}
|
|
104
|
+
disabled={!column.isSorted}
|
|
105
|
+
onClick={handleClearSort}
|
|
106
|
+
>
|
|
107
|
+
<ListItemIcon>
|
|
108
|
+
<ClearAllIcon />
|
|
109
|
+
</ListItemIcon>
|
|
110
|
+
<ListItemText>
|
|
111
|
+
{localization.columnActionMenuItemClearSort}
|
|
112
|
+
</ListItemText>
|
|
113
|
+
</MenuItem>,
|
|
114
|
+
<MenuItem
|
|
115
|
+
key={2}
|
|
116
|
+
disabled={column.isSorted && !column.isSortedDesc}
|
|
117
|
+
onClick={handleSortAsc}
|
|
118
|
+
>
|
|
119
|
+
<ListItemIcon>
|
|
120
|
+
<SortIcon />
|
|
121
|
+
</ListItemIcon>
|
|
122
|
+
<ListItemText>
|
|
123
|
+
{localization.columnActionMenuItemSortAsc?.replace(
|
|
124
|
+
'{column}',
|
|
125
|
+
String(column.Header),
|
|
126
|
+
)}
|
|
127
|
+
</ListItemText>
|
|
128
|
+
</MenuItem>,
|
|
129
|
+
<MenuItem
|
|
130
|
+
key={3}
|
|
131
|
+
disabled={column.isSorted && column.isSortedDesc}
|
|
132
|
+
onClick={handleSortDesc}
|
|
133
|
+
>
|
|
134
|
+
<ListItemIcon>
|
|
135
|
+
<SortIcon style={{ transform: 'rotate(180deg) scaleX(-1)' }} />
|
|
136
|
+
</ListItemIcon>
|
|
137
|
+
<ListItemText>
|
|
138
|
+
{localization.columnActionMenuItemSortDesc?.replace(
|
|
139
|
+
'{column}',
|
|
140
|
+
String(column.Header),
|
|
141
|
+
)}
|
|
142
|
+
</ListItemText>
|
|
143
|
+
</MenuItem>,
|
|
144
|
+
]}
|
|
145
|
+
{!disableFilters &&
|
|
146
|
+
column.canFilter && [
|
|
147
|
+
<Divider key={0} />,
|
|
148
|
+
<MenuItem key={1} onClick={handleFilterByColumn}>
|
|
149
|
+
<ListItemIcon>
|
|
150
|
+
<FilterListIcon />
|
|
151
|
+
</ListItemIcon>
|
|
152
|
+
<ListItemText>
|
|
153
|
+
{localization.filterTextFieldPlaceholder?.replace(
|
|
154
|
+
'{column}',
|
|
155
|
+
String(column.Header),
|
|
156
|
+
)}
|
|
157
|
+
</ListItemText>
|
|
158
|
+
<IconButton size="small" onMouseEnter={handleOpenFilterModeMenu}>
|
|
159
|
+
<ArrowRightIcon />
|
|
160
|
+
</IconButton>
|
|
161
|
+
</MenuItem>,
|
|
162
|
+
<MRT_FilterMenu
|
|
163
|
+
anchorEl={filterMenuAnchorEl}
|
|
164
|
+
column={column}
|
|
165
|
+
key={2}
|
|
166
|
+
setAnchorEl={setFilterMenuAnchorEl}
|
|
167
|
+
onSelect={handleFilterByColumn}
|
|
168
|
+
/>,
|
|
169
|
+
]}
|
|
170
|
+
{enableColumnGrouping &&
|
|
171
|
+
column.canGroupBy && [
|
|
172
|
+
<Divider key={1} />,
|
|
173
|
+
<MenuItem key={2} onClick={handleGroupByColumn}>
|
|
174
|
+
<ListItemIcon>
|
|
175
|
+
<DynamicFeedIcon />
|
|
176
|
+
</ListItemIcon>
|
|
177
|
+
<ListItemText>
|
|
178
|
+
{localization[
|
|
179
|
+
column.isGrouped
|
|
180
|
+
? 'columnActionMenuItemUnGroupBy'
|
|
181
|
+
: 'columnActionMenuItemGroupBy'
|
|
182
|
+
]?.replace('{column}', String(column.Header))}
|
|
183
|
+
</ListItemText>
|
|
184
|
+
</MenuItem>,
|
|
185
|
+
]}
|
|
186
|
+
{!disableColumnHiding && [
|
|
122
187
|
<Divider key={0} />,
|
|
123
|
-
<MenuItem
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
</MenuItem>,
|
|
134
|
-
]}
|
|
135
|
-
{enableColumnGrouping &&
|
|
136
|
-
column.canGroupBy && [
|
|
137
|
-
<Divider key={1} />,
|
|
138
|
-
<MenuItem
|
|
139
|
-
key={2}
|
|
140
|
-
onClick={handleGroupByColumn}
|
|
141
|
-
sx={commonMenuItemStyles}
|
|
142
|
-
>
|
|
143
|
-
<DynamicFeedIcon />{' '}
|
|
144
|
-
{localization[
|
|
145
|
-
column.isGrouped
|
|
146
|
-
? 'columnActionMenuItemUnGroupBy'
|
|
147
|
-
: 'columnActionMenuItemGroupBy'
|
|
148
|
-
]?.replace('{column}', String(column.Header))}
|
|
188
|
+
<MenuItem key={1} onClick={handleHideColumn}>
|
|
189
|
+
<ListItemIcon>
|
|
190
|
+
<VisibilityOffIcon />
|
|
191
|
+
</ListItemIcon>
|
|
192
|
+
<ListItemText>
|
|
193
|
+
{localization.columnActionMenuItemHideColumn?.replace(
|
|
194
|
+
'{column}',
|
|
195
|
+
String(column.Header),
|
|
196
|
+
)}
|
|
197
|
+
</ListItemText>
|
|
149
198
|
</MenuItem>,
|
|
150
199
|
]}
|
|
151
|
-
|
|
152
|
-
<Divider key={0} />,
|
|
153
|
-
<MenuItem key={1} onClick={handleHideColumn} sx={commonMenuItemStyles}>
|
|
154
|
-
<VisibilityOffIcon />{' '}
|
|
155
|
-
{localization.columnActionMenuItemHideColumn?.replace(
|
|
156
|
-
'{column}',
|
|
157
|
-
String(column.Header),
|
|
158
|
-
)}
|
|
159
|
-
</MenuItem>,
|
|
160
|
-
]}
|
|
200
|
+
</MenuList>
|
|
161
201
|
</Menu>
|
|
162
202
|
);
|
|
163
203
|
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import React, { FC, useMemo } from 'react';
|
|
2
|
+
import { Divider, Menu, MenuItem, Typography } from '@mui/material';
|
|
3
|
+
import { useMRT } from '../useMRT';
|
|
4
|
+
import { MRT_FilterType, MRT_HeaderGroup } from '..';
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
anchorEl: HTMLElement | null;
|
|
8
|
+
column: MRT_HeaderGroup;
|
|
9
|
+
setAnchorEl: (anchorEl: HTMLElement | null) => void;
|
|
10
|
+
onSelect?: () => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const MRT_FilterMenu: FC<Props> = ({
|
|
14
|
+
anchorEl,
|
|
15
|
+
column,
|
|
16
|
+
onSelect,
|
|
17
|
+
setAnchorEl,
|
|
18
|
+
}) => {
|
|
19
|
+
const { localization, setCurrentFilterTypes, tableInstance } = useMRT();
|
|
20
|
+
|
|
21
|
+
const filterTypes: {
|
|
22
|
+
type: MRT_FilterType;
|
|
23
|
+
label: string;
|
|
24
|
+
divider: boolean;
|
|
25
|
+
}[] = useMemo(
|
|
26
|
+
() => [
|
|
27
|
+
{
|
|
28
|
+
type: 'fuzzy',
|
|
29
|
+
label: localization.filterMenuItemFuzzy,
|
|
30
|
+
divider: false,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
type: 'contains',
|
|
34
|
+
label: localization.filterMenuItemContains,
|
|
35
|
+
divider: true,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: 'startsWith',
|
|
39
|
+
label: localization.filterMenuItemStartsWith,
|
|
40
|
+
divider: false,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
type: 'endsWith',
|
|
44
|
+
label: localization.filterMenuItemEndsWith,
|
|
45
|
+
divider: true,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
type: 'equals',
|
|
49
|
+
label: localization.filterMenuItemEquals,
|
|
50
|
+
divider: false,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: 'notEquals',
|
|
54
|
+
label: localization.filterMenuItemNotEquals,
|
|
55
|
+
divider: true,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
type: 'empty',
|
|
59
|
+
label: localization.filterMenuItemEmpty,
|
|
60
|
+
divider: false,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
type: 'notEmpty',
|
|
64
|
+
label: localization.filterMenuItemNotEmpty,
|
|
65
|
+
divider: false,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
[],
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const handleSelectMenuItem = (value: MRT_FilterType) => {
|
|
72
|
+
setAnchorEl(null);
|
|
73
|
+
setCurrentFilterTypes((prev: { [key: string]: MRT_FilterType }) => ({
|
|
74
|
+
...prev,
|
|
75
|
+
[column.id]: value,
|
|
76
|
+
}));
|
|
77
|
+
if (['empty', 'notEmpty'].includes(value)) {
|
|
78
|
+
column.setFilter(' ');
|
|
79
|
+
}
|
|
80
|
+
onSelect?.();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const filterType = tableInstance.state.currentFilterTypes[column.id];
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<Menu
|
|
87
|
+
anchorEl={anchorEl}
|
|
88
|
+
open={!!anchorEl}
|
|
89
|
+
anchorOrigin={{ vertical: 'center', horizontal: 'right' }}
|
|
90
|
+
onClose={() => setAnchorEl(null)}
|
|
91
|
+
>
|
|
92
|
+
<Typography sx={{ fontWeight: 'bold', p: '1rem', fontSize: '1.1rem' }}>
|
|
93
|
+
{localization.filterMenuTitle}
|
|
94
|
+
</Typography>
|
|
95
|
+
<Divider />
|
|
96
|
+
{filterTypes.map(({ type, label, divider }) => (
|
|
97
|
+
<MenuItem
|
|
98
|
+
divider={divider}
|
|
99
|
+
key={type}
|
|
100
|
+
onClick={() => handleSelectMenuItem(type)}
|
|
101
|
+
selected={type === filterType}
|
|
102
|
+
value={type}
|
|
103
|
+
>
|
|
104
|
+
{label}
|
|
105
|
+
</MenuItem>
|
|
106
|
+
))}
|
|
107
|
+
</Menu>
|
|
108
|
+
);
|
|
109
|
+
};
|
package/src/useMRT.tsx
CHANGED
|
@@ -5,6 +5,8 @@ import React, {
|
|
|
5
5
|
useContext,
|
|
6
6
|
useMemo,
|
|
7
7
|
useState,
|
|
8
|
+
Dispatch,
|
|
9
|
+
SetStateAction,
|
|
8
10
|
} from 'react';
|
|
9
11
|
import {
|
|
10
12
|
PluginHook,
|
|
@@ -19,7 +21,8 @@ import {
|
|
|
19
21
|
useSortBy,
|
|
20
22
|
useTable,
|
|
21
23
|
} from 'react-table';
|
|
22
|
-
import { MRT_Row, MRT_TableInstance } from '.';
|
|
24
|
+
import { MRT_FilterType, MRT_Row, MRT_TableInstance } from '.';
|
|
25
|
+
import { defaultFilterFNs } from './filtersFNs';
|
|
23
26
|
import { MRT_Icons } from './icons';
|
|
24
27
|
import { MRT_Localization } from './localization';
|
|
25
28
|
import { MaterialReactTableProps } from './MaterialReactTable';
|
|
@@ -28,19 +31,25 @@ export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
|
|
|
28
31
|
anyRowsCanExpand: boolean;
|
|
29
32
|
anyRowsExpanded: boolean;
|
|
30
33
|
icons: MRT_Icons;
|
|
34
|
+
idPrefix: string;
|
|
31
35
|
localization: MRT_Localization;
|
|
32
|
-
setCurrentEditingRow:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
setCurrentEditingRow: Dispatch<SetStateAction<MRT_Row<D> | null>>;
|
|
37
|
+
setCurrentFilterTypes: Dispatch<
|
|
38
|
+
SetStateAction<{
|
|
39
|
+
[key: string]: MRT_FilterType;
|
|
40
|
+
}>
|
|
41
|
+
>;
|
|
42
|
+
setDensePadding: Dispatch<SetStateAction<boolean>>;
|
|
43
|
+
setFullScreen: Dispatch<SetStateAction<boolean>>;
|
|
44
|
+
setShowFilters: Dispatch<SetStateAction<boolean>>;
|
|
45
|
+
setShowSearch: Dispatch<SetStateAction<boolean>>;
|
|
37
46
|
tableInstance: MRT_TableInstance<D>;
|
|
38
47
|
};
|
|
39
48
|
|
|
40
|
-
const MaterialReactTableContext = (<D extends {}>() =>
|
|
49
|
+
const MaterialReactTableContext = (<D extends {} = {}>() =>
|
|
41
50
|
createContext<UseMRT<D>>({} as UseMRT<D>) as Context<UseMRT<D>>)();
|
|
42
51
|
|
|
43
|
-
export const MaterialReactTableProvider = <D extends {}>(
|
|
52
|
+
export const MaterialReactTableProvider = <D extends {} = {}>(
|
|
44
53
|
props: PropsWithChildren<MaterialReactTableProps<D>>,
|
|
45
54
|
) => {
|
|
46
55
|
const hooks: PluginHook<D>[] = [
|
|
@@ -56,7 +65,7 @@ export const MaterialReactTableProvider = <D extends {}>(
|
|
|
56
65
|
if (props.enableColumnResizing)
|
|
57
66
|
hooks.unshift(useResizeColumns, useFlexLayout);
|
|
58
67
|
|
|
59
|
-
const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row | null>(
|
|
68
|
+
const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row<D> | null>(
|
|
60
69
|
null,
|
|
61
70
|
);
|
|
62
71
|
const [densePadding, setDensePadding] = useState(
|
|
@@ -72,14 +81,50 @@ export const MaterialReactTableProvider = <D extends {}>(
|
|
|
72
81
|
props.initialState?.showSearch ?? false,
|
|
73
82
|
);
|
|
74
83
|
|
|
75
|
-
const
|
|
84
|
+
const filterTypes = useMemo<Partial<{ [key in MRT_FilterType]: any }>>(
|
|
85
|
+
() => ({
|
|
86
|
+
...defaultFilterFNs,
|
|
87
|
+
...props.filterTypes,
|
|
88
|
+
}),
|
|
89
|
+
[props.filterTypes],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const [currentFilterTypes, setCurrentFilterTypes] = useState<{
|
|
93
|
+
[key: string]: MRT_FilterType;
|
|
94
|
+
}>(() =>
|
|
95
|
+
Object.assign(
|
|
96
|
+
{},
|
|
97
|
+
...props.columns
|
|
98
|
+
.map((c) => c.accessor?.toString() as string)
|
|
99
|
+
.map((accessor) => ({
|
|
100
|
+
[accessor]:
|
|
101
|
+
props?.initialState?.filters?.[accessor as any] ?? 'fuzzy',
|
|
102
|
+
})),
|
|
103
|
+
),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const columns = useMemo(
|
|
107
|
+
() =>
|
|
108
|
+
props.columns.map((column) => {
|
|
109
|
+
column.filter =
|
|
110
|
+
filterTypes[currentFilterTypes[column.accessor as string]];
|
|
111
|
+
return column;
|
|
112
|
+
}),
|
|
113
|
+
[props.columns, filterTypes, currentFilterTypes],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const tableInstance = useTable(
|
|
76
117
|
{
|
|
77
118
|
...props,
|
|
119
|
+
columns,
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
filterTypes,
|
|
78
122
|
useControlledState: (state) =>
|
|
79
123
|
useMemo(
|
|
80
124
|
() => ({
|
|
81
125
|
...state,
|
|
82
126
|
currentEditingRow,
|
|
127
|
+
currentFilterTypes,
|
|
83
128
|
densePadding,
|
|
84
129
|
fullScreen,
|
|
85
130
|
showFilters,
|
|
@@ -89,6 +134,7 @@ export const MaterialReactTableProvider = <D extends {}>(
|
|
|
89
134
|
}),
|
|
90
135
|
[
|
|
91
136
|
currentEditingRow,
|
|
137
|
+
currentFilterTypes,
|
|
92
138
|
densePadding,
|
|
93
139
|
fullScreen,
|
|
94
140
|
showFilters,
|
|
@@ -100,6 +146,10 @@ export const MaterialReactTableProvider = <D extends {}>(
|
|
|
100
146
|
...hooks,
|
|
101
147
|
) as MRT_TableInstance<D>;
|
|
102
148
|
|
|
149
|
+
const idPrefix = useMemo(
|
|
150
|
+
() => props.idPrefix ?? Math.random().toString(36).substring(2, 9),
|
|
151
|
+
[props.idPrefix],
|
|
152
|
+
);
|
|
103
153
|
const anyRowsCanExpand = useMemo(
|
|
104
154
|
() => tableInstance.rows.some((row) => row.canExpand),
|
|
105
155
|
[tableInstance.rows],
|
|
@@ -115,7 +165,10 @@ export const MaterialReactTableProvider = <D extends {}>(
|
|
|
115
165
|
...props,
|
|
116
166
|
anyRowsCanExpand,
|
|
117
167
|
anyRowsExpanded,
|
|
168
|
+
idPrefix,
|
|
169
|
+
//@ts-ignore
|
|
118
170
|
setCurrentEditingRow,
|
|
171
|
+
setCurrentFilterTypes,
|
|
119
172
|
setDensePadding,
|
|
120
173
|
setFullScreen,
|
|
121
174
|
setShowFilters,
|
|
@@ -129,6 +182,7 @@ export const MaterialReactTableProvider = <D extends {}>(
|
|
|
129
182
|
);
|
|
130
183
|
};
|
|
131
184
|
|
|
132
|
-
export const useMRT = <D extends {}>(): UseMRT<D> =>
|
|
133
|
-
|
|
134
|
-
|
|
185
|
+
export const useMRT = <D extends {} = {}>(): UseMRT<D> =>
|
|
186
|
+
useContext<UseMRT<D>>(
|
|
187
|
+
MaterialReactTableContext as unknown as Context<UseMRT<D>>,
|
|
188
|
+
);
|