material-react-table 0.6.0 → 0.6.3
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 +18 -10
- package/dist/filtersFNs.d.ts +6 -10
- package/dist/material-react-table.cjs.development.js +231 -200
- 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 +232 -201
- package/dist/material-react-table.esm.js.map +1 -1
- package/dist/toolbar/MRT_LinearProgressBar.d.ts +5 -0
- package/dist/toolbar/MRT_ToolbarTop.d.ts +11 -0
- package/dist/useMRT.d.ts +5 -1
- package/dist/utils.d.ts +2 -0
- package/package.json +3 -3
- package/src/MaterialReactTable.tsx +66 -52
- package/src/body/MRT_TableBody.tsx +3 -1
- package/src/filtersFNs.ts +6 -16
- package/src/inputs/MRT_FilterTextField.tsx +1 -0
- package/src/menus/MRT_ColumnActionMenu.tsx +5 -4
- package/src/table/MRT_TableContainer.tsx +4 -28
- package/src/toolbar/MRT_LinearProgressBar.tsx +25 -0
- package/src/toolbar/MRT_TablePagination.tsx +1 -1
- package/src/toolbar/MRT_ToolbarBottom.tsx +18 -21
- package/src/toolbar/MRT_ToolbarTop.tsx +21 -10
- package/src/useMRT.tsx +53 -48
- package/src/utils.ts +17 -0
package/src/useMRT.tsx
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import React, {
|
|
2
2
|
Context,
|
|
3
|
+
Dispatch,
|
|
3
4
|
PropsWithChildren,
|
|
5
|
+
SetStateAction,
|
|
4
6
|
createContext,
|
|
7
|
+
useCallback,
|
|
5
8
|
useContext,
|
|
6
9
|
useMemo,
|
|
7
10
|
useState,
|
|
8
|
-
Dispatch,
|
|
9
|
-
SetStateAction,
|
|
10
11
|
} from 'react';
|
|
11
12
|
import {
|
|
12
13
|
PluginHook,
|
|
@@ -21,18 +22,24 @@ import {
|
|
|
21
22
|
useSortBy,
|
|
22
23
|
useTable,
|
|
23
24
|
} from 'react-table';
|
|
24
|
-
import type {
|
|
25
|
+
import type {
|
|
26
|
+
MRT_ColumnInterface,
|
|
27
|
+
MRT_FilterType,
|
|
28
|
+
MRT_Row,
|
|
29
|
+
MRT_TableInstance,
|
|
30
|
+
} from '.';
|
|
25
31
|
import { MRT_FILTER_TYPE } from './enums';
|
|
26
|
-
import { defaultFilterFNs } from './filtersFNs';
|
|
27
32
|
import { MRT_Icons } from './icons';
|
|
28
33
|
import { MRT_Localization } from './localization';
|
|
29
34
|
import { MaterialReactTableProps } from './MaterialReactTable';
|
|
35
|
+
import { findLowestLevelCols } from './utils';
|
|
30
36
|
|
|
31
37
|
export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
|
|
32
38
|
anyRowsCanExpand: boolean;
|
|
33
39
|
anyRowsExpanded: boolean;
|
|
34
40
|
icons: MRT_Icons;
|
|
35
41
|
idPrefix: string;
|
|
42
|
+
filterTypes: { [key in MRT_FILTER_TYPE]: any };
|
|
36
43
|
localization: MRT_Localization;
|
|
37
44
|
setCurrentEditingRow: Dispatch<SetStateAction<MRT_Row<D> | null>>;
|
|
38
45
|
setCurrentFilterTypes: Dispatch<
|
|
@@ -82,65 +89,63 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
|
|
|
82
89
|
props.initialState?.showSearch ?? false,
|
|
83
90
|
);
|
|
84
91
|
|
|
85
|
-
const
|
|
86
|
-
[key
|
|
87
|
-
}>(
|
|
88
|
-
(
|
|
89
|
-
...defaultFilterFNs,
|
|
90
|
-
...props.filterTypes,
|
|
91
|
-
}),
|
|
92
|
-
[props.filterTypes],
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
const getInitialFilterTypeState = () => {
|
|
96
|
-
let lowestLevelColumns: any[] = props.columns;
|
|
97
|
-
let currentCols: any[] = props.columns;
|
|
98
|
-
while (!!currentCols.length && currentCols.some((col) => col.columns)) {
|
|
99
|
-
const nextCols = currentCols
|
|
100
|
-
.filter((col) => !!col.columns)
|
|
101
|
-
.map((col) => col.columns)
|
|
102
|
-
.flat();
|
|
103
|
-
if (nextCols.every((col) => !col.columns)) {
|
|
104
|
-
lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
|
|
105
|
-
}
|
|
106
|
-
currentCols = nextCols;
|
|
107
|
-
}
|
|
108
|
-
lowestLevelColumns = lowestLevelColumns.filter((col) => !col.columns);
|
|
109
|
-
|
|
110
|
-
return Object.assign(
|
|
92
|
+
const [currentFilterTypes, setCurrentFilterTypes] = useState<{
|
|
93
|
+
[key: string]: MRT_FilterType;
|
|
94
|
+
}>(() =>
|
|
95
|
+
Object.assign(
|
|
111
96
|
{},
|
|
112
|
-
...
|
|
97
|
+
...findLowestLevelCols(props.columns).map((c) => ({
|
|
113
98
|
[c.accessor as string]:
|
|
114
99
|
c.filter ??
|
|
115
100
|
props?.initialState?.filters?.[c.accessor as any] ??
|
|
116
101
|
(!!c.filterSelectOptions ? 'equals' : 'fuzzy'),
|
|
117
102
|
})),
|
|
118
|
-
)
|
|
119
|
-
|
|
103
|
+
),
|
|
104
|
+
);
|
|
120
105
|
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
106
|
+
const applyFiltersToColumns = useCallback(
|
|
107
|
+
(cols: MRT_ColumnInterface[]) =>
|
|
108
|
+
cols.map((column) => {
|
|
109
|
+
if (column.columns) {
|
|
110
|
+
applyFiltersToColumns(column.columns);
|
|
111
|
+
} else {
|
|
112
|
+
column.filter =
|
|
113
|
+
props?.filterTypes?.[
|
|
114
|
+
currentFilterTypes[column.accessor as string] as MRT_FILTER_TYPE
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
return column;
|
|
118
|
+
}),
|
|
119
|
+
[currentFilterTypes, props.filterTypes],
|
|
120
|
+
);
|
|
124
121
|
|
|
125
122
|
const columns = useMemo(
|
|
123
|
+
() => applyFiltersToColumns(props.columns),
|
|
124
|
+
[props.columns, applyFiltersToColumns],
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const data = useMemo(
|
|
126
128
|
() =>
|
|
127
|
-
props.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
129
|
+
!props.isLoading || !!props.data.length
|
|
130
|
+
? props.data
|
|
131
|
+
: [...Array(10)].map((_) =>
|
|
132
|
+
Object.assign(
|
|
133
|
+
{},
|
|
134
|
+
...findLowestLevelCols(props.columns).map((c) => ({
|
|
135
|
+
[c.accessor as string]: null,
|
|
136
|
+
})),
|
|
137
|
+
),
|
|
138
|
+
),
|
|
139
|
+
[props.data, props.isLoading],
|
|
135
140
|
);
|
|
136
141
|
|
|
137
142
|
const tableInstance = useTable(
|
|
143
|
+
// @ts-ignore
|
|
138
144
|
{
|
|
139
145
|
...props,
|
|
140
|
-
columns,
|
|
141
146
|
// @ts-ignore
|
|
142
|
-
|
|
143
|
-
|
|
147
|
+
columns,
|
|
148
|
+
data,
|
|
144
149
|
useControlledState: (state) =>
|
|
145
150
|
useMemo(
|
|
146
151
|
() => ({
|
|
@@ -166,7 +171,7 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
|
|
|
166
171
|
),
|
|
167
172
|
},
|
|
168
173
|
...hooks,
|
|
169
|
-
) as MRT_TableInstance<D>;
|
|
174
|
+
) as unknown as MRT_TableInstance<D>;
|
|
170
175
|
|
|
171
176
|
const idPrefix = useMemo(
|
|
172
177
|
() => props.idPrefix ?? Math.random().toString(36).substring(2, 9),
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MRT_ColumnInterface } from '.';
|
|
2
|
+
|
|
3
|
+
export const findLowestLevelCols = (columns: MRT_ColumnInterface[]) => {
|
|
4
|
+
let lowestLevelColumns: MRT_ColumnInterface[] = columns;
|
|
5
|
+
let currentCols: MRT_ColumnInterface[] | undefined = columns;
|
|
6
|
+
while (!!currentCols?.length && currentCols.some((col) => col.columns)) {
|
|
7
|
+
const nextCols: MRT_ColumnInterface[] = currentCols
|
|
8
|
+
.filter((col) => !!col.columns)
|
|
9
|
+
.map((col) => col.columns)
|
|
10
|
+
.flat() as MRT_ColumnInterface[];
|
|
11
|
+
if (nextCols.every((col) => !col?.columns)) {
|
|
12
|
+
lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
|
|
13
|
+
}
|
|
14
|
+
currentCols = nextCols;
|
|
15
|
+
}
|
|
16
|
+
return lowestLevelColumns.filter((col) => !col.columns);
|
|
17
|
+
};
|