material-react-table 2.8.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.
Files changed (34) hide show
  1. package/dist/index.d.ts +17 -9
  2. package/dist/index.esm.js +250 -198
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.js +250 -197
  5. package/dist/index.js.map +1 -1
  6. package/locales/ja/index.esm.js +4 -4
  7. package/locales/ja/index.js +4 -4
  8. package/package.json +6 -4
  9. package/src/components/body/MRT_TableBody.tsx +2 -3
  10. package/src/components/body/MRT_TableBodyCell.tsx +10 -3
  11. package/src/components/body/MRT_TableBodyRow.tsx +77 -57
  12. package/src/components/footer/MRT_TableFooterCell.tsx +10 -1
  13. package/src/components/footer/MRT_TableFooterRow.tsx +19 -8
  14. package/src/components/head/MRT_TableHeadCell.tsx +10 -0
  15. package/src/components/head/MRT_TableHeadCellResizeHandle.tsx +4 -2
  16. package/src/components/head/MRT_TableHeadRow.tsx +19 -8
  17. package/src/components/inputs/MRT_EditCellTextField.tsx +4 -3
  18. package/src/components/inputs/MRT_SelectCheckbox.tsx +4 -6
  19. package/src/components/table/MRT_Table.tsx +4 -0
  20. package/src/components/toolbar/MRT_ToolbarAlertBanner.tsx +1 -1
  21. package/src/hooks/display-columns/getMRT_RowActionsColumnDef.tsx +1 -0
  22. package/src/hooks/display-columns/getMRT_RowDragColumnDef.tsx +1 -0
  23. package/src/hooks/display-columns/getMRT_RowExpandColumnDef.tsx +4 -2
  24. package/src/hooks/display-columns/getMRT_RowNumbersColumnDef.tsx +1 -0
  25. package/src/hooks/display-columns/getMRT_RowPinningColumnDef.tsx +1 -0
  26. package/src/hooks/display-columns/getMRT_RowSelectColumnDef.tsx +2 -1
  27. package/src/hooks/useMRT_ColumnVirtualizer.ts +77 -76
  28. package/src/hooks/useMRT_RowVirtualizer.ts +30 -32
  29. package/src/hooks/useMRT_TableOptions.ts +27 -17
  30. package/src/locales/ja.ts +4 -4
  31. package/src/types.ts +3 -0
  32. package/src/utils/displayColumn.utils.ts +2 -2
  33. package/src/utils/row.utils.ts +22 -1
  34. package/src/utils/style.utils.ts +70 -30
@@ -86,9 +86,11 @@ export const getMRT_RowExpandColumnDef = <TData extends MRT_RowData>(
86
86
  id: 'mrt-row-expand',
87
87
  size:
88
88
  groupedColumnMode === 'remove'
89
- ? defaultColumn?.size
89
+ ? defaultColumn?.size ?? 180
90
90
  : renderDetailPanel
91
- ? 60
91
+ ? enableExpandAll
92
+ ? 60
93
+ : 70
92
94
  : 100,
93
95
  tableOptions,
94
96
  }),
@@ -30,6 +30,7 @@ export const getMRT_RowNumbersColumnDef = <TData extends MRT_RowData>(
30
30
  ...defaultDisplayColumnProps({
31
31
  header: 'rowNumbers',
32
32
  id: 'mrt-row-numbers',
33
+ size: 50,
33
34
  tableOptions,
34
35
  }),
35
36
  };
@@ -24,6 +24,7 @@ export const getMRT_RowPinningColumnDef = <TData extends MRT_RowData>(
24
24
  ...defaultDisplayColumnProps({
25
25
  header: 'pin',
26
26
  id: 'mrt-row-pin',
27
+ size: 60,
27
28
  tableOptions,
28
29
  }),
29
30
  };
@@ -28,12 +28,13 @@ export const getMRT_RowSelectColumnDef = <TData extends MRT_RowData>(
28
28
  ),
29
29
  Header:
30
30
  enableSelectAll && enableMultiRowSelection
31
- ? ({ table }) => <MRT_SelectCheckbox selectAll table={table} />
31
+ ? ({ table }) => <MRT_SelectCheckbox table={table} />
32
32
  : undefined,
33
33
  grow: false,
34
34
  ...defaultDisplayColumnProps({
35
35
  header: 'select',
36
36
  id: 'mrt-row-select',
37
+ size: enableSelectAll ? 60 : 70,
37
38
  tableOptions,
38
39
  }),
39
40
  };
@@ -16,7 +16,10 @@ export const useMRT_ColumnVirtualizer = <
16
16
  table: MRT_TableInstance<TData>,
17
17
  ): MRT_ColumnVirtualizer | undefined => {
18
18
  const {
19
+ getLeftLeafColumns,
20
+ getRightLeafColumns,
19
21
  getState,
22
+ getVisibleLeafColumns,
20
23
  options: {
21
24
  columnVirtualizerInstanceRef,
22
25
  columnVirtualizerOptions,
@@ -25,7 +28,9 @@ export const useMRT_ColumnVirtualizer = <
25
28
  },
26
29
  refs: { tableContainerRef },
27
30
  } = table;
28
- const { columnPinning, columnVisibility, draggingColumn } = getState();
31
+ const { columnPinning, draggingColumn } = getState();
32
+
33
+ if (!enableColumnVirtualization) return undefined;
29
34
 
30
35
  const columnVirtualizerProps = parseFromValuesOrFunc(
31
36
  columnVirtualizerOptions,
@@ -34,91 +39,87 @@ export const useMRT_ColumnVirtualizer = <
34
39
  },
35
40
  );
36
41
 
42
+ const visibleColumns = getVisibleLeafColumns();
43
+
37
44
  const [leftPinnedIndexes, rightPinnedIndexes] = useMemo(
38
45
  () =>
39
- enableColumnVirtualization && enableColumnPinning
46
+ enableColumnPinning
40
47
  ? [
41
- table.getLeftLeafColumns().map((c) => c.getPinnedIndex()),
42
- table
43
- .getRightLeafColumns()
48
+ getLeftLeafColumns().map((c) => c.getPinnedIndex()),
49
+ getRightLeafColumns()
44
50
  .map(
45
- (c) =>
46
- table.getVisibleLeafColumns().length - c.getPinnedIndex() - 1,
51
+ (column) => visibleColumns.length - column.getPinnedIndex() - 1,
47
52
  )
48
53
  .sort((a, b) => a - b),
49
54
  ]
50
55
  : [[], []],
51
- [columnPinning, enableColumnVirtualization, enableColumnPinning],
56
+ [columnPinning, enableColumnPinning],
52
57
  );
53
58
 
54
- //get first 16 column widths and average them if calc is needed
55
- const averageColumnWidth = useMemo(() => {
56
- if (!enableColumnVirtualization || columnVirtualizerProps?.estimateSize) {
57
- return 0;
58
- }
59
- const columnsWidths =
60
- table
61
- .getRowModel()
62
- .rows[0]?.getCenterVisibleCells()
63
- ?.slice(0, 16)
64
- ?.map((cell) => cell.column.getSize() * 1.2) ?? [];
65
- return columnsWidths.reduce((a, b) => a + b, 0) / columnsWidths.length;
66
- }, [table.getRowModel().rows, columnPinning, columnVisibility]);
67
-
68
- const draggingColumnIndex = draggingColumn?.id
69
- ? table
70
- .getVisibleLeafColumns()
71
- .findIndex((c) => c.id === draggingColumn?.id)
72
- : undefined;
73
-
74
- const columnVirtualizer = enableColumnVirtualization
75
- ? (useVirtualizer({
76
- count: table.getVisibleLeafColumns().length,
77
- estimateSize: () => averageColumnWidth,
78
- getScrollElement: () => tableContainerRef.current,
79
- horizontal: true,
80
- overscan: 3,
81
- rangeExtractor: useCallback(
82
- (range: Range) => {
83
- const newIndexes = extraIndexRangeExtractor(
84
- range,
85
- draggingColumnIndex,
86
- );
87
- return [
88
- ...new Set([
89
- ...leftPinnedIndexes,
90
- ...newIndexes,
91
- ...rightPinnedIndexes,
92
- ]),
93
- ];
94
- },
95
- [leftPinnedIndexes, rightPinnedIndexes, draggingColumnIndex],
96
- ),
97
- ...columnVirtualizerProps,
98
- }) as unknown as MRT_ColumnVirtualizer<TScrollElement, TItemElement>)
99
- : undefined;
100
-
101
- if (columnVirtualizer) {
102
- const virtualColumns = columnVirtualizer.getVirtualItems();
103
- columnVirtualizer.virtualColumns = virtualColumns;
104
- if (virtualColumns.length) {
105
- columnVirtualizer.virtualPaddingLeft =
106
- (virtualColumns[leftPinnedIndexes.length]?.start ?? 0) -
107
- (virtualColumns[leftPinnedIndexes.length - 1]?.end ?? 0);
108
- columnVirtualizer.virtualPaddingRight =
109
- columnVirtualizer.getTotalSize() -
110
- (virtualColumns[virtualColumns.length - rightPinnedIndexes.length - 1]
111
- ?.end ?? 0) -
112
- (rightPinnedIndexes.length
113
- ? columnVirtualizer.getTotalSize() -
114
- (virtualColumns[virtualColumns.length - rightPinnedIndexes.length]
115
- ?.start ?? 0)
116
- : 0);
117
- }
118
- if (columnVirtualizerInstanceRef) {
119
- //@ts-ignore
120
- columnVirtualizerInstanceRef.current = columnVirtualizer;
121
- }
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],
68
+ );
69
+
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;
122
123
  }
123
124
 
124
125
  return columnVirtualizer as any;
@@ -30,6 +30,8 @@ export const useMRT_RowVirtualizer = <
30
30
  } = table;
31
31
  const { density, draggingRow, expanded } = getState();
32
32
 
33
+ if (!enableRowVirtualization) return undefined;
34
+
33
35
  const rowVirtualizerProps = parseFromValuesOrFunc(rowVirtualizerOptions, {
34
36
  table,
35
37
  });
@@ -39,39 +41,35 @@ export const useMRT_RowVirtualizer = <
39
41
  const normalRowHeight =
40
42
  density === 'compact' ? 37 : density === 'comfortable' ? 58 : 73;
41
43
 
42
- const rowVirtualizer = enableRowVirtualization
43
- ? (useVirtualizer({
44
- count: renderDetailPanel ? rowCount * 2 : rowCount,
45
- estimateSize: (index) =>
46
- renderDetailPanel && index % 2 === 1
47
- ? expanded === true
48
- ? 100
49
- : 0
50
- : normalRowHeight,
51
- getScrollElement: () => tableContainerRef.current,
52
- measureElement:
53
- typeof window !== 'undefined' &&
54
- navigator.userAgent.indexOf('Firefox') === -1
55
- ? (element) => element?.getBoundingClientRect().height
56
- : undefined,
57
- overscan: 4,
58
- rangeExtractor: useCallback(
59
- (range: Range) => {
60
- return extraIndexRangeExtractor(range, draggingRow?.index ?? 0);
61
- },
62
- [draggingRow],
63
- ),
64
- ...rowVirtualizerProps,
65
- }) as unknown as MRT_RowVirtualizer<TScrollElement, TItemElement>)
66
- : undefined;
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();
67
69
 
68
- if (rowVirtualizer) {
69
- const virtualRows = rowVirtualizer.getVirtualItems();
70
- rowVirtualizer.virtualRows = virtualRows;
71
- if (rowVirtualizerInstanceRef) {
72
- //@ts-ignore
73
- rowVirtualizerInstanceRef.current = rowVirtualizer;
74
- }
70
+ if (rowVirtualizerInstanceRef) {
71
+ //@ts-ignore
72
+ rowVirtualizerInstanceRef.current = rowVirtualizer;
75
73
  }
76
74
 
77
75
  return rowVirtualizer;
@@ -51,6 +51,7 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
51
51
  enableColumnOrdering = false,
52
52
  enableColumnPinning = false,
53
53
  enableColumnResizing = false,
54
+ enableColumnVirtualization,
54
55
  enableDensityToggle = true,
55
56
  enableExpandAll = true,
56
57
  enableExpanding,
@@ -66,6 +67,7 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
66
67
  enablePagination = true,
67
68
  enableRowPinning = false,
68
69
  enableRowSelection = false,
70
+ enableRowVirtualization,
69
71
  enableSelectAll = true,
70
72
  enableSorting = true,
71
73
  enableStickyHeader = false,
@@ -96,31 +98,37 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
96
98
  ...rest
97
99
  }: MRT_TableOptions<TData>) => {
98
100
  const theme = useTheme();
99
- const _icons = useMemo(() => ({ ...MRT_Default_Icons, ...icons }), [icons]);
100
- const _localization = useMemo(
101
+
102
+ icons = useMemo(() => ({ ...MRT_Default_Icons, ...icons }), [icons]);
103
+ localization = useMemo(
101
104
  () => ({
102
105
  ...MRT_Localization_EN,
103
106
  ...localization,
104
107
  }),
105
108
  [localization],
106
109
  );
107
- const _aggregationFns = useMemo(
110
+ aggregationFns = useMemo(
108
111
  () => ({ ...MRT_AggregationFns, ...aggregationFns }),
109
112
  [],
110
113
  );
111
- const _filterFns = useMemo(() => ({ ...MRT_FilterFns, ...filterFns }), []);
112
- const _sortingFns = useMemo(() => ({ ...MRT_SortingFns, ...sortingFns }), []);
113
- const _defaultColumn = useMemo(
114
+ filterFns = useMemo(() => ({ ...MRT_FilterFns, ...filterFns }), []);
115
+ sortingFns = useMemo(() => ({ ...MRT_SortingFns, ...sortingFns }), []);
116
+ defaultColumn = useMemo(
114
117
  () => ({ ...MRT_DefaultColumn, ...defaultColumn }),
115
118
  [defaultColumn],
116
119
  );
117
- const _defaultDisplayColumn = useMemo(
120
+ defaultDisplayColumn = useMemo(
118
121
  () => ({
119
122
  ...MRT_DefaultDisplayColumn,
120
123
  ...defaultDisplayColumn,
121
124
  }),
122
125
  [defaultDisplayColumn],
123
126
  );
127
+ //cannot be changed after initialization
128
+ [enableColumnVirtualization, enableRowVirtualization] = useMemo(
129
+ () => [enableColumnVirtualization, enableRowVirtualization],
130
+ [],
131
+ );
124
132
 
125
133
  if (!columnResizeDirection) {
126
134
  columnResizeDirection = theme.direction || 'ltr';
@@ -130,12 +138,12 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
130
138
  layoutMode || (enableColumnResizing ? 'grid-no-grow' : 'semantic');
131
139
  if (
132
140
  layoutMode === 'semantic' &&
133
- (rest.enableRowVirtualization || rest.enableColumnVirtualization)
141
+ (enableRowVirtualization || enableColumnVirtualization)
134
142
  ) {
135
143
  layoutMode = 'grid';
136
144
  }
137
145
 
138
- if (rest.enableRowVirtualization) {
146
+ if (enableRowVirtualization) {
139
147
  enableStickyHeader = true;
140
148
  }
141
149
 
@@ -151,14 +159,14 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
151
159
  }
152
160
 
153
161
  return {
154
- aggregationFns: _aggregationFns,
162
+ aggregationFns,
155
163
  autoResetExpanded,
156
164
  columnFilterDisplayMode,
157
165
  columnResizeDirection,
158
166
  columnResizeMode,
159
167
  createDisplayMode,
160
- defaultColumn: _defaultColumn,
161
- defaultDisplayColumn: _defaultDisplayColumn,
168
+ defaultColumn,
169
+ defaultDisplayColumn,
162
170
  editDisplayMode,
163
171
  enableBottomToolbar,
164
172
  enableColumnActions,
@@ -166,6 +174,7 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
166
174
  enableColumnOrdering,
167
175
  enableColumnPinning,
168
176
  enableColumnResizing,
177
+ enableColumnVirtualization,
169
178
  enableDensityToggle,
170
179
  enableExpandAll,
171
180
  enableExpanding,
@@ -181,6 +190,7 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
181
190
  enablePagination,
182
191
  enableRowPinning,
183
192
  enableRowSelection,
193
+ enableRowVirtualization,
184
194
  enableSelectAll,
185
195
  enableSorting,
186
196
  enableStickyHeader,
@@ -188,10 +198,10 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
188
198
  enableTableHead,
189
199
  enableToolbarInternalActions,
190
200
  enableTopToolbar,
191
- filterFns: _filterFns,
192
- icons: _icons,
201
+ filterFns,
202
+ icons,
193
203
  layoutMode,
194
- localization: _localization,
204
+ localization,
195
205
  manualFiltering,
196
206
  manualGrouping,
197
207
  manualPagination,
@@ -207,7 +217,7 @@ export const useMRT_TableOptions: <TData extends MRT_RowData>(
207
217
  rowNumberDisplayMode,
208
218
  rowPinningDisplayMode,
209
219
  selectAllMode,
210
- sortingFns: _sortingFns,
220
+ sortingFns,
211
221
  ...rest,
212
- };
222
+ } as MRT_DefinedTableOptions<TData>;
213
223
  };
package/src/locales/ja.ts CHANGED
@@ -78,10 +78,10 @@ export const MRT_Localization_JA: MRT_Localization = {
78
78
  showHideColumns: '列の表示状態',
79
79
  showHideFilters: '検索バーを表示',
80
80
  showHideSearch: '検索',
81
- sortByColumnAsc: '{column}を昇順でを並べ替え',
82
- sortByColumnDesc: '{column}を降順でを並べ替え',
83
- sortedByColumnAsc: '{column}を昇順でを並べ替え',
84
- sortedByColumnDesc: '{column}を降順でを並べ替え',
81
+ sortByColumnAsc: '{column}を昇順で並べ替え',
82
+ sortByColumnDesc: '{column}を降順で並べ替え',
83
+ sortedByColumnAsc: '{column}を昇順で並べ替え',
84
+ sortedByColumnDesc: '{column}を降順で並べ替え',
85
85
  thenBy: 'さらに',
86
86
  toggleDensity: 'テーブルの高さを変更',
87
87
  toggleFullScreen: 'フルスクリーン切り替え',
package/src/types.ts CHANGED
@@ -1164,6 +1164,9 @@ export type MRT_TableOptions<TData extends MRT_RowData> = Omit<
1164
1164
  renderBottomToolbarCustomActions?: (props: {
1165
1165
  table: MRT_TableInstance<TData>;
1166
1166
  }) => ReactNode;
1167
+ renderCaption?:
1168
+ | ((props: { table: MRT_TableInstance<TData> }) => ReactNode)
1169
+ | ReactNode;
1167
1170
  renderColumnActionsMenuItems?: (props: {
1168
1171
  closeMenu: () => void;
1169
1172
  column: MRT_Column<TData>;
@@ -10,12 +10,12 @@ import { getAllLeafColumnDefs, getColumnId } from './column.utils';
10
10
  export function defaultDisplayColumnProps<TData extends MRT_RowData>({
11
11
  header,
12
12
  id,
13
- size = 60,
13
+ size,
14
14
  tableOptions,
15
15
  }: {
16
16
  header?: keyof MRT_Localization;
17
17
  id: MRT_DisplayColumnIds;
18
- size?: number;
18
+ size: number;
19
19
  tableOptions: MRT_DefinedTableOptions<TData>;
20
20
  }) {
21
21
  const { defaultDisplayColumn, displayColumnDefOptions, localization } =
@@ -1,4 +1,25 @@
1
- import { type MRT_RowData, type MRT_TableInstance } from '../types';
1
+ import {
2
+ type MRT_Row,
3
+ type MRT_RowData,
4
+ type MRT_TableInstance,
5
+ } from '../types';
6
+ import { parseFromValuesOrFunc } from './utils';
7
+
8
+ export const getIsRowSelected = <TData extends MRT_RowData>({
9
+ row,
10
+ table,
11
+ }: {
12
+ row: MRT_Row<TData>;
13
+ table: MRT_TableInstance<TData>;
14
+ }) => {
15
+ const { options: enableRowSelection } = table;
16
+ return (
17
+ row.getIsSelected() ||
18
+ (parseFromValuesOrFunc(enableRowSelection, row) &&
19
+ row.getCanSelectSubRows() &&
20
+ row.getIsAllSubRowsSelected())
21
+ );
22
+ };
2
23
 
3
24
  export const getCanRankRows = <TData extends MRT_RowData>(
4
25
  table: MRT_TableInstance<TData>,
@@ -42,6 +42,49 @@ export const getMRTTheme = <TData extends MRT_RowData>(
42
42
  };
43
43
  };
44
44
 
45
+ export const pinnedBeforeAfterStyles = {
46
+ content: '""',
47
+ height: '100%',
48
+ left: 0,
49
+ position: 'absolute',
50
+ top: 0,
51
+ width: '100%',
52
+ zIndex: -1,
53
+ };
54
+
55
+ export const getCommonPinnedCellStyles = <TData extends MRT_RowData>({
56
+ column,
57
+ table,
58
+ theme,
59
+ }: {
60
+ column?: MRT_Column<TData>;
61
+ table: MRT_TableInstance<TData>;
62
+ theme: Theme;
63
+ }) => {
64
+ const { baseBackgroundColor } = getMRTTheme(table, theme);
65
+ return {
66
+ '&[data-pinned="true"]': {
67
+ '&:before': {
68
+ backgroundColor: alpha(
69
+ darken(
70
+ baseBackgroundColor,
71
+ theme.palette.mode === 'dark' ? 0.05 : 0.01,
72
+ ),
73
+ 0.97,
74
+ ),
75
+ boxShadow: column
76
+ ? getIsLastLeftPinnedColumn(table, column)
77
+ ? `-4px 0 4px -4px ${alpha(theme.palette.grey[700], 0.5)} inset`
78
+ : getIsFirstRightPinnedColumn(column)
79
+ ? `4px 0 4px -4px ${alpha(theme.palette.grey[700], 0.5)} inset`
80
+ : undefined
81
+ : undefined,
82
+ ...pinnedBeforeAfterStyles,
83
+ },
84
+ },
85
+ };
86
+ };
87
+
45
88
  export const getCommonMRTCellStyles = <TData extends MRT_RowData>({
46
89
  column,
47
90
  header,
@@ -56,17 +99,20 @@ export const getCommonMRTCellStyles = <TData extends MRT_RowData>({
56
99
  theme: Theme;
57
100
  }) => {
58
101
  const {
59
- options: { layoutMode },
102
+ options: { enableColumnVirtualization, layoutMode },
60
103
  } = table;
61
104
  const { columnDef } = column;
62
105
 
106
+ const isColumnPinned =
107
+ columnDef.columnDefType !== 'group' && column.getIsPinned();
108
+
63
109
  const widthStyles: CSSProperties = {
64
110
  minWidth: `max(calc(var(--${header ? 'header' : 'col'}-${parseCSSVarId(
65
111
  header?.id ?? column.id,
66
112
  )}-size) * 1px), ${columnDef.minSize ?? 30}px)`,
67
113
  width: `calc(var(--${header ? 'header' : 'col'}-${parseCSSVarId(
68
114
  header?.id ?? column.id,
69
- )}-size) * 1px${header && layoutMode === 'grid-no-grow' ? ` + ${header?.subHeaders?.length ?? 0}rem` : ''})`,
115
+ )}-size) * 1px)`,
70
116
  };
71
117
 
72
118
  if (layoutMode === 'grid') {
@@ -81,44 +127,38 @@ export const getCommonMRTCellStyles = <TData extends MRT_RowData>({
81
127
  widthStyles.flex = `${+(columnDef.grow || 0)} 0 auto`;
82
128
  }
83
129
 
130
+ const pinnedStyles = isColumnPinned
131
+ ? {
132
+ ...getCommonPinnedCellStyles({ column, table, theme }),
133
+ left:
134
+ isColumnPinned === 'left'
135
+ ? `${column.getStart('left')}px`
136
+ : undefined,
137
+ opacity: 0.97,
138
+ position: 'sticky',
139
+ right:
140
+ isColumnPinned === 'right'
141
+ ? `${getTotalRight(table, column)}px`
142
+ : undefined,
143
+ zIndex: 1,
144
+ }
145
+ : {};
146
+
84
147
  return {
85
- backgroundColor:
86
- column.getIsPinned() && columnDef.columnDefType !== 'group'
87
- ? alpha(
88
- darken(
89
- getMRTTheme(table, theme).baseBackgroundColor,
90
- theme.palette.mode === 'dark' ? 0.05 : 0.01,
91
- ),
92
- 0.97,
93
- )
94
- : 'inherit',
148
+ backgroundColor: 'inherit',
95
149
  backgroundImage: 'inherit',
96
- boxShadow: getIsLastLeftPinnedColumn(table, column)
97
- ? `-4px 0 8px -6px ${alpha(theme.palette.grey[700], 0.5)} inset`
98
- : getIsFirstRightPinnedColumn(column)
99
- ? `4px 0 8px -6px ${alpha(theme.palette.grey[700], 0.5)} inset`
100
- : undefined,
101
150
  display: layoutMode?.startsWith('grid') ? 'flex' : undefined,
102
- left:
103
- column.getIsPinned() === 'left'
104
- ? `${column.getStart('left')}px`
105
- : undefined,
106
151
  opacity:
107
152
  table.getState().draggingColumn?.id === column.id ||
108
153
  table.getState().hoveredColumn?.id === column.id
109
154
  ? 0.5
110
155
  : 1,
111
- position:
112
- column.getIsPinned() && columnDef.columnDefType !== 'group'
113
- ? 'sticky'
114
- : undefined,
115
- right:
116
- column.getIsPinned() === 'right'
117
- ? `${getTotalRight(table, column)}px`
118
- : undefined,
119
- transition: table.options.enableColumnVirtualization
156
+ position: 'relative',
157
+ transition: enableColumnVirtualization
120
158
  ? 'none'
121
159
  : `padding 150ms ease-in-out`,
160
+ zIndex: 0,
161
+ ...pinnedStyles,
122
162
  ...widthStyles,
123
163
  ...(parseFromValuesOrFunc(tableCellProps?.sx, theme) as any),
124
164
  };