Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize column via keyboard #3357

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ function DataGrid<R, SR, K extends Key>(
}
});

console.log('called?');

useLayoutEffect(() => {
if (!shouldFocusCellRef.current) return;
shouldFocusCellRef.current = false;
Expand Down Expand Up @@ -525,18 +527,22 @@ function DataGrid<R, SR, K extends Key>(

function handleKeyDown(event: KeyboardEvent<HTMLDivElement>) {
const { idx, rowIdx, mode } = selectedPosition;
const column = columns[idx];

if (mode === 'EDIT') return;

if (onCellKeyDown && isRowIdxWithinViewportBounds(rowIdx)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to check isRowIdxWithinViewportBounds before call onCellKeyDown ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think because row would be undefined otherwise. Same for summary row. We may have to add a type HEADER | ROW | SUMMARY

if (onCellKeyDown) {
const row = rows[rowIdx];
const cellEvent = createCellEvent(event);
onCellKeyDown(
{
mode: 'SELECT',
row,
column: columns[idx],
column,
rowIdx,
selectCell
selectCell,
resizeColumn: handleColumnResize,
getColumnWidth
},
cellEvent
);
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ interface SelectCellKeyDownArgs<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
rowIdx: number;
selectCell: (position: Position, enableEditor?: Maybe<boolean>) => void;
resizeColumn: (column: CalculatedColumn<TRow, TSummaryRow>, width: number) => void;
getColumnWidth: (column: CalculatedColumn<TRow, TSummaryRow>) => string | number;
}

export interface EditCellKeyDownArgs<TRow, TSummaryRow = unknown> {
Expand Down
34 changes: 34 additions & 0 deletions test/column/resizable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import type { Column } from '../../src';
import { resizeHandleClassname } from '../../src/HeaderCell';
import { isCtrlKeyHeldDown } from '../../src/utils';
import { getGrid, getHeaderCells, setup } from '../utils';

const pointerId = 1;
Expand Down Expand Up @@ -104,3 +106,35 @@ test('should use the minWidth if specified', () => {
resize({ column: col2, clientXStart: 295, clientXEnd: 100, rect: { right: 300, left: 100 } });
expect(getGrid()).toHaveStyle({ gridTemplateColumns: '100px 100px' });
});

test('should resize column via keyboard', async () => {
setup({
columns,
rows: [],
onCellKeyDown(args, event) {
if (args.mode === 'SELECT') {
const { key } = event;
const { column, getColumnWidth, resizeColumn } = args;
if (column.resizable && isCtrlKeyHeldDown(event) && event.shiftKey) {
event.preventGridDefault();
const leftKey = 'ArrowLeft';
const rightKey = 'ArrowRight';
if (key === leftKey || key === rightKey) {
const width = getColumnWidth(column);
const step = 10;
const isIncrease = key === rightKey;
const newWidth = isIncrease ? Number(width) + step : Number(width) - step;
resizeColumn(column, newWidth);
}
}
}
}
});
const [, col2] = getHeaderCells();
await userEvent.click(col2);
expect(getGrid()).toHaveStyle({ gridTemplateColumns: '100px 200px' });
await userEvent.keyboard('{Control>}{Shift>}{ArrowRight}{/Control}{/Shift}');
expect(getGrid()).toHaveStyle({ gridTemplateColumns: '100px 210px' });
await userEvent.keyboard('{Control>}{Shift>}{ArrowLeft}{/Control}{/Shift}');
expect(getGrid()).toHaveStyle({ gridTemplateColumns: '100px 200px' });
});
19 changes: 19 additions & 0 deletions website/demos/AllFeatures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { css } from '@linaria/core';

import DataGrid, { SelectColumn, textEditor } from '../../src';
import type { Column, CopyEvent, FillEvent, PasteEvent } from '../../src';
import { isCtrlKeyHeldDown } from '../../src/utils';
import { renderAvatar, renderDropdown } from './renderers';
import type { Props } from './types';

Expand Down Expand Up @@ -218,6 +219,24 @@ export default function AllFeatures({ direction }: Props) {
args.selectCell(true);
}
}}
onCellKeyDown={(args, event) => {
if (args.mode === 'SELECT') {
const { key } = event;
const { column, getColumnWidth, resizeColumn } = args;
if (column.resizable && isCtrlKeyHeldDown(event) && event.shiftKey) {
event.preventGridDefault();
const leftKey = 'ArrowLeft';
const rightKey = 'ArrowRight';
if (key === leftKey || key === rightKey) {
const width = getColumnWidth(column);
const step = 10;
const isIncrease = key === rightKey;
const newWidth = isIncrease ? Number(width) + step : Number(width) - step;
resizeColumn(column, newWidth);
}
}
}
}}
/>
);
}
Loading