material-react-table 0.36.2 → 0.37.0

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.
@@ -27,8 +27,8 @@ export const MRT_GlobalFilterTextField = <
27
27
  icons: { SearchIcon, CloseIcon },
28
28
  localization,
29
29
  muiSearchTextFieldProps,
30
- tableId,
31
30
  },
31
+ refs: { searchInputRef },
32
32
  } = table;
33
33
  const { globalFilter, showGlobalFilter } = getState();
34
34
 
@@ -64,7 +64,6 @@ export const MRT_GlobalFilterTextField = <
64
64
  return (
65
65
  <Collapse in={showGlobalFilter} orientation="horizontal">
66
66
  <TextField
67
- id={`mrt-${tableId}-search-text-field`}
68
67
  placeholder={localization.search}
69
68
  onChange={handleChange}
70
69
  value={searchValue ?? ''}
@@ -104,6 +103,12 @@ export const MRT_GlobalFilterTextField = <
104
103
  ),
105
104
  }}
106
105
  {...textFieldProps}
106
+ inputRef={(inputRef) => {
107
+ searchInputRef.current = inputRef;
108
+ if (textFieldProps?.inputRef) {
109
+ textFieldProps.inputRef = inputRef;
110
+ }
111
+ }}
107
112
  />
108
113
  <MRT_FilterOptionMenu
109
114
  anchorEl={anchorEl}
@@ -54,9 +54,9 @@ export const MRT_ColumnActionMenu: FC<Props> = ({
54
54
  RestartAltIcon,
55
55
  VisibilityOffIcon,
56
56
  },
57
- tableId,
58
57
  localization,
59
58
  },
59
+ refs: { filterInputRefs },
60
60
  setShowFilters,
61
61
  } = table;
62
62
  const { column } = header;
@@ -111,17 +111,7 @@ export const MRT_ColumnActionMenu: FC<Props> = ({
111
111
 
112
112
  const handleFilterByColumn = () => {
113
113
  setShowFilters(true);
114
- setTimeout(
115
- () =>
116
- document
117
- .getElementById(
118
- // @ts-ignore
119
- header.muiTableHeadCellFilterTextFieldProps?.id ??
120
- `mrt-${tableId}-${header.id}-filter-text-field`,
121
- )
122
- ?.focus(),
123
- 200,
124
- );
114
+ queueMicrotask(() => filterInputRefs.current[`${column.id}-0`]?.focus());
125
115
  setAnchorEl(null);
126
116
  };
127
117
 
@@ -3,7 +3,6 @@ import React, {
3
3
  RefObject,
4
4
  useEffect,
5
5
  useLayoutEffect,
6
- useRef,
7
6
  useState,
8
7
  } from 'react';
9
8
  import { TableContainer } from '@mui/material';
@@ -24,8 +23,8 @@ export const MRT_TableContainer: FC<Props> = ({ table }) => {
24
23
  enableStickyHeader,
25
24
  enableRowVirtualization,
26
25
  muiTableContainerProps,
27
- tableId,
28
26
  },
27
+ refs: { tableContainerRef, bottomToolbarRef, topToolbarRef },
29
28
  } = table;
30
29
  const { isFullScreen } = getState();
31
30
 
@@ -36,20 +35,15 @@ export const MRT_TableContainer: FC<Props> = ({ table }) => {
36
35
  ? muiTableContainerProps({ table })
37
36
  : muiTableContainerProps;
38
37
 
39
- const tableContainerRef =
40
- tableContainerProps?.ref ?? useRef<HTMLDivElement>(null);
41
-
42
38
  useIsomorphicLayoutEffect(() => {
43
39
  const topToolbarHeight =
44
40
  typeof document !== 'undefined'
45
- ? document?.getElementById(`mrt-${tableId}-toolbar-top`)
46
- ?.offsetHeight ?? 0
41
+ ? topToolbarRef.current?.offsetHeight ?? 0
47
42
  : 0;
48
43
 
49
44
  const bottomToolbarHeight =
50
45
  typeof document !== 'undefined'
51
- ? document?.getElementById(`mrt-${tableId}-toolbar-bottom`)
52
- ?.offsetHeight ?? 0
46
+ ? bottomToolbarRef?.current?.offsetHeight ?? 0
53
47
  : 0;
54
48
 
55
49
  setTotalToolbarHeight(topToolbarHeight + bottomToolbarHeight);
@@ -57,8 +51,14 @@ export const MRT_TableContainer: FC<Props> = ({ table }) => {
57
51
 
58
52
  return (
59
53
  <TableContainer
60
- ref={tableContainerRef}
61
54
  {...tableContainerProps}
55
+ ref={(ref: HTMLDivElement) => {
56
+ tableContainerRef.current = ref;
57
+ if (tableContainerProps?.ref) {
58
+ //@ts-ignore
59
+ tableContainerProps.ref.current = ref;
60
+ }
61
+ }}
62
62
  sx={(theme) => ({
63
63
  maxWidth: '100%',
64
64
  maxHeight:
@@ -1,4 +1,4 @@
1
- import React, { useEffect, useMemo, useState } from 'react';
1
+ import React, { useMemo, useRef, useState } from 'react';
2
2
  import {
3
3
  TableState,
4
4
  getCoreRowModel,
@@ -38,12 +38,12 @@ import { MRT_EditRowModal } from '../body/MRT_EditRowModal';
38
38
  export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
39
39
  props: MaterialReactTableProps<TData>,
40
40
  ) => {
41
- const [tableId, setIdPrefix] = useState(props.tableId);
42
- useEffect(
43
- () =>
44
- setIdPrefix(props.tableId ?? Math.random().toString(36).substring(2, 9)),
45
- [props.tableId],
46
- );
41
+ const bottomToolbarRef = useRef<HTMLDivElement>(null);
42
+ const editInputRefs = useRef<Record<string, HTMLInputElement>>({});
43
+ const filterInputRefs = useRef<Record<string, HTMLInputElement>>({});
44
+ const searchInputRef = useRef<HTMLInputElement>(null);
45
+ const tableContainerRef = useRef<HTMLDivElement>(null);
46
+ const topToolbarRef = useRef<HTMLDivElement>(null);
47
47
 
48
48
  const initialState: Partial<MRT_TableState<TData>> = useMemo(() => {
49
49
  const initState = props.initialState ?? {};
@@ -126,11 +126,8 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
126
126
  id: 'mrt-row-drag',
127
127
  },
128
128
  columnOrder.includes('mrt-row-actions') && {
129
- Cell: ({ cell }) => (
130
- <MRT_ToggleRowActionMenuButton
131
- row={cell.row as any}
132
- table={table}
133
- />
129
+ Cell: ({ row }) => (
130
+ <MRT_ToggleRowActionMenuButton row={row as any} table={table} />
134
131
  ),
135
132
  header: props.localization?.actions,
136
133
  size: 70,
@@ -139,8 +136,8 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
139
136
  id: 'mrt-row-actions',
140
137
  },
141
138
  columnOrder.includes('mrt-row-expand') && {
142
- Cell: ({ cell }) => (
143
- <MRT_ExpandButton row={cell.row as any} table={table} />
139
+ Cell: ({ row }) => (
140
+ <MRT_ExpandButton row={row as any} table={table} />
144
141
  ),
145
142
  Header: () =>
146
143
  props.enableExpandAll ? (
@@ -153,8 +150,8 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
153
150
  id: 'mrt-row-expand',
154
151
  },
155
152
  columnOrder.includes('mrt-row-select') && {
156
- Cell: ({ cell }) => (
157
- <MRT_SelectCheckbox row={cell.row as any} table={table} />
153
+ Cell: ({ row }) => (
154
+ <MRT_SelectCheckbox row={row as any} table={table} />
158
155
  ),
159
156
  Header: () =>
160
157
  props.enableSelectAll ? (
@@ -167,7 +164,7 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
167
164
  id: 'mrt-row-select',
168
165
  },
169
166
  columnOrder.includes('mrt-row-numbers') && {
170
- Cell: ({ cell }) => cell.row.index + 1,
167
+ Cell: ({ row }) => row.index + 1,
171
168
  Header: () => props.localization?.rowNumber,
172
169
  header: props.localization?.rowNumbers,
173
170
  size: 60,
@@ -269,8 +266,15 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
269
266
  showGlobalFilter,
270
267
  ...props.state,
271
268
  } as TableState,
272
- tableId,
273
269
  }),
270
+ refs: {
271
+ bottomToolbarRef,
272
+ editInputRefs,
273
+ filterInputRefs,
274
+ searchInputRef,
275
+ tableContainerRef,
276
+ topToolbarRef,
277
+ },
274
278
  setColumnFilterFns: props.onFilterFnsChange ?? setColumnFilterFns,
275
279
  setDensity: props.onDensityChange ?? setDensity,
276
280
  setDraggingColumn: props.onDraggingColumnChange ?? setDraggingColumn,
@@ -21,8 +21,8 @@ export const MRT_BottomToolbar: FC<Props> = ({ table }) => {
21
21
  positionToolbarAlertBanner,
22
22
  positionToolbarDropZone,
23
23
  renderBottomToolbarCustomActions,
24
- tableId,
25
24
  },
25
+ refs: { bottomToolbarRef },
26
26
  } = table;
27
27
  const { isFullScreen } = getState();
28
28
 
@@ -37,9 +37,15 @@ export const MRT_BottomToolbar: FC<Props> = ({ table }) => {
37
37
 
38
38
  return (
39
39
  <Toolbar
40
- id={`mrt-${tableId}-toolbar-bottom`}
41
40
  variant="dense"
42
41
  {...toolbarProps}
42
+ ref={(ref: HTMLDivElement) => {
43
+ bottomToolbarRef.current = ref;
44
+ if (toolbarProps?.ref) {
45
+ // @ts-ignore
46
+ toolbarProps.ref.current = ref;
47
+ }
48
+ }}
43
49
  sx={(theme) =>
44
50
  ({
45
51
  ...commonToolbarStyles({ theme }),
@@ -37,8 +37,8 @@ export const MRT_TopToolbar: FC<Props> = ({ table }) => {
37
37
  positionToolbarAlertBanner,
38
38
  positionToolbarDropZone,
39
39
  renderTopToolbarCustomActions,
40
- tableId,
41
40
  },
41
+ refs: { topToolbarRef },
42
42
  } = table;
43
43
 
44
44
  const { isFullScreen, showGlobalFilter } = getState();
@@ -55,9 +55,15 @@ export const MRT_TopToolbar: FC<Props> = ({ table }) => {
55
55
 
56
56
  return (
57
57
  <Toolbar
58
- id={`mrt-${tableId}-toolbar-top`}
59
58
  variant="dense"
60
59
  {...toolbarProps}
60
+ ref={(ref: HTMLDivElement) => {
61
+ topToolbarRef.current = ref;
62
+ if (toolbarProps?.ref) {
63
+ // @ts-ignore
64
+ toolbarProps.ref.current = ref;
65
+ }
66
+ }}
61
67
  sx={(theme) =>
62
68
  ({
63
69
  position: isFullScreen ? 'sticky' : undefined,