Skip to content

Commit 63652bd

Browse files
authored
feat(AnalyticalTable): allow resizing columns via Shift+ArrowLeft/Right (#4989)
Closes #4397
1 parent 9472c8e commit 63652bd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/main/src/components/AnalyticalTable/hooks/useKeyboardNavigation.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { useCallback, useEffect, useRef } from 'react';
2+
import { actions } from 'react-table';
3+
import { getLeafHeaders } from '../util/index.js';
24

35
const CELL_DATA_ATTRIBUTES = ['visibleColumnIndex', 'columnIndex', 'rowIndex', 'visibleRowIndex'];
46

@@ -322,6 +324,41 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
322324
];
323325
};
324326

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+
325361
export const useKeyboardNavigation = (hooks) => {
326362
hooks.getTableProps.push(useGetTableProps);
363+
hooks.getHeaderProps.push(setHeaderProps);
327364
};

packages/main/src/components/AnalyticalTable/util/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,16 @@ export function getSubRowsByString(subRowsKey, row) {
156156
return subRowsKey.split('.').reduce((acc, cur) => acc?.[cur], row);
157157
}
158158
}
159+
160+
// copied from https://github.com/TanStack/table/blob/06703a56890122cedf1b2fa4b82982999537774e/src/plugin-hooks/useResizeColumns.js#L286-L296 (22. Aug 2023)
161+
export function getLeafHeaders(header) {
162+
const leafHeaders = [];
163+
const recurseHeader = (header) => {
164+
if (header.columns && header.columns.length) {
165+
header.columns.map(recurseHeader);
166+
}
167+
leafHeaders.push(header);
168+
};
169+
recurseHeader(header);
170+
return leafHeaders;
171+
}

0 commit comments

Comments
 (0)