|
1 | 1 | import {
|
| 2 | + MRT_Header, |
2 | 3 | type MRT_Cell,
|
3 | 4 | type MRT_RowData,
|
4 | 5 | type MRT_TableInstance,
|
5 | 6 | } from '../types';
|
| 7 | +import { |
| 8 | + getMRT_RowSelectionHandler, |
| 9 | + getMRT_SelectAllHandler, |
| 10 | +} from './row.utils'; |
6 | 11 | import { parseFromValuesOrFunc } from './utils';
|
7 | 12 |
|
8 | 13 | export const isCellEditable = <TData extends MRT_RowData>({
|
@@ -49,23 +54,65 @@ export const openEditingCell = <TData extends MRT_RowData>({
|
49 | 54 | }
|
50 | 55 | };
|
51 | 56 |
|
52 |
| -export const cellNavigation = ({ |
| 57 | +export const cellNavigation = <TData extends MRT_RowData = MRT_RowData>({ |
| 58 | + cell, |
53 | 59 | cellElements,
|
54 | 60 | cellValue,
|
55 | 61 | containerElement,
|
56 | 62 | event,
|
| 63 | + header, |
57 | 64 | parentElement,
|
| 65 | + table, |
58 | 66 | }: {
|
59 |
| - cellElements?: Array<HTMLTableCellElement | HTMLDivElement>; |
| 67 | + cell?: MRT_Cell<TData>; |
| 68 | + header?: MRT_Header<TData>; |
| 69 | + cellElements?: Array<HTMLTableCellElement>; |
60 | 70 | cellValue?: string;
|
61 |
| - containerElement?: HTMLDivElement | HTMLTableElement; |
62 |
| - event: React.KeyboardEvent<HTMLTableCellElement | HTMLDivElement>; |
63 |
| - parentElement?: HTMLTableRowElement | HTMLDivElement; |
| 71 | + containerElement?: HTMLTableElement; |
| 72 | + event: React.KeyboardEvent<HTMLTableCellElement>; |
| 73 | + parentElement?: HTMLTableRowElement; |
| 74 | + table: MRT_TableInstance<TData>; |
64 | 75 | }) => {
|
65 | 76 | if (cellValue && (event.ctrlKey || event.metaKey) && event.key === 'c') {
|
66 | 77 | navigator.clipboard.writeText(cellValue);
|
67 |
| - } |
68 |
| - if ( |
| 78 | + } else if (['Enter', ' '].includes(event.key)) { |
| 79 | + if (cell?.column?.id === 'mrt-row-select') { |
| 80 | + getMRT_RowSelectionHandler({ |
| 81 | + row: cell.row, |
| 82 | + table, |
| 83 | + //@ts-ignore |
| 84 | + staticRowIndex: +event.target.getAttribute('data-index'), |
| 85 | + })(event as any); |
| 86 | + } else if ( |
| 87 | + header?.column?.id === 'mrt-row-select' && |
| 88 | + table.options.enableSelectAll |
| 89 | + ) { |
| 90 | + getMRT_SelectAllHandler({ |
| 91 | + table, |
| 92 | + })(event as any); |
| 93 | + } else if ( |
| 94 | + cell?.column?.id === 'mrt-row-expand' && |
| 95 | + (cell.row.getCanExpand() || |
| 96 | + table.options.renderDetailPanel?.({ row: cell.row, table })) |
| 97 | + ) { |
| 98 | + cell.row.toggleExpanded(); |
| 99 | + } else if ( |
| 100 | + header?.column?.id === 'mrt-row-expand' && |
| 101 | + table.options.enableExpandAll |
| 102 | + ) { |
| 103 | + table.toggleAllRowsExpanded(); |
| 104 | + } else if (header?.column?.getCanSort()) { |
| 105 | + header.column.toggleSorting(); |
| 106 | + } else if (cell?.column.id === 'mrt-row-pin') { |
| 107 | + cell.row.getIsPinned() |
| 108 | + ? cell.row.pin(false) |
| 109 | + : cell.row.pin( |
| 110 | + table.options.rowPinningDisplayMode?.includes('bottom') |
| 111 | + ? 'bottom' |
| 112 | + : 'top', |
| 113 | + ); |
| 114 | + } |
| 115 | + } else if ( |
69 | 116 | ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown', 'Home', 'End'].includes(
|
70 | 117 | event.key,
|
71 | 118 | )
|
|
0 commit comments