material-react-table 0.22.0 → 0.22.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +90 -18
- package/dist/MaterialReactTable.d.ts +158 -152
- package/dist/filtersFns.d.ts +22 -22
- package/dist/material-react-table.cjs.development.js.map +1 -1
- package/dist/material-react-table.cjs.production.min.js.map +1 -1
- package/dist/material-react-table.esm.js.map +1 -1
- package/dist/sortingFns.d.ts +2 -2
- package/dist/table/MRT_TableRoot.d.ts +1 -1
- package/dist/utils.d.ts +7 -7
- package/package.json +6 -6
- package/src/MaterialReactTable.tsx +189 -174
- package/src/filtersFns.ts +22 -22
- package/src/sortingFns.ts +6 -6
- package/src/table/MRT_TableRoot.tsx +9 -10
- package/src/utils.ts +28 -22
package/src/filtersFns.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { rankItem, rankings, RankingInfo } from '@tanstack/match-sorter-utils';
|
2
2
|
import { filterFns, Row } from '@tanstack/react-table';
|
3
3
|
|
4
|
-
export const fuzzy = <
|
5
|
-
row: Row<
|
4
|
+
export const fuzzy = <TData extends Record<string, any> = {}>(
|
5
|
+
row: Row<TData>,
|
6
6
|
columnId: string,
|
7
7
|
filterValue: string,
|
8
8
|
addMeta: (item: RankingInfo) => void,
|
@@ -16,8 +16,8 @@ export const fuzzy = <D extends Record<string, any> = {}>(
|
|
16
16
|
|
17
17
|
fuzzy.autoRemove = (val: any) => !val;
|
18
18
|
|
19
|
-
export const contains = <
|
20
|
-
row: Row<
|
19
|
+
export const contains = <TData extends Record<string, any> = {}>(
|
20
|
+
row: Row<TData>,
|
21
21
|
id: string,
|
22
22
|
filterValue: string | number,
|
23
23
|
) =>
|
@@ -30,8 +30,8 @@ export const contains = <D extends Record<string, any> = {}>(
|
|
30
30
|
|
31
31
|
contains.autoRemove = (val: any) => !val;
|
32
32
|
|
33
|
-
export const startsWith = <
|
34
|
-
row: Row<
|
33
|
+
export const startsWith = <TData extends Record<string, any> = {}>(
|
34
|
+
row: Row<TData>,
|
35
35
|
id: string,
|
36
36
|
filterValue: string | number,
|
37
37
|
) =>
|
@@ -44,8 +44,8 @@ export const startsWith = <D extends Record<string, any> = {}>(
|
|
44
44
|
|
45
45
|
startsWith.autoRemove = (val: any) => !val;
|
46
46
|
|
47
|
-
export const endsWith = <
|
48
|
-
row: Row<
|
47
|
+
export const endsWith = <TData extends Record<string, any> = {}>(
|
48
|
+
row: Row<TData>,
|
49
49
|
id: string,
|
50
50
|
filterValue: string | number,
|
51
51
|
) =>
|
@@ -58,8 +58,8 @@ export const endsWith = <D extends Record<string, any> = {}>(
|
|
58
58
|
|
59
59
|
endsWith.autoRemove = (val: any) => !val;
|
60
60
|
|
61
|
-
export const equals = <
|
62
|
-
row: Row<
|
61
|
+
export const equals = <TData extends Record<string, any> = {}>(
|
62
|
+
row: Row<TData>,
|
63
63
|
id: string,
|
64
64
|
filterValue: string | number,
|
65
65
|
) =>
|
@@ -68,8 +68,8 @@ export const equals = <D extends Record<string, any> = {}>(
|
|
68
68
|
|
69
69
|
equals.autoRemove = (val: any) => !val;
|
70
70
|
|
71
|
-
export const notEquals = <
|
72
|
-
row: Row<
|
71
|
+
export const notEquals = <TData extends Record<string, any> = {}>(
|
72
|
+
row: Row<TData>,
|
73
73
|
id: string,
|
74
74
|
filterValue: string | number,
|
75
75
|
) =>
|
@@ -78,8 +78,8 @@ export const notEquals = <D extends Record<string, any> = {}>(
|
|
78
78
|
|
79
79
|
notEquals.autoRemove = (val: any) => !val;
|
80
80
|
|
81
|
-
export const greaterThan = <
|
82
|
-
row: Row<
|
81
|
+
export const greaterThan = <TData extends Record<string, any> = {}>(
|
82
|
+
row: Row<TData>,
|
83
83
|
id: string,
|
84
84
|
filterValue: string | number,
|
85
85
|
) =>
|
@@ -90,8 +90,8 @@ export const greaterThan = <D extends Record<string, any> = {}>(
|
|
90
90
|
|
91
91
|
greaterThan.autoRemove = (val: any) => !val;
|
92
92
|
|
93
|
-
export const lessThan = <
|
94
|
-
row: Row<
|
93
|
+
export const lessThan = <TData extends Record<string, any> = {}>(
|
94
|
+
row: Row<TData>,
|
95
95
|
id: string,
|
96
96
|
filterValue: string | number,
|
97
97
|
) =>
|
@@ -102,8 +102,8 @@ export const lessThan = <D extends Record<string, any> = {}>(
|
|
102
102
|
|
103
103
|
lessThan.autoRemove = (val: any) => !val;
|
104
104
|
|
105
|
-
export const between = <
|
106
|
-
row: Row<
|
105
|
+
export const between = <TData extends Record<string, any> = {}>(
|
106
|
+
row: Row<TData>,
|
107
107
|
id: string,
|
108
108
|
filterValues: [string | number, string | number],
|
109
109
|
) =>
|
@@ -117,16 +117,16 @@ export const between = <D extends Record<string, any> = {}>(
|
|
117
117
|
|
118
118
|
between.autoRemove = (val: any) => !val;
|
119
119
|
|
120
|
-
export const empty = <
|
121
|
-
row: Row<
|
120
|
+
export const empty = <TData extends Record<string, any> = {}>(
|
121
|
+
row: Row<TData>,
|
122
122
|
id: string,
|
123
123
|
_filterValue: string | number,
|
124
124
|
) => !row.getValue(id).toString().trim();
|
125
125
|
|
126
126
|
empty.autoRemove = (val: any) => !val;
|
127
127
|
|
128
|
-
export const notEmpty = <
|
129
|
-
row: Row<
|
128
|
+
export const notEmpty = <TData extends Record<string, any> = {}>(
|
129
|
+
row: Row<TData>,
|
130
130
|
id: string,
|
131
131
|
_filterValue: string | number,
|
132
132
|
) => !!row.getValue(id).toString().trim();
|
package/src/sortingFns.ts
CHANGED
@@ -2,9 +2,9 @@ import { compareItems, RankingInfo } from '@tanstack/match-sorter-utils';
|
|
2
2
|
import { Row, sortingFns } from '@tanstack/react-table';
|
3
3
|
import { MRT_Row } from '.';
|
4
4
|
|
5
|
-
const fuzzy = <
|
6
|
-
rowA: Row<
|
7
|
-
rowB: Row<
|
5
|
+
const fuzzy = <TData extends Record<string, any> = {}>(
|
6
|
+
rowA: Row<TData>,
|
7
|
+
rowB: Row<TData>,
|
8
8
|
columnId: string,
|
9
9
|
) => {
|
10
10
|
let dir = 0;
|
@@ -25,9 +25,9 @@ export const MRT_SortingFns = {
|
|
25
25
|
fuzzy,
|
26
26
|
};
|
27
27
|
|
28
|
-
export const rankGlobalFuzzy = <
|
29
|
-
rowA: MRT_Row<
|
30
|
-
rowB: MRT_Row<
|
28
|
+
export const rankGlobalFuzzy = <TData extends Record<string, any> = {}>(
|
29
|
+
rowA: MRT_Row<TData>,
|
30
|
+
rowB: MRT_Row<TData>,
|
31
31
|
) =>
|
32
32
|
Math.max(...Object.values(rowB.columnFiltersMeta).map((v: any) => v.rank)) -
|
33
33
|
Math.max(...Object.values(rowA.columnFiltersMeta).map((v: any) => v.rank));
|
@@ -32,8 +32,8 @@ import {
|
|
32
32
|
} from '../utils';
|
33
33
|
import { MRT_FilterFns } from '../filtersFns';
|
34
34
|
|
35
|
-
export const MRT_TableRoot = <
|
36
|
-
props: MaterialReactTableProps<
|
35
|
+
export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
|
36
|
+
props: MaterialReactTableProps<TData>,
|
37
37
|
) => {
|
38
38
|
const [tableId, setIdPrefix] = useState(props.tableId);
|
39
39
|
useEffect(
|
@@ -42,7 +42,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
42
42
|
[props.tableId],
|
43
43
|
);
|
44
44
|
|
45
|
-
const initialState: Partial<MRT_TableState<
|
45
|
+
const initialState: Partial<MRT_TableState<TData>> = useMemo(() => {
|
46
46
|
const initState = props.initialState ?? {};
|
47
47
|
initState.columnOrder =
|
48
48
|
initState.columnOrder ?? getDefaultColumnOrderIds(props);
|
@@ -53,10 +53,9 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
53
53
|
initialState.columnOrder ?? [],
|
54
54
|
);
|
55
55
|
const [currentEditingCell, setCurrentEditingCell] =
|
56
|
-
useState<MRT_Cell<
|
57
|
-
const [currentEditingRow, setCurrentEditingRow] =
|
58
|
-
initialState?.currentEditingRow ?? null
|
59
|
-
);
|
56
|
+
useState<MRT_Cell<TData> | null>(initialState?.currentEditingCell ?? null);
|
57
|
+
const [currentEditingRow, setCurrentEditingRow] =
|
58
|
+
useState<MRT_Row<TData> | null>(initialState?.currentEditingRow ?? null);
|
60
59
|
const [density, setDensity] = useState(
|
61
60
|
initialState?.density ?? 'comfortable',
|
62
61
|
);
|
@@ -78,7 +77,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
78
77
|
}>(() =>
|
79
78
|
Object.assign(
|
80
79
|
{},
|
81
|
-
...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<
|
80
|
+
...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<TData>[]).map(
|
82
81
|
(col) => ({
|
83
82
|
[col.id?.toString() ?? col.accessorKey?.toString() ?? '']:
|
84
83
|
col.filterFn instanceof Function
|
@@ -158,7 +157,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
158
157
|
muiTableHeadCellProps: props.muiTableHeadCellProps,
|
159
158
|
size: 60,
|
160
159
|
},
|
161
|
-
] as MRT_ColumnDef<
|
160
|
+
] as MRT_ColumnDef<TData>[]
|
162
161
|
).filter(Boolean),
|
163
162
|
[
|
164
163
|
columnOrder,
|
@@ -184,7 +183,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
184
183
|
[currentFilterFns, displayColumns, props.columns],
|
185
184
|
);
|
186
185
|
|
187
|
-
const data:
|
186
|
+
const data: TData[] = useMemo(
|
188
187
|
() =>
|
189
188
|
(props.state?.isLoading || props.state?.showSkeletons) &&
|
190
189
|
!props.data.length
|
package/src/utils.ts
CHANGED
@@ -9,21 +9,21 @@ import {
|
|
9
9
|
import { MRT_FilterFns } from './filtersFns';
|
10
10
|
import { MRT_SortingFns } from './sortingFns';
|
11
11
|
|
12
|
-
const getColumnId = <
|
13
|
-
columnDef: MRT_ColumnDef<
|
12
|
+
const getColumnId = <TData extends Record<string, any> = {}>(
|
13
|
+
columnDef: MRT_ColumnDef<TData>,
|
14
14
|
): string =>
|
15
15
|
columnDef.id ?? columnDef.accessorKey?.toString?.() ?? columnDef.header;
|
16
16
|
|
17
|
-
export const getAllLeafColumnDefs = <
|
18
|
-
columns: MRT_ColumnDef<
|
19
|
-
): MRT_ColumnDef<
|
20
|
-
let lowestLevelColumns: MRT_ColumnDef<
|
21
|
-
let currentCols: MRT_ColumnDef<
|
17
|
+
export const getAllLeafColumnDefs = <TData extends Record<string, any> = {}>(
|
18
|
+
columns: MRT_ColumnDef<TData>[],
|
19
|
+
): MRT_ColumnDef<TData>[] => {
|
20
|
+
let lowestLevelColumns: MRT_ColumnDef<TData>[] = columns;
|
21
|
+
let currentCols: MRT_ColumnDef<TData>[] | undefined = columns;
|
22
22
|
while (!!currentCols?.length && currentCols.some((col) => col.columns)) {
|
23
|
-
const nextCols: MRT_ColumnDef<
|
23
|
+
const nextCols: MRT_ColumnDef<TData>[] = currentCols
|
24
24
|
.filter((col) => !!col.columns)
|
25
25
|
.map((col) => col.columns)
|
26
|
-
.flat() as MRT_ColumnDef<
|
26
|
+
.flat() as MRT_ColumnDef<TData>[];
|
27
27
|
if (nextCols.every((col) => !col?.columns)) {
|
28
28
|
lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
|
29
29
|
}
|
@@ -32,10 +32,10 @@ export const getAllLeafColumnDefs = <D extends Record<string, any> = {}>(
|
|
32
32
|
return lowestLevelColumns.filter((col) => !col.columns);
|
33
33
|
};
|
34
34
|
|
35
|
-
export const prepareColumns = <
|
36
|
-
columnDefs: MRT_ColumnDef<
|
35
|
+
export const prepareColumns = <TData extends Record<string, any> = {}>(
|
36
|
+
columnDefs: MRT_ColumnDef<TData>[],
|
37
37
|
currentFilterFns: { [key: string]: MRT_FilterOption },
|
38
|
-
): MRT_DefinedColumnDef<
|
38
|
+
): MRT_DefinedColumnDef<TData>[] =>
|
39
39
|
columnDefs.map((columnDef) => {
|
40
40
|
if (!columnDef.id) columnDef.id = getColumnId(columnDef);
|
41
41
|
if (process.env.NODE_ENV !== 'production' && !columnDef.id) {
|
@@ -58,11 +58,11 @@ export const prepareColumns = <D extends Record<string, any> = {}>(
|
|
58
58
|
}
|
59
59
|
}
|
60
60
|
return columnDef;
|
61
|
-
}) as MRT_DefinedColumnDef<
|
61
|
+
}) as MRT_DefinedColumnDef<TData>[];
|
62
62
|
|
63
|
-
export const reorderColumn = <
|
64
|
-
movingColumn: MRT_Column<
|
65
|
-
receivingColumn: MRT_Column<
|
63
|
+
export const reorderColumn = <TData extends Record<string, any> = {}>(
|
64
|
+
movingColumn: MRT_Column<TData>,
|
65
|
+
receivingColumn: MRT_Column<TData>,
|
66
66
|
columnOrder: ColumnOrderState,
|
67
67
|
): ColumnOrderState => {
|
68
68
|
if (movingColumn.getCanPin()) {
|
@@ -76,8 +76,10 @@ export const reorderColumn = <D extends Record<string, any> = {}>(
|
|
76
76
|
return [...columnOrder];
|
77
77
|
};
|
78
78
|
|
79
|
-
export const getLeadingDisplayColumnIds = <
|
80
|
-
|
79
|
+
export const getLeadingDisplayColumnIds = <
|
80
|
+
TData extends Record<string, any> = {},
|
81
|
+
>(
|
82
|
+
props: MaterialReactTableProps<TData>,
|
81
83
|
) =>
|
82
84
|
[
|
83
85
|
((props.positionActionsColumn === 'first' && props.enableRowActions) ||
|
@@ -88,16 +90,20 @@ export const getLeadingDisplayColumnIds = <D extends Record<string, any> = {}>(
|
|
88
90
|
props.enableRowNumbers && 'mrt-row-numbers',
|
89
91
|
].filter(Boolean) as string[];
|
90
92
|
|
91
|
-
export const getTrailingDisplayColumnIds = <
|
92
|
-
|
93
|
+
export const getTrailingDisplayColumnIds = <
|
94
|
+
TData extends Record<string, any> = {},
|
95
|
+
>(
|
96
|
+
props: MaterialReactTableProps<TData>,
|
93
97
|
) => [
|
94
98
|
((props.positionActionsColumn === 'last' && props.enableRowActions) ||
|
95
99
|
(props.enableEditing && props.editingMode === 'row')) &&
|
96
100
|
'mrt-row-actions',
|
97
101
|
];
|
98
102
|
|
99
|
-
export const getDefaultColumnOrderIds = <
|
100
|
-
|
103
|
+
export const getDefaultColumnOrderIds = <
|
104
|
+
TData extends Record<string, any> = {},
|
105
|
+
>(
|
106
|
+
props: MaterialReactTableProps<TData>,
|
101
107
|
) =>
|
102
108
|
[
|
103
109
|
...getLeadingDisplayColumnIds(props),
|