material-react-table 0.6.9 → 0.6.10

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.
@@ -4,11 +4,12 @@ import { useMRT } from '../useMRT';
4
4
  import type { MRT_FilterType, MRT_HeaderGroup } from '..';
5
5
  import { MRT_FILTER_TYPE } from '../enums';
6
6
  import {
7
+ bestMatch,
8
+ bestMatchFirst,
7
9
  contains,
8
10
  empty,
9
11
  endsWith,
10
12
  equals,
11
- fuzzy,
12
13
  greaterThan,
13
14
  lessThan,
14
15
  notEmpty,
@@ -24,7 +25,7 @@ const commonMenuItemStyles = {
24
25
 
25
26
  interface Props {
26
27
  anchorEl: HTMLElement | null;
27
- column: MRT_HeaderGroup;
28
+ column?: MRT_HeaderGroup;
28
29
  setAnchorEl: (anchorEl: HTMLElement | null) => void;
29
30
  onSelect?: () => void;
30
31
  }
@@ -35,7 +36,13 @@ export const MRT_FilterTypeMenu: FC<Props> = ({
35
36
  onSelect,
36
37
  setAnchorEl,
37
38
  }) => {
38
- const { localization, setCurrentFilterTypes, tableInstance } = useMRT();
39
+ const {
40
+ enabledGlobalFilterTypes,
41
+ localization,
42
+ setCurrentFilterTypes,
43
+ setCurrentGlobalFilterType,
44
+ tableInstance,
45
+ } = useMRT();
39
46
 
40
47
  const filterTypes: {
41
48
  type: MRT_FILTER_TYPE;
@@ -46,15 +53,21 @@ export const MRT_FilterTypeMenu: FC<Props> = ({
46
53
  () =>
47
54
  [
48
55
  {
49
- type: MRT_FILTER_TYPE.FUZZY,
50
- label: localization.filterFuzzy,
56
+ type: MRT_FILTER_TYPE.BEST_MATCH_FIRST,
57
+ label: localization.filterBestMatchFirst,
51
58
  divider: false,
52
- fn: fuzzy,
59
+ fn: bestMatchFirst,
60
+ },
61
+ {
62
+ type: MRT_FILTER_TYPE.BEST_MATCH,
63
+ label: localization.filterBestMatch,
64
+ divider: !!column,
65
+ fn: bestMatch,
53
66
  },
54
67
  {
55
68
  type: MRT_FILTER_TYPE.CONTAINS,
56
69
  label: localization.filterContains,
57
- divider: true,
70
+ divider: false,
58
71
  fn: contains,
59
72
  },
60
73
  {
@@ -105,26 +118,39 @@ export const MRT_FilterTypeMenu: FC<Props> = ({
105
118
  divider: false,
106
119
  fn: notEmpty,
107
120
  },
108
- ].filter(
109
- (filterType) =>
110
- !column.filterTypes || column.filterTypes.includes(filterType.type),
121
+ ].filter((filterType) =>
122
+ column
123
+ ? !column.enabledFilterTypes ||
124
+ column.enabledFilterTypes.includes(filterType.type)
125
+ : (!enabledGlobalFilterTypes ||
126
+ enabledGlobalFilterTypes.includes(filterType.type)) &&
127
+ [
128
+ MRT_FILTER_TYPE.BEST_MATCH_FIRST,
129
+ MRT_FILTER_TYPE.BEST_MATCH,
130
+ ].includes(filterType.type),
111
131
  ),
112
132
  [],
113
133
  );
114
134
 
115
135
  const handleSelectFilterType = (value: MRT_FILTER_TYPE) => {
116
- setAnchorEl(null);
117
- setCurrentFilterTypes((prev: { [key: string]: MRT_FilterType }) => ({
118
- ...prev,
119
- [column.id]: value,
120
- }));
121
- if ([MRT_FILTER_TYPE.EMPTY, MRT_FILTER_TYPE.NOT_EMPTY].includes(value)) {
122
- column.setFilter(' ');
136
+ if (column) {
137
+ setCurrentFilterTypes((prev: { [key: string]: MRT_FilterType }) => ({
138
+ ...prev,
139
+ [column.id]: value,
140
+ }));
141
+ if ([MRT_FILTER_TYPE.EMPTY, MRT_FILTER_TYPE.NOT_EMPTY].includes(value)) {
142
+ column.setFilter(' ');
143
+ }
144
+ } else {
145
+ setCurrentGlobalFilterType(value);
123
146
  }
147
+ setAnchorEl(null);
124
148
  onSelect?.();
125
149
  };
126
150
 
127
- const filterType = tableInstance.state.currentFilterTypes[column.id];
151
+ const filterType = column
152
+ ? tableInstance.state.currentFilterTypes[column.id]
153
+ : tableInstance.state.currentGlobalFilterType;
128
154
 
129
155
  return (
130
156
  <Menu
package/src/useMRT.tsx CHANGED
@@ -47,6 +47,7 @@ export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
47
47
  [key: string]: MRT_FilterType;
48
48
  }>
49
49
  >;
50
+ setCurrentGlobalFilterType: Dispatch<SetStateAction<MRT_FILTER_TYPE>>;
50
51
  setDensePadding: Dispatch<SetStateAction<boolean>>;
51
52
  setFullScreen: Dispatch<SetStateAction<boolean>>;
52
53
  setShowFilters: Dispatch<SetStateAction<boolean>>;
@@ -98,11 +99,17 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
98
99
  [c.accessor as string]:
99
100
  c.filter ??
100
101
  props?.initialState?.filters?.[c.accessor as any] ??
101
- (!!c.filterSelectOptions ? 'equals' : 'fuzzy'),
102
+ (!!c.filterSelectOptions?.length
103
+ ? MRT_FILTER_TYPE.EQUALS
104
+ : MRT_FILTER_TYPE.BEST_MATCH),
102
105
  })),
103
106
  ),
104
107
  );
105
108
 
109
+ const [currentGlobalFilterType, setCurrentGlobalFilterType] = useState<
110
+ MRT_FilterType | string | undefined
111
+ >(props.globalFilter);
112
+
106
113
  const applyFiltersToColumns = useCallback(
107
114
  (cols: MRT_ColumnInterface[]) =>
108
115
  cols.map((column) => {
@@ -146,12 +153,14 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
146
153
  // @ts-ignore
147
154
  columns,
148
155
  data,
156
+ globalFilter: currentGlobalFilterType,
149
157
  useControlledState: (state) =>
150
158
  useMemo(
151
159
  () => ({
152
160
  ...state,
153
161
  currentEditingRow,
154
162
  currentFilterTypes,
163
+ currentGlobalFilterType,
155
164
  densePadding,
156
165
  fullScreen,
157
166
  showFilters,
@@ -162,6 +171,7 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
162
171
  [
163
172
  currentEditingRow,
164
173
  currentFilterTypes,
174
+ currentGlobalFilterType,
165
175
  densePadding,
166
176
  fullScreen,
167
177
  showFilters,
@@ -196,6 +206,7 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
196
206
  //@ts-ignore
197
207
  setCurrentEditingRow,
198
208
  setCurrentFilterTypes,
209
+ setCurrentGlobalFilterType,
199
210
  setDensePadding,
200
211
  setFullScreen,
201
212
  setShowFilters,