-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
press.ts
168 lines (140 loc) · 4.32 KB
/
press.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
getPreviousTabbableIn,
getNextTabbableIn,
isFocusable
} from "reakit-utils";
import { subscribeDefaultPrevented } from "./__utils/subscribeDefaultPrevented";
import { isTextField } from "./__utils/isTextField";
import { fireEvent } from "./fireEvent";
import { focus } from "./focus";
import { blur } from "./blur";
import "./mockClientRects";
const clickableInputTypes = [
"button",
"color",
"file",
"image",
"reset",
"submit"
];
function submitFormByPressingEnterOn(
element: HTMLInputElement,
options: KeyboardEventInit
) {
const { form } = element;
if (!form) return;
const elements = Array.from(form.elements);
const validInputs = elements.filter(
el => el instanceof HTMLInputElement && isTextField(el)
);
const submitButton = elements.find(
el =>
(el instanceof HTMLInputElement || el instanceof HTMLButtonElement) &&
el.type === "submit"
);
if (validInputs.length === 1 || submitButton) {
fireEvent.submit(form, options);
}
}
const keyDownMap: Record<
string,
(element: Element, options: KeyboardEventInit) => void
> = {
Tab(element, { shiftKey }) {
const body = element.ownerDocument?.body || document.body;
const nextElement = shiftKey
? getPreviousTabbableIn(body)
: getNextTabbableIn(body);
if (nextElement) {
focus(nextElement);
}
},
Enter(element, options) {
const nonSubmittableTypes = [...clickableInputTypes, "hidden"];
const isClickable =
element.tagName === "BUTTON" ||
(element instanceof HTMLInputElement &&
clickableInputTypes.includes(element.type));
const isSubmittable =
element instanceof HTMLInputElement &&
!nonSubmittableTypes.includes(element.type);
if (isClickable) {
fireEvent.click(element, options);
} else if (isSubmittable) {
submitFormByPressingEnterOn(element as HTMLInputElement, options);
}
}
};
const keyUpMap: Record<
string,
(element: Element, options: KeyboardEventInit) => void
> = {
// Space
" ": (element, options) => {
const spaceableTypes = [...clickableInputTypes, "checkbox", "radio"];
const isSpaceable =
element.tagName === "BUTTON" ||
(element instanceof HTMLInputElement &&
spaceableTypes.includes(element.type));
if (isSpaceable) {
fireEvent.click(element, options);
}
}
};
export function press(
key: string,
element?: Element | null,
options: KeyboardEventInit = {}
) {
if (element == null) {
element = document.activeElement || document.body;
}
if (!element) return;
// We can't press on elements that aren't focusable
if (!isFocusable(element) && element.tagName !== "BODY") return;
// If element is not focused, we should focus it
if (element.ownerDocument?.activeElement !== element) {
if (element.tagName === "BODY") {
blur();
} else {
focus(element);
}
}
// Track event.preventDefault() calls so we bail out of keydown/keyup effects
const defaultPrevented = subscribeDefaultPrevented(
element,
"keydown",
"keyup"
);
fireEvent.keyDown(element, { key, ...options });
if (!defaultPrevented.current && key in keyDownMap && !options.metaKey) {
keyDownMap[key](element, options);
}
// If keydown effect changed focus (e.g. Tab), keyup will be triggered on the
// next element.
if (element.ownerDocument?.activeElement !== element) {
element = element.ownerDocument!.activeElement!;
}
fireEvent.keyUp(element, { key, ...options });
if (!defaultPrevented.current && key in keyUpMap && !options.metaKey) {
keyUpMap[key](element, options);
}
defaultPrevented.unsubscribe();
}
function createPress(key: string, defaultOptions: KeyboardEventInit = {}) {
return (element?: Element | null, options: KeyboardEventInit = {}) =>
press(key, element, { ...defaultOptions, ...options });
}
press.Escape = createPress("Escape");
press.Tab = createPress("Tab");
press.ShiftTab = createPress("Tab", { shiftKey: true });
press.Enter = createPress("Enter");
press.Space = createPress(" ");
press.ArrowUp = createPress("ArrowUp");
press.ArrowRight = createPress("ArrowRight");
press.ArrowDown = createPress("ArrowDown");
press.ArrowLeft = createPress("ArrowLeft");
press.End = createPress("End");
press.Home = createPress("Home");
press.PageUp = createPress("PageUp");
press.PageDown = createPress("PageDown");