Skip to content

Commit 808c550

Browse files
feat: add type support for {home} and {end} (#536)
* Add type support for {home} and {end} * update README to include {home} and {end} * fix keycode on End and Home Co-authored-by: Kent C. Dodds <[email protected]>
1 parent b4330c4 commit 808c550

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ change the state of the checkbox.
4747
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
4848
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
4949

50-
5150
- [Installation](#installation)
5251
- [API](#api)
5352
- [`click(element, eventInit, options)`](#clickelement-eventinit-options)
@@ -202,6 +201,8 @@ The following special character strings are supported:
202201
| `{arrowright}` | ArrowRight | N/A | |
203202
| `{arrowup}` | ArrowUp | N/A | |
204203
| `{arrowdown}` | ArrowDown | N/A | |
204+
| `{home}` | Home | N/A | |
205+
| `{end}` | End | N/A | |
205206
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
206207
| `{ctrl}` | Control | `ctrlKey` | |
207208
| `{alt}` | Alt | `altKey` | |
@@ -686,6 +687,7 @@ Thanks goes to these people ([emoji key][emojis]):
686687
687688
<!-- markdownlint-enable -->
688689
<!-- prettier-ignore-end -->
690+
689691
<!-- ALL-CONTRIBUTORS-LIST:END -->
690692
691693
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/type.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,56 @@ test('navigation key: {arrowleft} and {arrowright} moves the cursor', () => {
10671067
`)
10681068
})
10691069

1070+
test('navigation key: {home} and {end} moves the cursor', () => {
1071+
const {element, getEventSnapshot} = setup('<input />')
1072+
userEvent.type(element, 'c{home}ab{end}d')
1073+
expect(getEventSnapshot()).toMatchInlineSnapshot(`
1074+
Events fired on: input[value="abcd"]
1075+
1076+
input[value=""] - pointerover
1077+
input[value=""] - pointerenter
1078+
input[value=""] - mouseover: Left (0)
1079+
input[value=""] - mouseenter: Left (0)
1080+
input[value=""] - pointermove
1081+
input[value=""] - mousemove: Left (0)
1082+
input[value=""] - pointerdown
1083+
input[value=""] - mousedown: Left (0)
1084+
input[value=""] - focus
1085+
input[value=""] - focusin
1086+
input[value=""] - pointerup
1087+
input[value=""] - mouseup: Left (0)
1088+
input[value=""] - click: Left (0)
1089+
input[value=""] - keydown: c (99)
1090+
input[value=""] - keypress: c (99)
1091+
input[value="c"] - input
1092+
"{CURSOR}" -> "c{CURSOR}"
1093+
input[value="c"] - keyup: c (99)
1094+
input[value="c"] - keydown: Home (36)
1095+
input[value="c"] - select
1096+
input[value="c"] - keyup: Home (36)
1097+
input[value="c"] - keydown: a (97)
1098+
input[value="c"] - keypress: a (97)
1099+
input[value="ac"] - input
1100+
"{CURSOR}c" -> "ac{CURSOR}"
1101+
input[value="ac"] - select
1102+
input[value="ac"] - keyup: a (97)
1103+
input[value="ac"] - keydown: b (98)
1104+
input[value="ac"] - keypress: b (98)
1105+
input[value="abc"] - input
1106+
"a{CURSOR}c" -> "abc{CURSOR}"
1107+
input[value="abc"] - select
1108+
input[value="abc"] - keyup: b (98)
1109+
input[value="abc"] - keydown: End (35)
1110+
input[value="abc"] - select
1111+
input[value="abc"] - keyup: End (35)
1112+
input[value="abc"] - keydown: d (100)
1113+
input[value="abc"] - keypress: d (100)
1114+
input[value="abcd"] - input
1115+
"abc{CURSOR}" -> "abcd{CURSOR}"
1116+
input[value="abcd"] - keyup: d (100)
1117+
`)
1118+
})
1119+
10701120
test('can type into an input with type `time`', () => {
10711121
const {element, getEventSnapshot} = setup('<input type="time" />')
10721122
userEvent.type(element, '01:05')

src/keys/navigation-key.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import {fireEvent} from '@testing-library/dom'
33
import {setSelectionRangeIfNecessary} from '../utils'
44

55
const keys = {
6+
Home: {
7+
keyCode: 36,
8+
},
9+
End: {
10+
keyCode: 35,
11+
},
612
ArrowLeft: {
713
keyCode: 37,
814
},
@@ -13,6 +19,21 @@ const keys = {
1319

1420
function getSelectionRange(currentElement, key) {
1521
const {selectionStart, selectionEnd} = currentElement()
22+
23+
if (key === 'Home') {
24+
return {
25+
selectionStart: 0,
26+
selectionEnd: 0,
27+
}
28+
}
29+
30+
if (key === 'End') {
31+
return {
32+
selectionStart: selectionEnd + 1,
33+
selectionEnd: selectionEnd + 1,
34+
}
35+
}
36+
1637
const cursorChange = Number(key in keys) * (key === 'ArrowLeft' ? -1 : 1)
1738
return {
1839
selectionStart: selectionStart + cursorChange,

src/type.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const specialCharMap = {
9696
escape: '{esc}',
9797
delete: '{del}',
9898
backspace: '{backspace}',
99+
home: '{home}',
100+
end: '{end}',
99101
selectAll: '{selectall}',
100102
space: '{space}',
101103
whitespace: ' ',
@@ -106,6 +108,8 @@ const specialCharCallbackMap = {
106108
[specialCharMap.arrowRight]: navigationKey('ArrowRight'),
107109
[specialCharMap.arrowDown]: handleArrowDown,
108110
[specialCharMap.arrowUp]: handleArrowUp,
111+
[specialCharMap.home]: navigationKey('Home'),
112+
[specialCharMap.end]: navigationKey('End'),
109113
[specialCharMap.enter]: handleEnter,
110114
[specialCharMap.escape]: handleEsc,
111115
[specialCharMap.delete]: handleDel,

0 commit comments

Comments
 (0)