-
Notifications
You must be signed in to change notification settings - Fork 239
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
Feature: Implement cursor navigation with navigation keys #404
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {fireEvent} from '@testing-library/dom' | ||
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. What are your opinions on using this kind of code organization? I noticed there is 3 files for typing:
I think navigation keys will end up adding a chunk (but not massive) amount of code. I opted for a new file but I am willing to move things around. 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 probably wouldn't have bothered making the new file myself, but I'm ok with it. |
||
|
||
import {setSelectionRangeIfNecessary} from '../utils' | ||
|
||
const keys = { | ||
ArrowLeft: { | ||
keyCode: 37, | ||
}, | ||
ArrowRight: { | ||
keyCode: 39, | ||
}, | ||
} | ||
|
||
function getSelectionRange(currentElement, key) { | ||
const {selectionStart, selectionEnd} = currentElement() | ||
const cursorChange = Number(key in keys) * (key === 'ArrowLeft' ? -1 : 1) | ||
return { | ||
selectionStart: selectionStart + cursorChange, | ||
selectionEnd: selectionEnd + cursorChange, | ||
} | ||
} | ||
|
||
function navigationKey(key) { | ||
const event = { | ||
key, | ||
keyCode: keys[key].keyCode, | ||
which: keys[key].keyCode, | ||
} | ||
|
||
return ({currentElement, eventOverrides}) => { | ||
fireEvent.keyDown(currentElement(), { | ||
...event, | ||
...eventOverrides, | ||
}) | ||
|
||
const range = getSelectionRange(currentElement, key) | ||
setSelectionRangeIfNecessary( | ||
currentElement(), | ||
range.selectionStart, | ||
range.selectionEnd, | ||
) | ||
|
||
fireEvent.keyUp(currentElement(), { | ||
...event, | ||
...eventOverrides, | ||
}) | ||
} | ||
} | ||
|
||
export {navigationKey} |
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.
I just tried this in the playground and here's what I got for the events (Note, I ignored mouse/pointer move events because they're too noisy in the playground):
Notice that there's no "select" event in there. I'm not sure whether it's possible to avoid that event, but it would be good to avoid if we can.
Also, I think this test could be simplified if we change the input to:
b{arrowleft}a{arrowright}c
(which would result inabc
, and much fewer events for the snapshot).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.
At least from what I could investigate, the [setSelectionRange](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange] will fire a
select
event, and I can't really seem to find another way to move the cursor.