material-react-table 0.5.1 → 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.
Files changed (45) hide show
  1. package/dist/MaterialReactTable.d.ts +15 -9
  2. package/dist/filtersFNs.d.ts +67 -0
  3. package/dist/localization.d.ts +10 -1
  4. package/dist/material-react-table.cjs.development.js +455 -147
  5. package/dist/material-react-table.cjs.development.js.map +1 -1
  6. package/dist/material-react-table.cjs.production.min.js +1 -1
  7. package/dist/material-react-table.cjs.production.min.js.map +1 -1
  8. package/dist/material-react-table.esm.js +457 -149
  9. package/dist/material-react-table.esm.js.map +1 -1
  10. package/dist/menus/MRT_FilterMenu.d.ts +10 -0
  11. package/dist/useMRT.d.ts +13 -14
  12. package/package.json +6 -2
  13. package/src/@types/react-table-config.d.ts +3 -3
  14. package/src/MaterialReactTable.tsx +30 -9
  15. package/src/body/MRT_TableBody.tsx +7 -2
  16. package/src/body/MRT_TableBodyCell.tsx +3 -2
  17. package/src/body/MRT_TableBodyRow.tsx +11 -8
  18. package/src/buttons/MRT_EditActionButtons.tsx +4 -2
  19. package/src/buttons/MRT_ExpandAllButton.tsx +3 -4
  20. package/src/buttons/MRT_ExpandButton.tsx +3 -1
  21. package/src/buttons/MRT_FullScreenToggleButton.tsx +3 -1
  22. package/src/buttons/MRT_ToggleColumnActionMenuButton.tsx +4 -3
  23. package/src/buttons/MRT_ToggleDensePaddingButton.tsx +3 -1
  24. package/src/buttons/MRT_ToggleFiltersButton.tsx +4 -2
  25. package/src/buttons/MRT_ToggleRowActionMenuButton.tsx +17 -11
  26. package/src/buttons/MRT_ToggleSearchButton.tsx +5 -2
  27. package/src/filtersFNs.ts +112 -0
  28. package/src/footer/MRT_TableFooter.tsx +6 -1
  29. package/src/footer/MRT_TableFooterCell.tsx +7 -2
  30. package/src/head/MRT_TableHeadCell.tsx +49 -47
  31. package/src/head/MRT_TableHeadCellActions.tsx +6 -1
  32. package/src/head/MRT_TableHeadRow.tsx +7 -2
  33. package/src/index.tsx +0 -2
  34. package/src/inputs/MRT_EditCellTextField.tsx +3 -1
  35. package/src/inputs/MRT_FilterTextField.tsx +117 -52
  36. package/src/inputs/MRT_SearchTextField.tsx +3 -3
  37. package/src/inputs/MRT_SelectCheckbox.tsx +5 -8
  38. package/src/localization.ts +20 -2
  39. package/src/menus/MRT_ColumnActionMenu.tsx +125 -85
  40. package/src/menus/MRT_FilterMenu.tsx +109 -0
  41. package/src/table/MRT_Table.tsx +7 -2
  42. package/src/table/MRT_TableContainer.tsx +16 -3
  43. package/src/toolbar/MRT_ToolbarBottom.tsx +2 -3
  44. package/src/toolbar/MRT_ToolbarTop.tsx +2 -3
  45. package/src/useMRT.tsx +104 -35
@@ -1,12 +1,17 @@
1
- import React, { FC } from 'react';
2
- import { Divider, Menu, MenuItem } from '@mui/material';
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
- const commonMenuItemStyles = {
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
- `filter-${column.id}-column`,
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
- {!disableSortBy &&
86
- column.canSort && [
87
- <MenuItem
88
- key={1}
89
- disabled={!column.isSorted}
90
- onClick={handleClearSort}
91
- sx={commonMenuItemStyles}
92
- >
93
- <ClearAllIcon /> {localization.columnActionMenuItemClearSort}
94
- </MenuItem>,
95
- <MenuItem
96
- key={2}
97
- disabled={column.isSorted && !column.isSortedDesc}
98
- onClick={handleSortAsc}
99
- sx={commonMenuItemStyles}
100
- >
101
- <SortIcon />{' '}
102
- {localization.columnActionMenuItemSortAsc?.replace(
103
- '{column}',
104
- String(column.Header),
105
- )}
106
- </MenuItem>,
107
- <MenuItem
108
- key={3}
109
- disabled={column.isSorted && column.isSortedDesc}
110
- onClick={handleSortDesc}
111
- sx={commonMenuItemStyles}
112
- >
113
- <SortIcon style={{ transform: 'rotate(180deg) scaleX(-1)' }} />{' '}
114
- {localization.columnActionMenuItemSortDesc?.replace(
115
- '{column}',
116
- String(column.Header),
117
- )}
118
- </MenuItem>,
119
- ]}
120
- {!disableFilters &&
121
- column.canFilter && [
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
- key={1}
125
- onClick={handleFilterByColumn}
126
- sx={commonMenuItemStyles}
127
- >
128
- <FilterListIcon />{' '}
129
- {localization.filterTextFieldPlaceholder?.replace(
130
- '{column}',
131
- String(column.Header),
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
- {!disableColumnHiding && [
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
+ };
@@ -11,12 +11,17 @@ export const MRT_Table: FC<Props> = () => {
11
11
  const { tableInstance, muiTableProps, hideTableHead, hideTableFooter } =
12
12
  useMRT();
13
13
 
14
+ const mTableProps =
15
+ muiTableProps instanceof Function
16
+ ? muiTableProps(tableInstance)
17
+ : muiTableProps;
18
+
14
19
  const tableProps = {
15
- ...muiTableProps,
20
+ ...mTableProps,
16
21
  ...tableInstance.getTableProps(),
17
22
  style: {
18
23
  ...tableInstance.getTableProps().style,
19
- ...muiTableProps?.style,
24
+ ...mTableProps?.style,
20
25
  },
21
26
  };
22
27
 
@@ -1,5 +1,11 @@
1
1
  import React, { FC, useEffect, useRef } from 'react';
2
- import { LinearProgress, Paper, TableContainer, Collapse } from '@mui/material';
2
+ import {
3
+ LinearProgress,
4
+ Paper,
5
+ TableContainer,
6
+ Collapse,
7
+ Box,
8
+ } from '@mui/material';
3
9
  import { useMRT } from '../useMRT';
4
10
  import { MRT_Table } from './MRT_Table';
5
11
  import { MRT_ToolbarTop } from '../toolbar/MRT_ToolbarTop';
@@ -9,7 +15,6 @@ interface Props {}
9
15
 
10
16
  export const MRT_TableContainer: FC<Props> = () => {
11
17
  const {
12
- fullScreen,
13
18
  hideToolbarBottom,
14
19
  hideToolbarTop,
15
20
  isFetching,
@@ -17,6 +22,7 @@ export const MRT_TableContainer: FC<Props> = () => {
17
22
  muiTableContainerProps,
18
23
  tableInstance,
19
24
  } = useMRT();
25
+ const fullScreen = tableInstance.state.fullScreen;
20
26
  const originalBodyOverflowStyle = useRef<string | undefined>();
21
27
 
22
28
  useEffect(() => {
@@ -63,7 +69,14 @@ export const MRT_TableContainer: FC<Props> = () => {
63
69
  <Collapse in={isFetching || isLoading} unmountOnExit>
64
70
  <LinearProgress />
65
71
  </Collapse>
66
- <MRT_Table />
72
+ <Box
73
+ sx={{
74
+ maxWidth: '100%',
75
+ overflowX: 'auto',
76
+ }}
77
+ >
78
+ <MRT_Table />
79
+ </Box>
67
80
  {!hideToolbarBottom && <MRT_ToolbarBottom />}
68
81
  </TableContainer>
69
82
  );
@@ -13,7 +13,6 @@ export const MRT_ToolbarBottom: FC<Props> = () => {
13
13
  manualPagination,
14
14
  muiTableToolbarBottomProps,
15
15
  positionPagination,
16
- fullScreen,
17
16
  positionToolbarActions,
18
17
  positionToolbarAlertBanner,
19
18
  tableInstance,
@@ -35,12 +34,12 @@ export const MRT_ToolbarBottom: FC<Props> = () => {
35
34
  theme.palette.common.white,
36
35
  0.05,
37
36
  )},${alpha(theme.palette.common.white, 0.05)})`,
38
- bottom: fullScreen ? '0' : undefined,
37
+ bottom: tableInstance.state.fullScreen ? '0' : undefined,
39
38
  display: 'flex',
40
39
  justifyContent: 'space-between',
41
40
  overflowY: 'hidden',
42
41
  p: '0 0.5rem !important',
43
- position: fullScreen ? 'fixed' : undefined,
42
+ position: tableInstance.state.fullScreen ? 'fixed' : undefined,
44
43
  width: 'calc(100% - 1rem)',
45
44
  zIndex: 1,
46
45
  ...toolbarProps?.sx,
@@ -16,7 +16,6 @@ export const MRT_ToolbarTop: FC<Props> = () => {
16
16
  muiTableToolbarTopProps,
17
17
  positionPagination,
18
18
  positionToolbarActions,
19
- fullScreen,
20
19
  positionToolbarAlertBanner,
21
20
  renderToolbarCustomActions,
22
21
  tableInstance,
@@ -40,8 +39,8 @@ export const MRT_ToolbarTop: FC<Props> = () => {
40
39
  )},${alpha(theme.palette.common.white, 0.05)})`,
41
40
  display: 'grid',
42
41
  p: '0 !important',
43
- position: fullScreen ? 'sticky' : undefined,
44
- top: fullScreen ? '0' : undefined,
42
+ position: tableInstance.state.fullScreen ? 'sticky' : undefined,
43
+ top: tableInstance.state.fullScreen ? '0' : undefined,
45
44
  width: '100%',
46
45
  zIndex: 1,
47
46
  ...toolbarProps?.sx,
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';
@@ -27,25 +30,26 @@ import { MaterialReactTableProps } from './MaterialReactTable';
27
30
  export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
28
31
  anyRowsCanExpand: boolean;
29
32
  anyRowsExpanded: boolean;
30
- currentEditingRow: MRT_Row<D> | null;
31
- densePadding: boolean;
32
- fullScreen: boolean;
33
33
  icons: MRT_Icons;
34
+ idPrefix: string;
34
35
  localization: MRT_Localization;
35
- setCurrentEditingRow: (currentRowEditingId: MRT_Row<D> | null) => void;
36
- setDensePadding: (densePadding: boolean) => void;
37
- setFullScreen: (fullScreen: boolean) => void;
38
- setShowFilters: (showFilters: boolean) => void;
39
- setShowSearch: (showSearch: boolean) => void;
40
- showFilters: boolean;
41
- showSearch: boolean;
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>>;
42
46
  tableInstance: MRT_TableInstance<D>;
43
47
  };
44
48
 
45
- const MaterialReactTableContext = (<D extends {}>() =>
49
+ const MaterialReactTableContext = (<D extends {} = {}>() =>
46
50
  createContext<UseMRT<D>>({} as UseMRT<D>) as Context<UseMRT<D>>)();
47
51
 
48
- export const MaterialReactTableProvider = <D extends {}>(
52
+ export const MaterialReactTableProvider = <D extends {} = {}>(
49
53
  props: PropsWithChildren<MaterialReactTableProps<D>>,
50
54
  ) => {
51
55
  const hooks: PluginHook<D>[] = [
@@ -61,19 +65,7 @@ export const MaterialReactTableProvider = <D extends {}>(
61
65
  if (props.enableColumnResizing)
62
66
  hooks.unshift(useResizeColumns, useFlexLayout);
63
67
 
64
- const tableInstance = useTable<D>(props, ...hooks) as MRT_TableInstance<D>;
65
-
66
- const anyRowsCanExpand = useMemo(
67
- // @ts-ignore
68
- () => tableInstance.rows.some((row: MRT_Row) => row.canExpand),
69
- [tableInstance.rows],
70
- );
71
- const anyRowsExpanded = useMemo(
72
- // @ts-ignore
73
- () => tableInstance.rows.some((row: MRT_Row) => row.isExpanded),
74
- [tableInstance.rows],
75
- );
76
- const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row | null>(
68
+ const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row<D> | null>(
77
69
  null,
78
70
  );
79
71
  const [densePadding, setDensePadding] = useState(
@@ -86,7 +78,85 @@ export const MaterialReactTableProvider = <D extends {}>(
86
78
  props.initialState?.showFilters ?? false,
87
79
  );
88
80
  const [showSearch, setShowSearch] = useState(
89
- props.initialState?.showSearchTextField ?? false,
81
+ props.initialState?.showSearch ?? false,
82
+ );
83
+
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(
117
+ {
118
+ ...props,
119
+ columns,
120
+ // @ts-ignore
121
+ filterTypes,
122
+ useControlledState: (state) =>
123
+ useMemo(
124
+ () => ({
125
+ ...state,
126
+ currentEditingRow,
127
+ currentFilterTypes,
128
+ densePadding,
129
+ fullScreen,
130
+ showFilters,
131
+ showSearch,
132
+ //@ts-ignore
133
+ ...props?.useControlledState?.(state),
134
+ }),
135
+ [
136
+ currentEditingRow,
137
+ currentFilterTypes,
138
+ densePadding,
139
+ fullScreen,
140
+ showFilters,
141
+ showSearch,
142
+ state,
143
+ ],
144
+ ),
145
+ },
146
+ ...hooks,
147
+ ) as MRT_TableInstance<D>;
148
+
149
+ const idPrefix = useMemo(
150
+ () => props.idPrefix ?? Math.random().toString(36).substring(2, 9),
151
+ [props.idPrefix],
152
+ );
153
+ const anyRowsCanExpand = useMemo(
154
+ () => tableInstance.rows.some((row) => row.canExpand),
155
+ [tableInstance.rows],
156
+ );
157
+ const anyRowsExpanded = useMemo(
158
+ () => tableInstance.rows.some((row) => row.isExpanded),
159
+ [tableInstance.rows],
90
160
  );
91
161
 
92
162
  return (
@@ -95,16 +165,14 @@ export const MaterialReactTableProvider = <D extends {}>(
95
165
  ...props,
96
166
  anyRowsCanExpand,
97
167
  anyRowsExpanded,
98
- currentEditingRow,
99
- densePadding,
168
+ idPrefix,
169
+ //@ts-ignore
100
170
  setCurrentEditingRow,
171
+ setCurrentFilterTypes,
101
172
  setDensePadding,
102
- fullScreen,
103
173
  setFullScreen,
104
174
  setShowFilters,
105
175
  setShowSearch,
106
- showFilters,
107
- showSearch,
108
176
  //@ts-ignore
109
177
  tableInstance,
110
178
  }}
@@ -114,6 +182,7 @@ export const MaterialReactTableProvider = <D extends {}>(
114
182
  );
115
183
  };
116
184
 
117
- export const useMRT = <D extends {}>(): UseMRT<D> =>
118
- // @ts-ignore
119
- useContext<UseMRT<D>>(MaterialReactTableContext);
185
+ export const useMRT = <D extends {} = {}>(): UseMRT<D> =>
186
+ useContext<UseMRT<D>>(
187
+ MaterialReactTableContext as unknown as Context<UseMRT<D>>,
188
+ );