material-react-table 0.38.4 → 0.40.1

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.
@@ -1,16 +1,16 @@
1
1
  import React, { useMemo } from 'react';
2
2
  import { Box, Menu, MenuItem } from '@mui/material';
3
- import type { MRT_FilterOption, MRT_Header, MRT_TableInstance } from '..';
3
+ import type {
4
+ MRT_FilterOption,
5
+ MRT_Header,
6
+ MRT_InternalFilterOption,
7
+ MRT_TableInstance,
8
+ } from '..';
4
9
  import { MRT_Localization } from '../localization';
5
10
 
6
- export const internalFilterOptions = (
11
+ export const mrtFilterOptions = (
7
12
  localization: MRT_Localization,
8
- ): {
9
- option: string;
10
- symbol: string;
11
- label: string;
12
- divider: boolean;
13
- }[] => [
13
+ ): MRT_InternalFilterOption[] => [
14
14
  {
15
15
  option: 'fuzzy',
16
16
  symbol: '≈',
@@ -118,31 +118,33 @@ export const MRT_FilterOptionMenu = <TData extends Record<string, any> = {}>({
118
118
  enabledGlobalFilterOptions,
119
119
  columnFilterModeOptions,
120
120
  localization,
121
+ renderColumnFilterModeMenuItems,
122
+ renderGlobalFilterModeMenuItems,
121
123
  },
122
124
  setColumnFilterFns,
123
125
  setGlobalFilterFn,
124
126
  } = table;
125
- const { columnFilterFns, globalFilterFn, density } = getState();
127
+ const { globalFilterFn, density } = getState();
126
128
  const { column } = header ?? {};
127
129
  const { columnDef } = column ?? {};
128
130
 
129
131
  const allowedColumnFilterOptions =
130
132
  columnDef?.columnFilterModeOptions ?? columnFilterModeOptions;
131
133
 
132
- const filterOptions = useMemo(
134
+ const internalFilterOptions = useMemo(
133
135
  () =>
134
- internalFilterOptions(localization).filter((filterOption) =>
136
+ mrtFilterOptions(localization).filter((filterOption) =>
135
137
  columnDef
136
138
  ? allowedColumnFilterOptions === undefined ||
137
139
  allowedColumnFilterOptions?.includes(filterOption.option)
138
140
  : (!enabledGlobalFilterOptions ||
139
141
  enabledGlobalFilterOptions.includes(filterOption.option)) &&
140
- ['fuzzy', 'contains'].includes(filterOption.option),
142
+ ['fuzzy', 'contains', 'startsWith'].includes(filterOption.option),
141
143
  ),
142
144
  [],
143
145
  );
144
146
 
145
- const handleSelectFilterType = (option: MRT_FilterOption) => {
147
+ const handleSelectFilterMode = (option: MRT_FilterOption) => {
146
148
  if (header && column) {
147
149
  setColumnFilterFns((prev: { [key: string]: any }) => ({
148
150
  ...prev,
@@ -162,7 +164,8 @@ export const MRT_FilterOptionMenu = <TData extends Record<string, any> = {}>({
162
164
  onSelect?.();
163
165
  };
164
166
 
165
- const filterOption = !!header ? columnFilterFns[header.id] : globalFilterFn;
167
+ const filterOption =
168
+ !!header && columnDef ? columnDef._filterFn : globalFilterFn;
166
169
 
167
170
  return (
168
171
  <Menu
@@ -174,25 +177,39 @@ export const MRT_FilterOptionMenu = <TData extends Record<string, any> = {}>({
174
177
  dense: density === 'compact',
175
178
  }}
176
179
  >
177
- {filterOptions.map(({ option, label, divider, symbol }, index) => (
178
- <MenuItem
179
- divider={divider}
180
- key={index}
181
- onClick={() => handleSelectFilterType(option as MRT_FilterOption)}
182
- selected={option === filterOption}
183
- sx={{
184
- py: '6px',
185
- my: 0,
186
- alignItems: 'center',
187
- display: 'flex',
188
- gap: '2ch',
189
- }}
190
- value={option}
191
- >
192
- <Box sx={{ fontSize: '1.25rem', width: '2ch' }}>{symbol}</Box>
193
- {label}
194
- </MenuItem>
195
- ))}
180
+ {(header && column
181
+ ? renderColumnFilterModeMenuItems?.({
182
+ column: column as any,
183
+ internalFilterOptions,
184
+ onSelectFilterMode: handleSelectFilterMode,
185
+ table,
186
+ })
187
+ : renderGlobalFilterModeMenuItems?.({
188
+ internalFilterOptions,
189
+ onSelectFilterMode: handleSelectFilterMode,
190
+ table,
191
+ })) ??
192
+ internalFilterOptions.map(
193
+ ({ option, label, divider, symbol }, index) => (
194
+ <MenuItem
195
+ divider={divider}
196
+ key={index}
197
+ onClick={() => handleSelectFilterMode(option as MRT_FilterOption)}
198
+ selected={option === filterOption}
199
+ sx={{
200
+ alignItems: 'center',
201
+ display: 'flex',
202
+ gap: '2ch',
203
+ my: 0,
204
+ py: '6px',
205
+ }}
206
+ value={option}
207
+ >
208
+ <Box sx={{ fontSize: '1.25rem', width: '2ch' }}>{symbol}</Box>
209
+ {label}
210
+ </MenuItem>
211
+ ),
212
+ )}
196
213
  </Menu>
197
214
  );
198
215
  };
@@ -84,7 +84,7 @@ export const MRT_ShowHideColumnsMenuItems = <
84
84
  };
85
85
 
86
86
  const handleDragEnter = (_e: DragEvent) => {
87
- if (!isDragging) {
87
+ if (!isDragging && columnDef.enableColumnOrdering !== false) {
88
88
  setHoveredColumn(column);
89
89
  }
90
90
  };
@@ -1,4 +1,4 @@
1
- import React, { useMemo, useRef, useState } from 'react';
1
+ import React, { useEffect, useMemo, useRef, useState } from 'react';
2
2
  import {
3
3
  TableState,
4
4
  getCoreRowModel,
@@ -24,6 +24,7 @@ import {
24
24
  getDefaultColumnFilterFn,
25
25
  defaultDisplayColumnDefOptions,
26
26
  showExpandColumn,
27
+ getColumnId,
27
28
  } from '../column.utils';
28
29
  import type {
29
30
  MRT_Cell,
@@ -65,13 +66,11 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
65
66
  {},
66
67
  ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<TData>[]).map(
67
68
  (col) => ({
68
- [col.id?.toString() ?? col.accessorKey?.toString() ?? '']:
69
+ [getColumnId(col)]:
69
70
  col.filterFn instanceof Function
70
71
  ? col.filterFn.name ?? 'custom'
71
72
  : col.filterFn ??
72
- initialState?.columnFilterFns?.[
73
- col.id?.toString() ?? col.accessorKey?.toString() ?? ''
74
- ] ??
73
+ initialState?.columnFilterFns?.[getColumnId(col)] ??
75
74
  getDefaultColumnFilterFn(col),
76
75
  }),
77
76
  ),
@@ -135,7 +134,7 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
135
134
  <MRT_ToggleRowActionMenuButton
136
135
  cell={cell as any}
137
136
  row={row as any}
138
- table={table}
137
+ table={table as any}
139
138
  />
140
139
  ),
141
140
  header: props.localization?.actions,
@@ -147,11 +146,11 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
147
146
  columnOrder.includes('mrt-row-expand') &&
148
147
  showExpandColumn(props, grouping) && {
149
148
  Cell: ({ row }) => (
150
- <MRT_ExpandButton row={row as any} table={table} />
149
+ <MRT_ExpandButton row={row as any} table={table as any} />
151
150
  ),
152
151
  Header: () =>
153
152
  props.enableExpandAll ? (
154
- <MRT_ExpandAllButton table={table} />
153
+ <MRT_ExpandAllButton table={table as any} />
155
154
  ) : null,
156
155
  header: props.localization?.expand,
157
156
  size: 60,
@@ -161,11 +160,11 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
161
160
  },
162
161
  columnOrder.includes('mrt-row-select') && {
163
162
  Cell: ({ row }) => (
164
- <MRT_SelectCheckbox row={row as any} table={table} />
163
+ <MRT_SelectCheckbox row={row as any} table={table as any} />
165
164
  ),
166
165
  Header: () =>
167
166
  props.enableSelectAll ? (
168
- <MRT_SelectCheckbox selectAll table={table} />
167
+ <MRT_SelectCheckbox selectAll table={table as any} />
169
168
  ) : null,
170
169
  header: props.localization?.select,
171
170
  size: 60,
@@ -302,7 +301,13 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
302
301
  setShowAlertBanner: props.onShowAlertBannerChange ?? setShowAlertBanner,
303
302
  setShowFilters: props.onShowFiltersChange ?? setShowFilters,
304
303
  setShowGlobalFilter: props.onShowGlobalFilterChange ?? setShowGlobalFilter,
305
- } as MRT_TableInstance;
304
+ } as MRT_TableInstance<TData>;
305
+
306
+ useEffect(() => {
307
+ if (props.tableInstanceRef) {
308
+ props.tableInstanceRef.current = table;
309
+ }
310
+ }, [table]);
306
311
 
307
312
  return (
308
313
  <>
@@ -316,9 +321,9 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
316
321
  open={isFullScreen}
317
322
  transitionDuration={400}
318
323
  >
319
- <MRT_TablePaper table={table} />
324
+ <MRT_TablePaper table={table as any} />
320
325
  </Dialog>
321
- {!isFullScreen && <MRT_TablePaper table={table} />}
326
+ {!isFullScreen && <MRT_TablePaper table={table as any} />}
322
327
  {editingRow && props.editingMode === 'modal' && (
323
328
  <MRT_EditRowModal row={editingRow as any} table={table} open />
324
329
  )}
@@ -13,14 +13,22 @@ export const MRT_ToolbarDropZone: FC<Props> = ({ table }) => {
13
13
  setHoveredColumn,
14
14
  } = table;
15
15
 
16
- const { draggingColumn, hoveredColumn } = getState();
16
+ const { draggingColumn, hoveredColumn, grouping } = getState();
17
17
 
18
18
  const handleDragEnter = (_event: DragEvent<HTMLDivElement>) => {
19
19
  setHoveredColumn({ id: 'drop-zone' });
20
20
  };
21
21
 
22
22
  return (
23
- <Fade unmountOnExit mountOnEnter in={!!enableGrouping && !!draggingColumn}>
23
+ <Fade
24
+ unmountOnExit
25
+ mountOnEnter
26
+ in={
27
+ !!enableGrouping &&
28
+ !!draggingColumn &&
29
+ !grouping.includes(draggingColumn.id)
30
+ }
31
+ >
24
32
  <Box
25
33
  sx={(theme) => ({
26
34
  alignItems: 'center',