material-react-table 3.0.0-beta.1 → 3.0.0-rc.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.
@@ -10,6 +10,13 @@ import {
10
10
  } from './row.utils';
11
11
  import { parseFromValuesOrFunc } from './utils';
12
12
 
13
+ const isWinCtrlMacMeta = (event: React.KeyboardEvent<HTMLTableCellElement>) => {
14
+ return (
15
+ (event.ctrlKey && navigator.platform.toLowerCase().includes('win')) ||
16
+ (event.metaKey && navigator.platform.toLowerCase().includes('mac'))
17
+ );
18
+ };
19
+
13
20
  export const isCellEditable = <TData extends MRT_RowData>({
14
21
  cell,
15
22
  table,
@@ -54,7 +61,7 @@ export const openEditingCell = <TData extends MRT_RowData>({
54
61
  }
55
62
  };
56
63
 
57
- export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
64
+ export const cellKeyboardShortcuts = <TData extends MRT_RowData = MRT_RowData>({
58
65
  cell,
59
66
  cellElements,
60
67
  cellValue,
@@ -73,10 +80,14 @@ export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
73
80
  parentElement?: HTMLTableRowElement;
74
81
  table: MRT_TableInstance<TData>;
75
82
  }) => {
76
- if (cellValue && (event.ctrlKey || event.metaKey) && event.key === 'c') {
83
+ if (!table.options.enableKeyboardShortcuts) return;
84
+ const currentCell = event.currentTarget;
85
+
86
+ if (cellValue && isWinCtrlMacMeta(event) && event.key === 'c') {
77
87
  navigator.clipboard.writeText(cellValue);
78
88
  } else if (['Enter', ' '].includes(event.key)) {
79
89
  if (cell?.column?.id === 'mrt-row-select') {
90
+ event.preventDefault();
80
91
  getMRT_RowSelectionHandler({
81
92
  row: cell.row,
82
93
  table,
@@ -87,6 +98,7 @@ export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
87
98
  header?.column?.id === 'mrt-row-select' &&
88
99
  table.options.enableSelectAll
89
100
  ) {
101
+ event.preventDefault();
90
102
  getMRT_SelectAllHandler({
91
103
  table,
92
104
  })(event as any);
@@ -95,15 +107,16 @@ export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
95
107
  (cell.row.getCanExpand() ||
96
108
  table.options.renderDetailPanel?.({ row: cell.row, table }))
97
109
  ) {
110
+ event.preventDefault();
98
111
  cell.row.toggleExpanded();
99
112
  } else if (
100
113
  header?.column?.id === 'mrt-row-expand' &&
101
114
  table.options.enableExpandAll
102
115
  ) {
116
+ event.preventDefault();
103
117
  table.toggleAllRowsExpanded();
104
- } else if (header?.column?.getCanSort()) {
105
- header.column.toggleSorting();
106
118
  } else if (cell?.column.id === 'mrt-row-pin') {
119
+ event.preventDefault();
107
120
  cell.row.getIsPinned()
108
121
  ? cell.row.pin(false)
109
122
  : cell.row.pin(
@@ -111,16 +124,32 @@ export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
111
124
  ? 'bottom'
112
125
  : 'top',
113
126
  );
127
+ } else if (header && isWinCtrlMacMeta(event)) {
128
+ const actionsButton = currentCell.querySelector(
129
+ `button[aria-label="${table.options.localization.columnActions}"]`,
130
+ );
131
+ if (actionsButton) {
132
+ (actionsButton as HTMLButtonElement).click();
133
+ }
134
+ } else if (header?.column?.getCanSort()) {
135
+ event.preventDefault();
136
+ header.column.toggleSorting();
114
137
  }
115
138
  } else if (
116
- ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown', 'Home', 'End'].includes(
117
- event.key,
118
- )
139
+ [
140
+ 'ArrowRight',
141
+ 'ArrowLeft',
142
+ 'ArrowUp',
143
+ 'ArrowDown',
144
+ 'Home',
145
+ 'End',
146
+ 'PageUp',
147
+ 'PageDown',
148
+ ].includes(event.key)
119
149
  ) {
120
150
  event.preventDefault();
121
- const currentCell = event.currentTarget;
122
- const currentRow = parentElement || currentCell.closest('tr');
123
151
 
152
+ const currentRow = parentElement || currentCell.closest('tr');
124
153
  const tableElement = containerElement || currentCell.closest('table');
125
154
  const allCells =
126
155
  cellElements ||
@@ -146,6 +175,17 @@ export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
146
175
  return targetCell as HTMLElement;
147
176
  };
148
177
 
178
+ //page up/down first or last cell in column
179
+ const findBottomTopCell = (columnIndex: number, edge: 'b' | 't') => {
180
+ const row =
181
+ edge === 't'
182
+ ? tableElement?.querySelector('tr')
183
+ : tableElement?.lastElementChild?.lastElementChild;
184
+ const rowCells = Array.from(row?.children || []);
185
+ const targetCell = rowCells[columnIndex];
186
+ return targetCell as HTMLElement;
187
+ };
188
+
149
189
  const findAdjacentCell = (
150
190
  columnIndex: number,
151
191
  searchDirection: 'f' | 'b',
@@ -173,10 +213,16 @@ export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({
173
213
  nextCell = findAdjacentCell(currentIndex, 'f');
174
214
  break;
175
215
  case 'Home':
176
- nextCell = findEdgeCell(event.ctrlKey ? 'f' : 'c', 'f');
216
+ nextCell = findEdgeCell(isWinCtrlMacMeta(event) ? 'f' : 'c', 'f');
177
217
  break;
178
218
  case 'End':
179
- nextCell = findEdgeCell(event.ctrlKey ? 'l' : 'c', 'l');
219
+ nextCell = findEdgeCell(isWinCtrlMacMeta(event) ? 'l' : 'c', 'l');
220
+ break;
221
+ case 'PageUp':
222
+ nextCell = findBottomTopCell(currentIndex, 't');
223
+ break;
224
+ case 'PageDown':
225
+ nextCell = findBottomTopCell(currentIndex, 'b');
180
226
  break;
181
227
  }
182
228