|
1 | 1 | import { useCallback, useEffect, useRef } from 'react';
|
| 2 | +import { actions } from 'react-table'; |
| 3 | +import { getLeafHeaders } from '../util/index.js'; |
2 | 4 |
|
3 | 5 | const CELL_DATA_ATTRIBUTES = ['visibleColumnIndex', 'columnIndex', 'rowIndex', 'visibleRowIndex'];
|
4 | 6 |
|
@@ -322,6 +324,41 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
|
322 | 324 | ];
|
323 | 325 | };
|
324 | 326 |
|
| 327 | +function getPayload(e, column) { |
| 328 | + e.preventDefault(); |
| 329 | + e.stopPropagation(); |
| 330 | + const clientX = e.target.getBoundingClientRect().x + e.target.getBoundingClientRect().width; |
| 331 | + const columnId = column.id; |
| 332 | + const columnWidth = column.totalWidth; |
| 333 | + const headersToResize = getLeafHeaders(column); |
| 334 | + const headerIdWidths = headersToResize.map((d) => [d.id, d.totalWidth]); |
| 335 | + return { clientX, columnId, columnWidth, headerIdWidths }; |
| 336 | +} |
| 337 | + |
| 338 | +const setHeaderProps = (headerProps, { instance: { dispatch }, column }) => { |
| 339 | + // resize col with keyboard |
| 340 | + const handleKeyDown = (e) => { |
| 341 | + if (e.nativeEvent.shiftKey) { |
| 342 | + if (e.key === 'ArrowRight') { |
| 343 | + const payload = getPayload(e, column); |
| 344 | + dispatch({ type: actions.columnStartResizing, ...payload }); |
| 345 | + dispatch({ type: actions.columnResizing, clientX: payload.clientX + 16 }); |
| 346 | + dispatch({ type: actions.columnDoneResizing }); |
| 347 | + return; |
| 348 | + } |
| 349 | + if (e.key === 'ArrowLeft') { |
| 350 | + const payload = getPayload(e, column); |
| 351 | + dispatch({ type: actions.columnStartResizing, ...payload }); |
| 352 | + dispatch({ type: actions.columnResizing, clientX: payload.clientX - 16 }); |
| 353 | + dispatch({ type: actions.columnDoneResizing }); |
| 354 | + return; |
| 355 | + } |
| 356 | + } |
| 357 | + }; |
| 358 | + return [headerProps, { onKeyDown: handleKeyDown }]; |
| 359 | +}; |
| 360 | + |
325 | 361 | export const useKeyboardNavigation = (hooks) => {
|
326 | 362 | hooks.getTableProps.push(useGetTableProps);
|
| 363 | + hooks.getHeaderProps.push(setHeaderProps); |
327 | 364 | };
|
0 commit comments