-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Resize column via keyboard #3754
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
Changes from 17 commits
5646a13
c1dc28d
3476af5
f49791d
e0c3713
5408a5c
7d7b52e
2545ce2
5d664f4
8a984d9
c20529d
5274ef5
1eeaffb
f185b0a
aa99284
a9668da
eebd2cd
2f4750d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { | |
| getCellStyle, | ||
| getHeaderCellRowSpan, | ||
| getHeaderCellStyle, | ||
| isCtrlKeyHeldDown, | ||
| stopPropagation | ||
| } from './utils'; | ||
| import type { CalculatedColumn, SortColumn } from './types'; | ||
|
|
@@ -160,10 +161,25 @@ export default function HeaderCell<R, SR>({ | |
| } | ||
|
|
||
| function onKeyDown(event: React.KeyboardEvent<HTMLSpanElement>) { | ||
| if (event.key === ' ' || event.key === 'Enter') { | ||
| const { key } = event; | ||
| if (sortable && (key === ' ' || key === 'Enter')) { | ||
| // prevent scrolling | ||
| event.preventDefault(); | ||
| onSort(event.ctrlKey || event.metaKey); | ||
| } else if ( | ||
| resizable && | ||
| isCtrlKeyHeldDown(event) && | ||
| (key === 'ArrowLeft' || key === 'ArrowRight') | ||
| ) { | ||
| // prevent navigation | ||
| // TODO: check if we can use `preventDefault` instead | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to use |
||
| event.stopPropagation(); | ||
| const { width } = event.currentTarget.getBoundingClientRect(); | ||
| const offset = key === 'ArrowLeft' ? -10 : 10; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also add support to resize to
amanmahajan7 marked this conversation as resolved.
Outdated
|
||
| const newWidth = clampColumnWidth(width + offset, column); | ||
| if (newWidth !== width) { | ||
| onColumnResize(column, newWidth); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -192,6 +208,7 @@ export default function HeaderCell<R, SR>({ | |
| if (event.dataTransfer.types.includes(dragDropKey.toLowerCase())) { | ||
| const sourceKey = event.dataTransfer.getData(dragDropKey.toLowerCase()); | ||
| if (sourceKey !== column.key) { | ||
| // prevent the browser from redirecting in some cases | ||
| event.preventDefault(); | ||
| onColumnsReorder?.(sourceKey, column.key); | ||
| } | ||
|
|
@@ -242,7 +259,7 @@ export default function HeaderCell<R, SR>({ | |
| }} | ||
| onFocus={handleFocus} | ||
| onClick={onClick} | ||
| onKeyDown={sortable ? onKeyDown : undefined} | ||
| onKeyDown={onKeyDown} | ||
| {...draggableProps} | ||
| > | ||
| {column.renderHeaderCell({ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open to suggestion on which shortcut to use. I did not find any good example other than this. But the example uses a different strategy where the resizer gets focused. I think we can keep our implementation simple