Skip to content

Commit f731f68

Browse files
feat(keyboard): handle PageUp and PageDown on input (#734)
* feat: add support for pageup and pagedown control keys * remove PageUp and PageDown behaviour from textarea and editable elements Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>
1 parent 2f900ef commit f731f68

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/__tests__/type.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,52 @@ test('navigation key: {home} and {end} moves the cursor', () => {
10241024
`)
10251025
})
10261026

1027+
test('navigation key: {pageUp} and {pageDown} moves the cursor for <input>', () => {
1028+
const {element, getEventSnapshot} = setup('<input />')
1029+
userEvent.type(element, 'c{pageUp}ab{pageDown}d')
1030+
expect(getEventSnapshot()).toMatchInlineSnapshot(`
1031+
Events fired on: input[value="abcd"]
1032+
1033+
input[value=""] - pointerover
1034+
input[value=""] - pointerenter
1035+
input[value=""] - mouseover: Left (0)
1036+
input[value=""] - mouseenter: Left (0)
1037+
input[value=""] - pointermove
1038+
input[value=""] - mousemove: Left (0)
1039+
input[value=""] - pointerdown
1040+
input[value=""] - mousedown: Left (0)
1041+
input[value=""] - focus
1042+
input[value=""] - focusin
1043+
input[value=""] - pointerup
1044+
input[value=""] - mouseup: Left (0)
1045+
input[value=""] - click: Left (0)
1046+
input[value=""] - keydown: c (99)
1047+
input[value=""] - keypress: c (99)
1048+
input[value="c"] - input
1049+
input[value="c"] - keyup: c (99)
1050+
input[value="c"] - keydown: PageUp (33)
1051+
input[value="c"] - select
1052+
input[value="c"] - keyup: PageUp (33)
1053+
input[value="c"] - keydown: a (97)
1054+
input[value="c"] - keypress: a (97)
1055+
input[value="ac"] - select
1056+
input[value="ac"] - input
1057+
input[value="ac"] - keyup: a (97)
1058+
input[value="ac"] - keydown: b (98)
1059+
input[value="ac"] - keypress: b (98)
1060+
input[value="abc"] - select
1061+
input[value="abc"] - input
1062+
input[value="abc"] - keyup: b (98)
1063+
input[value="abc"] - keydown: PageDown (34)
1064+
input[value="abc"] - select
1065+
input[value="abc"] - keyup: PageDown (34)
1066+
input[value="abc"] - keydown: d (100)
1067+
input[value="abc"] - keypress: d (100)
1068+
input[value="abcd"] - input
1069+
input[value="abcd"] - keyup: d (100)
1070+
`)
1071+
})
1072+
10271073
test('can type into an input with type `time`', () => {
10281074
const {element, getEventSnapshot} = setup('<input type="time" />')
10291075
userEvent.type(element, '01:05')

src/keyboard/keyMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export const defaultKeyMap: keyboardKey[] = [
7373
{code: 'Home', key: 'Home', keyCode: 36},
7474
{code: 'End', key: 'End', keyCode: 35},
7575
{code: 'Delete', key: 'Delete', keyCode: 46},
76+
{code: 'PageUp', key: 'PageUp', keyCode: 33},
77+
{code: 'PageDown', key: 'PageDown', keyCode: 34},
7678

7779
// TODO: add mappings
7880
]

src/keyboard/plugins/control.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ export const keydownBehavior: behaviorPlugin[] = [
3131
}
3232
},
3333
},
34+
{
35+
matches: (keyDef, element) =>
36+
(keyDef.key === 'PageUp' || keyDef.key === 'PageDown') &&
37+
isElementType(element, ['input']),
38+
handle: (keyDef, element) => {
39+
// This could probably been improved by collapsing a selection range
40+
if (keyDef.key === 'PageUp') {
41+
setSelectionRange(element, 0, 0)
42+
} else {
43+
const newPos = getValue(element)?.length ?? /* istanbul ignore next */ 0
44+
setSelectionRange(element, newPos, newPos)
45+
}
46+
},
47+
},
3448
{
3549
matches: (keyDef, element) =>
3650
keyDef.key === 'Delete' && isEditable(element) && !isCursorAtEnd(element),

src/keyboard/specialCharMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ export const specialCharMap = {
1212
selectAll: '{selectall}',
1313
space: '{space}',
1414
whitespace: ' ',
15+
pageUp: '{pageUp}',
16+
pageDown: '{pageDown}',
1517
} as const

0 commit comments

Comments
 (0)