-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathclick.ts
168 lines (141 loc) · 4.65 KB
/
click.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 "./polyfills.js";
import { closest, isVisible } from "@ariakit/core/utils/dom";
import { isFocusable } from "@ariakit/core/utils/focus";
import { queuedMicrotasks } from "./__utils.js";
import { fireEvent } from "./fire-event.js";
import { focus } from "./focus.js";
import { hover } from "./hover.js";
import { mouseDown } from "./mouse-down.js";
import { mouseUp } from "./mouse-up.js";
import { sleep } from "./sleep.js";
function getClosestLabel(element: Element) {
if (!isFocusable(element)) {
return closest(element, "label");
}
return null;
}
function getInputFromLabel(element: HTMLLabelElement) {
const input = element.htmlFor
? element.ownerDocument?.getElementById(element.htmlFor)
: element.querySelector("input,textarea,select");
return input as
| HTMLInputElement
| HTMLTextAreaElement
| HTMLSelectElement
| null
| undefined;
}
function clickLabel(element: HTMLLabelElement, options?: MouseEventInit) {
const input = getInputFromLabel(element);
const isInputDisabled = Boolean(input?.disabled);
if (input) {
// JSDOM will automatically "click" input right after we "click" the label.
// Since we need to "focus" it first, we temporarily disable it so it won't
// get automatically clicked.
input.disabled = true;
}
const defaultAllowed = fireEvent.click(element, options);
if (input) {
// Now we can revert input disabled state and fire events on it in the
// right order.
input.disabled = isInputDisabled;
if (defaultAllowed && isFocusable(input)) {
focus(input);
// Only "click" is fired! Browsers don't go over the whole event stack in
// this case (mousedown, mouseup etc.).
fireEvent.click(input);
}
}
}
function setSelected(element: HTMLOptionElement, selected: boolean) {
element.setAttribute("selected", selected ? "selected" : "");
element.selected = selected;
}
function clickOption(
element: HTMLOptionElement,
eventOptions?: MouseEventInit
) {
// https://stackoverflow.com/a/16530782/5513909
const select = closest(element, "select") as HTMLSelectElement & {
lastOptionSelectedNotByShiftKey?: HTMLOptionElement;
};
if (!select) {
fireEvent.click(element, eventOptions);
return;
}
if (select.multiple) {
const options = Array.from(select.options);
const resetOptions = () =>
options.forEach((option) => {
setSelected(option, false);
});
const selectRange = (a: number, b: number) => {
const from = Math.min(a, b);
const to = Math.max(a, b) + 1;
const selectedOptions = options.slice(from, to);
selectedOptions.forEach((option) => {
setSelected(option, true);
});
};
if (eventOptions?.shiftKey) {
const elementIndex = options.indexOf(element);
const referenceOption = select.lastOptionSelectedNotByShiftKey;
const referenceOptionIndex = referenceOption
? options.indexOf(referenceOption)
: -1;
resetOptions();
// Select options between the reference option and the clicked element
selectRange(elementIndex, referenceOptionIndex);
setSelected(element, true);
} else {
// Keep track of this option as this will be used later when shift key
// is used.
select.lastOptionSelectedNotByShiftKey = element;
if (eventOptions?.ctrlKey) {
// Clicking with ctrlKey will select/deselect the option
setSelected(element, !element.selected);
} else {
// Simply clicking an option will select only that option
resetOptions();
setSelected(element, true);
}
}
} else {
setSelected(element, true);
}
fireEvent.input(select);
fireEvent.change(select);
fireEvent.click(element, eventOptions);
}
export async function click(
element: Element,
options?: MouseEventInit,
tap = false
) {
if (!isVisible(element)) return;
await hover(element, options);
mouseDown(element, options);
await queuedMicrotasks();
// The element may be hidden after hover/mouseDown, so we need to check again
// and find the first visible parent.
while (!isVisible(element)) {
if (!element.parentElement) return;
element = element.parentElement;
}
if (!tap) {
await sleep();
}
mouseUp(element, options);
// click is not called on disabled elements
const { disabled } = element as HTMLButtonElement;
if (disabled) return;
const label = getClosestLabel(element);
if (label) {
clickLabel(label, { detail: 1, ...options });
} else if (element instanceof HTMLOptionElement) {
clickOption(element, { detail: 1, ...options });
} else {
fireEvent.click(element, { detail: 1, ...options });
}
await sleep();
}