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

Support Home and End key events #523

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ The following special character strings are supported:
| `{arrowright}` | ArrowRight | N/A | |
| `{arrowup}` | ArrowUp | N/A | |
| `{arrowdown}` | ArrowDown | N/A | |
| `{home}` | Home | N/A | |
| `{end}` | End | N/A | |
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
| `{ctrl}` | Control | `ctrlKey` | |
| `{alt}` | Alt | `altKey` | |
Expand Down Expand Up @@ -532,6 +534,8 @@ method.
| arrowRight | `{arrowright}` |
| arrowDown | `{arrowdown}` |
| arrowUp | `{arrowup}` |
| home | `{home}` |
| end | `{end}` |
| enter | `{enter}` |
| escape | `{esc}` |
| delete | `{del}` |
Expand Down
52 changes: 52 additions & 0 deletions src/__tests__/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -1445,3 +1445,55 @@ test('{arrowup} fires keyup/keydown events', () => {
input[value=""] - keyup: ArrowUp (38)
`)
})

test('{home} fires keyup/keydown events', () => {
const {element, getEventSnapshot} = setup('<input />')

userEvent.type(element, '{home}')

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]

input[value=""] - pointerover
input[value=""] - pointerenter
input[value=""] - mouseover: Left (0)
input[value=""] - mouseenter: Left (0)
input[value=""] - pointermove
input[value=""] - mousemove: Left (0)
input[value=""] - pointerdown
input[value=""] - mousedown: Left (0)
input[value=""] - focus
input[value=""] - focusin
input[value=""] - pointerup
input[value=""] - mouseup: Left (0)
input[value=""] - click: Left (0)
input[value=""] - keydown: Home (36)
input[value=""] - keyup: Home (36)
`)
})

test('{end} fires keyup/keydown events', () => {
const {element, getEventSnapshot} = setup('<input />')

userEvent.type(element, '{end}')

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]

input[value=""] - pointerover
input[value=""] - pointerenter
input[value=""] - mouseover: Left (0)
input[value=""] - mouseenter: Left (0)
input[value=""] - pointermove
input[value=""] - mousemove: Left (0)
input[value=""] - pointerdown
input[value=""] - mousedown: Left (0)
input[value=""] - focus
input[value=""] - focusin
input[value=""] - pointerup
input[value=""] - mouseup: Left (0)
input[value=""] - click: Left (0)
input[value=""] - keydown: End (35)
input[value=""] - keyup: End (35)
`)
})
42 changes: 42 additions & 0 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ const specialCharMap = {
arrowRight: '{arrowright}',
arrowDown: '{arrowdown}',
arrowUp: '{arrowup}',
home: '{home}',
end: '{end}',
enter: '{enter}',
escape: '{esc}',
delete: '{del}',
Expand All @@ -106,6 +108,8 @@ const specialCharCallbackMap = {
[specialCharMap.arrowRight]: navigationKey('ArrowRight'),
[specialCharMap.arrowDown]: handleArrowDown,
[specialCharMap.arrowUp]: handleArrowUp,
[specialCharMap.home]: handleHome,
[specialCharMap.end]: handleEnd,
[specialCharMap.enter]: handleEnter,
[specialCharMap.escape]: handleEsc,
[specialCharMap.delete]: handleDel,
Expand Down Expand Up @@ -786,5 +790,43 @@ function handleArrowUp({currentElement, eventOverrides}) {
...eventOverrides,
})
}

function handleHome({currentElement, eventOverrides}) {
const key = 'Home'
const keyCode = 36

fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})

fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
}

function handleEnd({currentElement, eventOverrides}) {
const key = 'End'
const keyCode = 35

fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})

fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
}

export {type, specialCharMap}
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export enum specialChars {
arrowRight = '{arrowright}',
arrowDown = '{arrowdown}',
arrowUp = '{arrowup}',
home = '{home}',
end = '{end}',
enter = '{enter}',
escape = '{esc}',
delete = '{del}',
Expand Down