-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5646a13
Use react events
amanmahajan7 c1dc28d
Add `ResizeHandle` component
amanmahajan7 3476af5
Initial commit
amanmahajan7 f49791d
Add test
amanmahajan7 e0c3713
Tweak tests
amanmahajan7 5408a5c
Remove conditional handler
amanmahajan7 7d7b52e
Update comments
amanmahajan7 2545ce2
useRef
amanmahajan7 5d664f4
rename
amanmahajan7 8a984d9
Merge branch 'use-react-events' into am-accessible-resizing
amanmahajan7 c20529d
Revert "rename"
amanmahajan7 5274ef5
Merge branch 'use-react-events' into am-accessible-resizing
amanmahajan7 1eeaffb
Merge branch 'main' into am-accessible-resizing
amanmahajan7 f185b0a
Add space
amanmahajan7 aa99284
Merge branch 'main' into am-accessible-resizing
amanmahajan7 a9668da
Use `stopPropagation` for now
amanmahajan7 eebd2cd
Merge branch 'main' into am-accessible-resizing
amanmahajan7 2f4750d
Handle RTL
amanmahajan7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ import { | |
| getCellStyle, | ||
| getHeaderCellRowSpan, | ||
| getHeaderCellStyle, | ||
| getLeftRightKey, | ||
| isCtrlKeyHeldDown, | ||
| stopPropagation | ||
| } from './utils'; | ||
| import type { CalculatedColumn, SortColumn } from './types'; | ||
|
|
@@ -160,10 +162,26 @@ 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 { leftKey } = getLeftRightKey(direction); | ||
| const offset = key === leftKey ? -10 : 10; | ||
| const newWidth = clampColumnWidth(width + offset, column); | ||
| if (newWidth !== width) { | ||
| onColumnResize(column, newWidth); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -192,6 +210,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 +261,7 @@ export default function HeaderCell<R, SR>({ | |
| }} | ||
| onFocus={handleFocus} | ||
| onClick={onClick} | ||
| onKeyDown={sortable ? onKeyDown : undefined} | ||
| onKeyDown={onKeyDown} | ||
| {...draggableProps} | ||
| > | ||
| {column.renderHeaderCell({ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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