material-react-table 2.7.0 → 2.9.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.
- package/dist/index.d.ts +253 -99
- package/dist/index.esm.js +3546 -3475
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +3567 -3498
- package/dist/index.js.map +1 -1
- package/locales/ja/index.esm.js +4 -4
- package/locales/ja/index.js +4 -4
- package/package.json +25 -23
- package/src/{MaterialReactTable.tsx → components/MaterialReactTable.tsx} +3 -3
- package/src/{body → components/body}/MRT_TableBody.tsx +6 -7
- package/src/{body → components/body}/MRT_TableBodyCell.tsx +16 -26
- package/src/{body → components/body}/MRT_TableBodyCellValue.tsx +2 -2
- package/src/{body → components/body}/MRT_TableBodyRow.tsx +79 -59
- package/src/{body → components/body}/MRT_TableBodyRowGrabHandle.tsx +3 -3
- package/src/{body → components/body}/MRT_TableBodyRowPinButton.tsx +3 -3
- package/src/{body → components/body}/MRT_TableDetailPanel.tsx +3 -3
- package/src/{buttons → components/buttons}/MRT_ColumnPinningButtons.tsx +2 -2
- package/src/{buttons → components/buttons}/MRT_CopyButton.tsx +3 -3
- package/src/{buttons → components/buttons}/MRT_EditActionButtons.tsx +2 -2
- package/src/{buttons → components/buttons}/MRT_ExpandAllButton.tsx +3 -3
- package/src/{buttons → components/buttons}/MRT_ExpandButton.tsx +6 -3
- package/src/{buttons → components/buttons}/MRT_GrabHandleButton.tsx +3 -3
- package/src/{buttons → components/buttons}/MRT_RowPinButton.tsx +3 -3
- package/src/{buttons → components/buttons}/MRT_ShowHideColumnsButton.tsx +1 -1
- package/src/{buttons → components/buttons}/MRT_ToggleDensePaddingButton.tsx +1 -1
- package/src/{buttons → components/buttons}/MRT_ToggleFiltersButton.tsx +1 -1
- package/src/{buttons → components/buttons}/MRT_ToggleFullScreenButton.tsx +1 -1
- package/src/{buttons → components/buttons}/MRT_ToggleGlobalFilterButton.tsx +1 -1
- package/src/{buttons → components/buttons}/MRT_ToggleRowActionMenuButton.tsx +4 -4
- package/src/{footer → components/footer}/MRT_TableFooter.tsx +2 -2
- package/src/{footer → components/footer}/MRT_TableFooterCell.tsx +13 -4
- package/src/{footer → components/footer}/MRT_TableFooterRow.tsx +22 -11
- package/src/{head → components/head}/MRT_TableHead.tsx +3 -3
- package/src/{head → components/head}/MRT_TableHeadCell.tsx +13 -3
- package/src/{head → components/head}/MRT_TableHeadCellColumnActionsButton.tsx +4 -4
- package/src/{head → components/head}/MRT_TableHeadCellFilterContainer.tsx +5 -5
- package/src/{head → components/head}/MRT_TableHeadCellFilterLabel.tsx +2 -2
- package/src/{head → components/head}/MRT_TableHeadCellGrabHandle.tsx +4 -3
- package/src/{head → components/head}/MRT_TableHeadCellResizeHandle.tsx +6 -4
- package/src/{head → components/head}/MRT_TableHeadCellSortLabel.tsx +2 -2
- package/src/{head → components/head}/MRT_TableHeadRow.tsx +22 -11
- package/src/{inputs → components/inputs}/MRT_EditCellTextField.tsx +15 -6
- package/src/{inputs → components/inputs}/MRT_FilterCheckbox.tsx +3 -3
- package/src/{inputs → components/inputs}/MRT_FilterRangeFields.tsx +2 -2
- package/src/{inputs → components/inputs}/MRT_FilterRangeSlider.tsx +2 -2
- package/src/{inputs → components/inputs}/MRT_FilterTextField.tsx +3 -3
- package/src/{inputs → components/inputs}/MRT_GlobalFilterTextField.tsx +2 -2
- package/src/{inputs → components/inputs}/MRT_SelectCheckbox.tsx +7 -9
- package/src/{menus → components/menus}/MRT_ColumnActionMenu.tsx +8 -8
- package/src/{menus → components/menus}/MRT_FilterOptionMenu.tsx +2 -2
- package/src/{menus → components/menus}/MRT_RowActionMenu.tsx +9 -9
- package/src/{menus → components/menus}/MRT_ShowHideColumnsMenu.tsx +5 -7
- package/src/{menus → components/menus}/MRT_ShowHideColumnsMenuItems.tsx +6 -5
- package/src/{modals → components/modals}/MRT_EditRowModal.tsx +4 -4
- package/src/{table → components/table}/MRT_Table.tsx +8 -4
- package/src/{table → components/table}/MRT_TableContainer.tsx +2 -2
- package/src/{table → components/table}/MRT_TableLoadingOverlay.tsx +3 -3
- package/src/{table → components/table}/MRT_TablePaper.tsx +3 -3
- package/src/{toolbar → components/toolbar}/MRT_BottomToolbar.tsx +3 -3
- package/src/{toolbar → components/toolbar}/MRT_LinearProgressBar.tsx +2 -2
- package/src/{toolbar → components/toolbar}/MRT_TablePagination.tsx +3 -3
- package/src/{toolbar → components/toolbar}/MRT_ToolbarAlertBanner.tsx +3 -3
- package/src/{toolbar → components/toolbar}/MRT_ToolbarDropZone.tsx +2 -2
- package/src/{toolbar → components/toolbar}/MRT_ToolbarInternalButtons.tsx +2 -2
- package/src/{toolbar → components/toolbar}/MRT_TopToolbar.tsx +3 -3
- package/src/{filterFns.ts → fns/filterFns.ts} +1 -1
- package/src/{sortingFns.ts → fns/sortingFns.ts} +1 -1
- package/src/hooks/display-columns/getMRT_DisplayColumns.tsx +26 -0
- package/src/hooks/display-columns/getMRT_RowActionsColumnDef.tsx +35 -0
- package/src/hooks/display-columns/getMRT_RowDragColumnDef.tsx +36 -0
- package/src/hooks/display-columns/getMRT_RowExpandColumnDef.tsx +98 -0
- package/src/hooks/display-columns/getMRT_RowNumbersColumnDef.tsx +37 -0
- package/src/hooks/display-columns/getMRT_RowPinningColumnDef.tsx +31 -0
- package/src/hooks/display-columns/getMRT_RowSelectColumnDef.tsx +41 -0
- package/src/hooks/display-columns/getMRT_RowSpacerColumnDef.tsx +40 -0
- package/src/hooks/useMRT_ColumnVirtualizer.ts +79 -80
- package/src/hooks/useMRT_Effects.ts +1 -1
- package/src/hooks/useMRT_RowVirtualizer.ts +32 -36
- package/src/hooks/useMRT_Rows.ts +2 -2
- package/src/hooks/useMRT_TableInstance.ts +54 -39
- package/src/hooks/useMRT_TableOptions.ts +30 -20
- package/src/{useMaterialReactTable.ts → hooks/useMaterialReactTable.ts} +3 -3
- package/src/index.ts +79 -82
- package/src/locales/ja.ts +4 -4
- package/src/types.ts +57 -26
- package/src/utils/column.utils.ts +173 -0
- package/src/utils/displayColumn.utils.ts +134 -0
- package/src/utils/row.utils.ts +47 -0
- package/src/{style.utils.ts → utils/style.utils.ts} +88 -42
- package/src/utils/tanstack.helpers.ts +64 -0
- package/src/utils/utils.ts +23 -0
- package/src/utils/virtualization.utils.ts +19 -0
- package/src/utils.ts +0 -0
- package/src/column.utils.ts +0 -361
- package/src/hooks/useMRT_DisplayColumns.tsx +0 -290
- /package/src/{aggregationFns.ts → fns/aggregationFns.ts} +0 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
import {
|
2
|
+
type MRT_ColumnDef,
|
3
|
+
type MRT_RowData,
|
4
|
+
type MRT_StatefulTableOptions,
|
5
|
+
} from '../../types';
|
6
|
+
import {
|
7
|
+
defaultDisplayColumnProps,
|
8
|
+
showRowNumbersColumn,
|
9
|
+
} from '../../utils/displayColumn.utils';
|
10
|
+
|
11
|
+
export const getMRT_RowNumbersColumnDef = <TData extends MRT_RowData>(
|
12
|
+
tableOptions: MRT_StatefulTableOptions<TData>,
|
13
|
+
): MRT_ColumnDef<TData> | null => {
|
14
|
+
if (!showRowNumbersColumn(tableOptions)) {
|
15
|
+
return null;
|
16
|
+
}
|
17
|
+
|
18
|
+
const { localization, rowNumberDisplayMode } = tableOptions;
|
19
|
+
const {
|
20
|
+
pagination: { pageIndex, pageSize },
|
21
|
+
} = tableOptions.state;
|
22
|
+
|
23
|
+
return {
|
24
|
+
Cell: ({ row, staticRowIndex }) =>
|
25
|
+
((rowNumberDisplayMode === 'static'
|
26
|
+
? (staticRowIndex || 0) + (pageSize || 0) * (pageIndex || 0)
|
27
|
+
: row.index) ?? 0) + 1,
|
28
|
+
Header: () => localization.rowNumber,
|
29
|
+
grow: false,
|
30
|
+
...defaultDisplayColumnProps({
|
31
|
+
header: 'rowNumbers',
|
32
|
+
id: 'mrt-row-numbers',
|
33
|
+
size: 50,
|
34
|
+
tableOptions,
|
35
|
+
}),
|
36
|
+
};
|
37
|
+
};
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { MRT_TableBodyRowPinButton } from '../../components/body/MRT_TableBodyRowPinButton';
|
2
|
+
import {
|
3
|
+
type MRT_ColumnDef,
|
4
|
+
type MRT_RowData,
|
5
|
+
type MRT_StatefulTableOptions,
|
6
|
+
} from '../../types';
|
7
|
+
import {
|
8
|
+
defaultDisplayColumnProps,
|
9
|
+
showRowPinningColumn,
|
10
|
+
} from '../../utils/displayColumn.utils';
|
11
|
+
|
12
|
+
export const getMRT_RowPinningColumnDef = <TData extends MRT_RowData>(
|
13
|
+
tableOptions: MRT_StatefulTableOptions<TData>,
|
14
|
+
): MRT_ColumnDef<TData> | null => {
|
15
|
+
if (!showRowPinningColumn(tableOptions)) {
|
16
|
+
return null;
|
17
|
+
}
|
18
|
+
|
19
|
+
return {
|
20
|
+
Cell: ({ row, table }) => (
|
21
|
+
<MRT_TableBodyRowPinButton row={row} table={table} />
|
22
|
+
),
|
23
|
+
grow: false,
|
24
|
+
...defaultDisplayColumnProps({
|
25
|
+
header: 'pin',
|
26
|
+
id: 'mrt-row-pin',
|
27
|
+
size: 60,
|
28
|
+
tableOptions,
|
29
|
+
}),
|
30
|
+
};
|
31
|
+
};
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import { MRT_SelectCheckbox } from '../../components/inputs/MRT_SelectCheckbox';
|
2
|
+
import {
|
3
|
+
type MRT_ColumnDef,
|
4
|
+
type MRT_RowData,
|
5
|
+
type MRT_StatefulTableOptions,
|
6
|
+
} from '../../types';
|
7
|
+
import {
|
8
|
+
defaultDisplayColumnProps,
|
9
|
+
showRowSelectionColumn,
|
10
|
+
} from '../../utils/displayColumn.utils';
|
11
|
+
|
12
|
+
export const getMRT_RowSelectColumnDef = <TData extends MRT_RowData>(
|
13
|
+
tableOptions: MRT_StatefulTableOptions<TData>,
|
14
|
+
): MRT_ColumnDef<TData> | null => {
|
15
|
+
if (!showRowSelectionColumn(tableOptions)) {
|
16
|
+
return null;
|
17
|
+
}
|
18
|
+
|
19
|
+
const { enableMultiRowSelection, enableSelectAll } = tableOptions;
|
20
|
+
|
21
|
+
return {
|
22
|
+
Cell: ({ row, staticRowIndex, table }) => (
|
23
|
+
<MRT_SelectCheckbox
|
24
|
+
row={row}
|
25
|
+
staticRowIndex={staticRowIndex}
|
26
|
+
table={table}
|
27
|
+
/>
|
28
|
+
),
|
29
|
+
Header:
|
30
|
+
enableSelectAll && enableMultiRowSelection
|
31
|
+
? ({ table }) => <MRT_SelectCheckbox table={table} />
|
32
|
+
: undefined,
|
33
|
+
grow: false,
|
34
|
+
...defaultDisplayColumnProps({
|
35
|
+
header: 'select',
|
36
|
+
id: 'mrt-row-select',
|
37
|
+
size: enableSelectAll ? 60 : 70,
|
38
|
+
tableOptions,
|
39
|
+
}),
|
40
|
+
};
|
41
|
+
};
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { MRT_DefaultDisplayColumn } from '../useMRT_TableOptions';
|
2
|
+
import {
|
3
|
+
type MRT_ColumnDef,
|
4
|
+
type MRT_RowData,
|
5
|
+
type MRT_StatefulTableOptions,
|
6
|
+
} from '../../types';
|
7
|
+
import {
|
8
|
+
defaultDisplayColumnProps,
|
9
|
+
showRowSpacerColumn,
|
10
|
+
} from '../../utils/displayColumn.utils';
|
11
|
+
|
12
|
+
const blankColProps = {
|
13
|
+
children: null,
|
14
|
+
sx: {
|
15
|
+
minWidth: 0,
|
16
|
+
p: 0,
|
17
|
+
width: 0,
|
18
|
+
},
|
19
|
+
};
|
20
|
+
|
21
|
+
export const getMRT_RowSpacerColumnDef = <TData extends MRT_RowData>(
|
22
|
+
tableOptions: MRT_StatefulTableOptions<TData>,
|
23
|
+
): MRT_ColumnDef<TData> | null => {
|
24
|
+
if (!showRowSpacerColumn(tableOptions)) {
|
25
|
+
return null;
|
26
|
+
}
|
27
|
+
|
28
|
+
return {
|
29
|
+
...defaultDisplayColumnProps({
|
30
|
+
id: 'mrt-row-spacer',
|
31
|
+
size: 0,
|
32
|
+
tableOptions,
|
33
|
+
}),
|
34
|
+
grow: true,
|
35
|
+
...MRT_DefaultDisplayColumn,
|
36
|
+
muiTableBodyCellProps: blankColProps,
|
37
|
+
muiTableFooterCellProps: blankColProps,
|
38
|
+
muiTableHeadCellProps: blankColProps,
|
39
|
+
};
|
40
|
+
};
|
@@ -1,14 +1,12 @@
|
|
1
1
|
import { useCallback, useMemo } from 'react';
|
2
2
|
import { type Range, useVirtualizer } from '@tanstack/react-virtual';
|
3
|
-
import {
|
4
|
-
extraIndexRangeExtractor,
|
5
|
-
parseFromValuesOrFunc,
|
6
|
-
} from '../column.utils';
|
7
3
|
import {
|
8
4
|
type MRT_ColumnVirtualizer,
|
9
5
|
type MRT_RowData,
|
10
6
|
type MRT_TableInstance,
|
11
7
|
} from '../types';
|
8
|
+
import { parseFromValuesOrFunc } from '../utils/utils';
|
9
|
+
import { extraIndexRangeExtractor } from '../utils/virtualization.utils';
|
12
10
|
|
13
11
|
export const useMRT_ColumnVirtualizer = <
|
14
12
|
TData extends MRT_RowData,
|
@@ -18,7 +16,10 @@ export const useMRT_ColumnVirtualizer = <
|
|
18
16
|
table: MRT_TableInstance<TData>,
|
19
17
|
): MRT_ColumnVirtualizer | undefined => {
|
20
18
|
const {
|
19
|
+
getLeftLeafColumns,
|
20
|
+
getRightLeafColumns,
|
21
21
|
getState,
|
22
|
+
getVisibleLeafColumns,
|
22
23
|
options: {
|
23
24
|
columnVirtualizerInstanceRef,
|
24
25
|
columnVirtualizerOptions,
|
@@ -27,7 +28,9 @@ export const useMRT_ColumnVirtualizer = <
|
|
27
28
|
},
|
28
29
|
refs: { tableContainerRef },
|
29
30
|
} = table;
|
30
|
-
const { columnPinning,
|
31
|
+
const { columnPinning, draggingColumn } = getState();
|
32
|
+
|
33
|
+
if (!enableColumnVirtualization) return undefined;
|
31
34
|
|
32
35
|
const columnVirtualizerProps = parseFromValuesOrFunc(
|
33
36
|
columnVirtualizerOptions,
|
@@ -36,91 +39,87 @@ export const useMRT_ColumnVirtualizer = <
|
|
36
39
|
},
|
37
40
|
);
|
38
41
|
|
42
|
+
const visibleColumns = getVisibleLeafColumns();
|
43
|
+
|
39
44
|
const [leftPinnedIndexes, rightPinnedIndexes] = useMemo(
|
40
45
|
() =>
|
41
|
-
|
46
|
+
enableColumnPinning
|
42
47
|
? [
|
43
|
-
|
44
|
-
|
45
|
-
.getRightLeafColumns()
|
48
|
+
getLeftLeafColumns().map((c) => c.getPinnedIndex()),
|
49
|
+
getRightLeafColumns()
|
46
50
|
.map(
|
47
|
-
(
|
48
|
-
table.getVisibleLeafColumns().length - c.getPinnedIndex() - 1,
|
51
|
+
(column) => visibleColumns.length - column.getPinnedIndex() - 1,
|
49
52
|
)
|
50
53
|
.sort((a, b) => a - b),
|
51
54
|
]
|
52
55
|
: [[], []],
|
53
|
-
[columnPinning,
|
56
|
+
[columnPinning, enableColumnPinning],
|
57
|
+
);
|
58
|
+
|
59
|
+
const numPinnedLeft = leftPinnedIndexes.length;
|
60
|
+
const numPinnedRight = rightPinnedIndexes.length;
|
61
|
+
|
62
|
+
const draggingColumnIndex = useMemo(
|
63
|
+
() =>
|
64
|
+
draggingColumn?.id
|
65
|
+
? visibleColumns.findIndex((c) => c.id === draggingColumn?.id)
|
66
|
+
: undefined,
|
67
|
+
[draggingColumn?.id],
|
54
68
|
);
|
55
69
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
(virtualColumns[leftPinnedIndexes.length - 1]?.end ?? 0);
|
110
|
-
columnVirtualizer.virtualPaddingRight =
|
111
|
-
columnVirtualizer.getTotalSize() -
|
112
|
-
(virtualColumns[virtualColumns.length - rightPinnedIndexes.length - 1]
|
113
|
-
?.end ?? 0) -
|
114
|
-
(rightPinnedIndexes.length
|
115
|
-
? columnVirtualizer.getTotalSize() -
|
116
|
-
(virtualColumns[virtualColumns.length - rightPinnedIndexes.length]
|
117
|
-
?.start ?? 0)
|
118
|
-
: 0);
|
119
|
-
}
|
120
|
-
if (columnVirtualizerInstanceRef) {
|
121
|
-
//@ts-ignore
|
122
|
-
columnVirtualizerInstanceRef.current = columnVirtualizer;
|
123
|
-
}
|
70
|
+
const columnVirtualizer = useVirtualizer({
|
71
|
+
count: visibleColumns.length,
|
72
|
+
estimateSize: (index) => visibleColumns[index].getSize(),
|
73
|
+
getScrollElement: () => tableContainerRef.current,
|
74
|
+
horizontal: true,
|
75
|
+
overscan: 3,
|
76
|
+
rangeExtractor: useCallback(
|
77
|
+
(range: Range) => {
|
78
|
+
const newIndexes = extraIndexRangeExtractor(range, draggingColumnIndex);
|
79
|
+
if (!numPinnedLeft && !numPinnedRight) {
|
80
|
+
return newIndexes;
|
81
|
+
}
|
82
|
+
return [
|
83
|
+
...new Set([
|
84
|
+
...leftPinnedIndexes,
|
85
|
+
...newIndexes,
|
86
|
+
...rightPinnedIndexes,
|
87
|
+
]),
|
88
|
+
];
|
89
|
+
},
|
90
|
+
[leftPinnedIndexes, rightPinnedIndexes, draggingColumnIndex],
|
91
|
+
),
|
92
|
+
...columnVirtualizerProps,
|
93
|
+
}) as unknown as MRT_ColumnVirtualizer<TScrollElement, TItemElement>;
|
94
|
+
|
95
|
+
const virtualColumns = columnVirtualizer.getVirtualItems();
|
96
|
+
columnVirtualizer.virtualColumns = virtualColumns;
|
97
|
+
const numColumns = virtualColumns.length;
|
98
|
+
|
99
|
+
if (numColumns) {
|
100
|
+
const totalSize = columnVirtualizer.getTotalSize();
|
101
|
+
|
102
|
+
const leftNonPinnedStart = virtualColumns[numPinnedLeft]?.start || 0;
|
103
|
+
const leftNonPinnedEnd =
|
104
|
+
virtualColumns[leftPinnedIndexes.length - 1]?.end || 0;
|
105
|
+
|
106
|
+
const rightNonPinnedStart =
|
107
|
+
virtualColumns[numColumns - numPinnedRight]?.start || 0;
|
108
|
+
const rightNonPinnedEnd =
|
109
|
+
virtualColumns[numColumns - numPinnedRight - 1]?.end || 0;
|
110
|
+
|
111
|
+
columnVirtualizer.virtualPaddingLeft =
|
112
|
+
leftNonPinnedStart - leftNonPinnedEnd;
|
113
|
+
|
114
|
+
columnVirtualizer.virtualPaddingRight =
|
115
|
+
totalSize -
|
116
|
+
rightNonPinnedEnd -
|
117
|
+
(numPinnedRight ? totalSize - rightNonPinnedStart : 0);
|
118
|
+
}
|
119
|
+
|
120
|
+
if (columnVirtualizerInstanceRef) {
|
121
|
+
//@ts-ignore
|
122
|
+
columnVirtualizerInstanceRef.current = columnVirtualizer;
|
124
123
|
}
|
125
124
|
|
126
125
|
return columnVirtualizer as any;
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { useEffect, useReducer, useRef } from 'react';
|
2
|
-
import { getCanRankRows } from '../column.utils';
|
3
2
|
import {
|
4
3
|
type MRT_RowData,
|
5
4
|
type MRT_SortingState,
|
6
5
|
type MRT_TableInstance,
|
7
6
|
} from '../types';
|
7
|
+
import { getCanRankRows } from '../utils/row.utils';
|
8
8
|
|
9
9
|
export const useMRT_Effects = <TData extends MRT_RowData>(
|
10
10
|
table: MRT_TableInstance<TData>,
|
@@ -1,15 +1,13 @@
|
|
1
1
|
import { useCallback } from 'react';
|
2
2
|
import { type Range, useVirtualizer } from '@tanstack/react-virtual';
|
3
|
-
import {
|
4
|
-
extraIndexRangeExtractor,
|
5
|
-
parseFromValuesOrFunc,
|
6
|
-
} from '../column.utils';
|
7
3
|
import {
|
8
4
|
type MRT_Row,
|
9
5
|
type MRT_RowData,
|
10
6
|
type MRT_RowVirtualizer,
|
11
7
|
type MRT_TableInstance,
|
12
8
|
} from '../types';
|
9
|
+
import { parseFromValuesOrFunc } from '../utils/utils';
|
10
|
+
import { extraIndexRangeExtractor } from '../utils/virtualization.utils';
|
13
11
|
|
14
12
|
export const useMRT_RowVirtualizer = <
|
15
13
|
TData extends MRT_RowData,
|
@@ -32,6 +30,8 @@ export const useMRT_RowVirtualizer = <
|
|
32
30
|
} = table;
|
33
31
|
const { density, draggingRow, expanded } = getState();
|
34
32
|
|
33
|
+
if (!enableRowVirtualization) return undefined;
|
34
|
+
|
35
35
|
const rowVirtualizerProps = parseFromValuesOrFunc(rowVirtualizerOptions, {
|
36
36
|
table,
|
37
37
|
});
|
@@ -41,39 +41,35 @@ export const useMRT_RowVirtualizer = <
|
|
41
41
|
const normalRowHeight =
|
42
42
|
density === 'compact' ? 37 : density === 'comfortable' ? 58 : 73;
|
43
43
|
|
44
|
-
const rowVirtualizer =
|
45
|
-
?
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
44
|
+
const rowVirtualizer = useVirtualizer({
|
45
|
+
count: renderDetailPanel ? rowCount * 2 : rowCount,
|
46
|
+
estimateSize: (index) =>
|
47
|
+
renderDetailPanel && index % 2 === 1
|
48
|
+
? expanded === true
|
49
|
+
? 100
|
50
|
+
: 0
|
51
|
+
: normalRowHeight,
|
52
|
+
getScrollElement: () => tableContainerRef.current,
|
53
|
+
measureElement:
|
54
|
+
typeof window !== 'undefined' &&
|
55
|
+
navigator.userAgent.indexOf('Firefox') === -1
|
56
|
+
? (element) => element?.getBoundingClientRect().height
|
57
|
+
: undefined,
|
58
|
+
overscan: 4,
|
59
|
+
rangeExtractor: useCallback(
|
60
|
+
(range: Range) => {
|
61
|
+
return extraIndexRangeExtractor(range, draggingRow?.index ?? 0);
|
62
|
+
},
|
63
|
+
[draggingRow],
|
64
|
+
),
|
65
|
+
...rowVirtualizerProps,
|
66
|
+
}) as unknown as MRT_RowVirtualizer<TScrollElement, TItemElement>;
|
67
|
+
|
68
|
+
rowVirtualizer.virtualRows = rowVirtualizer.getVirtualItems();
|
69
69
|
|
70
|
-
if (
|
71
|
-
|
72
|
-
|
73
|
-
if (rowVirtualizerInstanceRef) {
|
74
|
-
//@ts-ignore
|
75
|
-
rowVirtualizerInstanceRef.current = rowVirtualizer;
|
76
|
-
}
|
70
|
+
if (rowVirtualizerInstanceRef) {
|
71
|
+
//@ts-ignore
|
72
|
+
rowVirtualizerInstanceRef.current = rowVirtualizer;
|
77
73
|
}
|
78
74
|
|
79
75
|
return rowVirtualizer;
|
package/src/hooks/useMRT_Rows.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import { useMemo } from 'react';
|
2
|
-
import {
|
3
|
-
import { rankGlobalFuzzy } from '../sortingFns';
|
2
|
+
import { rankGlobalFuzzy } from '../fns/sortingFns';
|
4
3
|
import {
|
5
4
|
type MRT_Row,
|
6
5
|
type MRT_RowData,
|
7
6
|
type MRT_TableInstance,
|
8
7
|
} from '../types';
|
8
|
+
import { getCanRankRows } from '../utils/row.utils';
|
9
9
|
|
10
10
|
export const useMRT_Rows = <TData extends MRT_RowData>(
|
11
11
|
table: MRT_TableInstance<TData>,
|
@@ -11,20 +11,13 @@ import {
|
|
11
11
|
getSortedRowModel,
|
12
12
|
useReactTable,
|
13
13
|
} from '@tanstack/react-table';
|
14
|
-
import {
|
15
|
-
createRow,
|
16
|
-
getAllLeafColumnDefs,
|
17
|
-
getColumnId,
|
18
|
-
getDefaultColumnFilterFn,
|
19
|
-
getDefaultColumnOrderIds,
|
20
|
-
prepareColumns,
|
21
|
-
} from '../column.utils';
|
22
14
|
import {
|
23
15
|
type MRT_Cell,
|
24
16
|
type MRT_Column,
|
25
17
|
type MRT_ColumnDef,
|
26
18
|
type MRT_ColumnFilterFnsState,
|
27
19
|
type MRT_ColumnOrderState,
|
20
|
+
type MRT_ColumnSizingInfoState,
|
28
21
|
type MRT_DefinedTableOptions,
|
29
22
|
type MRT_DensityState,
|
30
23
|
type MRT_FilterOption,
|
@@ -32,18 +25,25 @@ import {
|
|
32
25
|
type MRT_PaginationState,
|
33
26
|
type MRT_Row,
|
34
27
|
type MRT_RowData,
|
28
|
+
type MRT_StatefulTableOptions,
|
35
29
|
type MRT_TableInstance,
|
36
30
|
type MRT_TableState,
|
37
31
|
type MRT_Updater,
|
38
32
|
} from '../types';
|
39
|
-
import {
|
33
|
+
import {
|
34
|
+
getAllLeafColumnDefs,
|
35
|
+
getColumnId,
|
36
|
+
getDefaultColumnFilterFn,
|
37
|
+
prepareColumns,
|
38
|
+
} from '../utils/column.utils';
|
39
|
+
import { getDefaultColumnOrderIds } from '../utils/displayColumn.utils';
|
40
|
+
import { createRow } from '../utils/tanstack.helpers';
|
41
|
+
import { getMRT_DisplayColumns } from './display-columns/getMRT_DisplayColumns';
|
40
42
|
import { useMRT_Effects } from './useMRT_Effects';
|
41
43
|
|
42
|
-
export const useMRT_TableInstance
|
43
|
-
|
44
|
-
)
|
45
|
-
tableOptions: MRT_DefinedTableOptions<TData>,
|
46
|
-
) => {
|
44
|
+
export const useMRT_TableInstance = <TData extends MRT_RowData>(
|
45
|
+
_tableOptions: MRT_DefinedTableOptions<TData>,
|
46
|
+
): MRT_TableInstance<TData> => {
|
47
47
|
const bottomToolbarRef = useRef<HTMLDivElement>(null);
|
48
48
|
const editInputRefs = useRef<Record<string, HTMLInputElement>>({});
|
49
49
|
const filterInputRefs = useRef<Record<string, HTMLInputElement>>({});
|
@@ -56,14 +56,18 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
56
56
|
const tableFooterRef = useRef<HTMLTableSectionElement>(null);
|
57
57
|
|
58
58
|
const initialState: Partial<MRT_TableState<TData>> = useMemo(() => {
|
59
|
-
const initState =
|
59
|
+
const initState = _tableOptions.initialState ?? {};
|
60
60
|
initState.columnOrder =
|
61
|
-
initState.columnOrder ??
|
62
|
-
|
61
|
+
initState.columnOrder ??
|
62
|
+
getDefaultColumnOrderIds({
|
63
|
+
..._tableOptions,
|
64
|
+
state: { ..._tableOptions.initialState, ..._tableOptions.state },
|
65
|
+
} as MRT_StatefulTableOptions<TData>);
|
66
|
+
initState.globalFilterFn = _tableOptions.globalFilterFn ?? 'fuzzy';
|
63
67
|
return initState;
|
64
68
|
}, []);
|
65
69
|
|
66
|
-
|
70
|
+
_tableOptions.initialState = initialState;
|
67
71
|
|
68
72
|
const [creatingRow, _setCreatingRow] = useState<MRT_Row<TData> | null>(
|
69
73
|
initialState.creatingRow ?? null,
|
@@ -73,7 +77,7 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
73
77
|
Object.assign(
|
74
78
|
{},
|
75
79
|
...getAllLeafColumnDefs(
|
76
|
-
|
80
|
+
_tableOptions.columns as MRT_ColumnDef<TData>[],
|
77
81
|
).map((col) => ({
|
78
82
|
[getColumnId(col)]:
|
79
83
|
col.filterFn instanceof Function
|
@@ -87,6 +91,10 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
87
91
|
const [columnOrder, onColumnOrderChange] = useState<MRT_ColumnOrderState>(
|
88
92
|
initialState.columnOrder ?? [],
|
89
93
|
);
|
94
|
+
const [columnSizingInfo, onColumnSizingInfoChange] =
|
95
|
+
useState<MRT_ColumnSizingInfoState>(
|
96
|
+
initialState.columnSizingInfo ?? ({} as MRT_ColumnSizingInfoState),
|
97
|
+
);
|
90
98
|
const [density, setDensity] = useState<MRT_DensityState>(
|
91
99
|
initialState?.density ?? 'comfortable',
|
92
100
|
);
|
@@ -120,7 +128,7 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
120
128
|
initialState?.pagination ?? { pageIndex: 0, pageSize: 10 },
|
121
129
|
);
|
122
130
|
const [showAlertBanner, setShowAlertBanner] = useState<boolean>(
|
123
|
-
|
131
|
+
initialState?.showAlertBanner ?? false,
|
124
132
|
);
|
125
133
|
const [showColumnFilters, setShowColumnFilters] = useState<boolean>(
|
126
134
|
initialState?.showColumnFilters ?? false,
|
@@ -132,9 +140,10 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
132
140
|
initialState?.showToolbarDropZone ?? false,
|
133
141
|
);
|
134
142
|
|
135
|
-
|
143
|
+
_tableOptions.state = {
|
136
144
|
columnFilterFns,
|
137
145
|
columnOrder,
|
146
|
+
columnSizingInfo,
|
138
147
|
creatingRow,
|
139
148
|
density,
|
140
149
|
draggingColumn,
|
@@ -151,30 +160,35 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
151
160
|
showColumnFilters,
|
152
161
|
showGlobalFilter,
|
153
162
|
showToolbarDropZone,
|
154
|
-
...
|
163
|
+
..._tableOptions.state,
|
155
164
|
};
|
156
165
|
|
157
|
-
const
|
166
|
+
const tableOptions = _tableOptions as MRT_StatefulTableOptions<TData>;
|
158
167
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
168
|
+
//don't recompute columnDefs while resizing column or dragging column/row
|
169
|
+
const columnDefsRef = useRef<MRT_ColumnDef<TData>[]>([]);
|
170
|
+
tableOptions.columns =
|
171
|
+
tableOptions.state.columnSizingInfo.isResizingColumn ||
|
172
|
+
tableOptions.state.draggingColumn ||
|
173
|
+
tableOptions.state.draggingRow
|
174
|
+
? columnDefsRef.current
|
175
|
+
: prepareColumns({
|
176
|
+
columnDefs: [
|
177
|
+
...getMRT_DisplayColumns(tableOptions),
|
178
|
+
...tableOptions.columns,
|
179
|
+
],
|
180
|
+
tableOptions,
|
181
|
+
});
|
182
|
+
columnDefsRef.current = tableOptions.columns;
|
167
183
|
|
168
184
|
tableOptions.data = useMemo(
|
169
185
|
() =>
|
170
|
-
(tableOptions.state
|
186
|
+
(tableOptions.state.isLoading || tableOptions.state.showSkeletons) &&
|
171
187
|
!tableOptions.data.length
|
172
188
|
? [
|
173
|
-
...Array(
|
174
|
-
|
175
|
-
|
176
|
-
10,
|
177
|
-
).fill(null),
|
189
|
+
...Array(Math.min(tableOptions.state.pagination.pageSize, 20)).fill(
|
190
|
+
null,
|
191
|
+
),
|
178
192
|
].map(() =>
|
179
193
|
Object.assign(
|
180
194
|
{},
|
@@ -186,8 +200,8 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
186
200
|
: tableOptions.data,
|
187
201
|
[
|
188
202
|
tableOptions.data,
|
189
|
-
tableOptions.state
|
190
|
-
tableOptions.state
|
203
|
+
tableOptions.state.isLoading,
|
204
|
+
tableOptions.state.showSkeletons,
|
191
205
|
],
|
192
206
|
);
|
193
207
|
|
@@ -224,6 +238,7 @@ export const useMRT_TableInstance: <TData extends MRT_RowData>(
|
|
224
238
|
: undefined,
|
225
239
|
getSubRows: (row) => row?.subRows,
|
226
240
|
onColumnOrderChange,
|
241
|
+
onColumnSizingInfoChange,
|
227
242
|
onGroupingChange,
|
228
243
|
onPaginationChange,
|
229
244
|
...tableOptions,
|