-
Notifications
You must be signed in to change notification settings - Fork 252
/
click.ts
171 lines (159 loc) Β· 4.95 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
169
170
171
import {fireEvent} from '@testing-library/dom'
import {
getMouseEventOptions,
isLabelWithInternallyDisabledControl,
isFocusable,
isDisabled,
isElementType,
hasPointerEvents,
PointerOptions,
} from './utils'
import {hover} from './hover'
import {blur} from './blur'
import {focus} from './focus'
function getPreviouslyFocusedElement(element: Element) {
const focusedElement = element.ownerDocument.activeElement
const wasAnotherElementFocused =
focusedElement &&
focusedElement !== element.ownerDocument.body &&
focusedElement !== element
return wasAnotherElementFocused ? focusedElement : null
}
export declare interface clickOptions {
skipHover?: boolean
clickCount?: number
}
function clickLabel(
label: HTMLLabelElement,
init: MouseEventInit | undefined,
{clickCount}: clickOptions & PointerOptions,
) {
if (isLabelWithInternallyDisabledControl(label)) return
fireEvent.pointerDown(label, init)
fireEvent.mouseDown(
label,
getMouseEventOptions('mousedown', init, clickCount),
)
fireEvent.pointerUp(label, init)
fireEvent.mouseUp(label, getMouseEventOptions('mouseup', init, clickCount))
fireClick(label, getMouseEventOptions('click', init, clickCount))
// clicking the label will trigger a click of the label.control
// however, it will not focus the label.control so we have to do it
// ourselves.
if (label.control) focus(label.control)
}
function clickBooleanElement(
element: HTMLInputElement,
init: MouseEventInit | undefined,
{clickCount}: clickOptions & PointerOptions,
) {
fireEvent.pointerDown(element, init)
if (!element.disabled) {
fireEvent.mouseDown(
element,
getMouseEventOptions('mousedown', init, clickCount),
)
}
focus(element)
fireEvent.pointerUp(element, init)
if (!element.disabled) {
fireEvent.mouseUp(
element,
getMouseEventOptions('mouseup', init, clickCount),
)
fireClick(element, getMouseEventOptions('click', init, clickCount))
}
}
function clickElement(
element: Element,
init: MouseEventInit | undefined,
{clickCount}: clickOptions & PointerOptions,
) {
const previousElement = getPreviouslyFocusedElement(element)
fireEvent.pointerDown(element, init)
if (!isDisabled(element)) {
const continueDefaultHandling = fireEvent.mouseDown(
element,
getMouseEventOptions('mousedown', init, clickCount),
)
if (continueDefaultHandling) {
const closestFocusable = findClosest(element, isFocusable)
if (previousElement && !closestFocusable) {
blur(previousElement)
} else if (closestFocusable) {
focus(closestFocusable)
}
}
}
fireEvent.pointerUp(element, init)
if (!isDisabled(element)) {
fireEvent.mouseUp(
element,
getMouseEventOptions('mouseup', init, clickCount),
)
fireClick(element, getMouseEventOptions('click', init, clickCount))
const parentLabel = element.closest('label')
if (parentLabel?.control) focus(parentLabel.control)
}
}
function findClosest(element: Element, callback: (e: Element) => boolean) {
let el: Element | null = element
do {
if (callback(el)) {
return el
}
el = el.parentElement
} while (el && el !== element.ownerDocument.body)
return undefined
}
function click(
element: Element,
init?: MouseEventInit,
{
skipHover = false,
clickCount = 0,
skipPointerEventsCheck = false,
}: clickOptions & PointerOptions = {},
) {
if (!skipPointerEventsCheck && !hasPointerEvents(element)) {
throw new Error(
'unable to click element as it has or inherits pointer-events set to "none".',
)
}
// We just checked for `pointerEvents`. We can always skip this one in `hover`.
if (!skipHover) hover(element, init, {skipPointerEventsCheck: true})
if (isElementType(element, 'label')) {
clickLabel(element, init, {clickCount})
} else if (isElementType(element, 'input')) {
if (element.type === 'checkbox' || element.type === 'radio') {
clickBooleanElement(element, init, {clickCount})
} else {
clickElement(element, init, {clickCount})
}
} else {
clickElement(element, init, {clickCount})
}
}
function fireClick(element: Element, mouseEventOptions: MouseEventInit) {
if (mouseEventOptions.button === 2) {
fireEvent.contextMenu(element, mouseEventOptions)
} else {
fireEvent.click(element, mouseEventOptions)
}
}
function dblClick(
element: Element,
init?: MouseEventInit,
{skipPointerEventsCheck = false}: clickOptions & PointerOptions = {},
) {
if (!skipPointerEventsCheck && !hasPointerEvents(element)) {
throw new Error(
'unable to double-click element as it has or inherits pointer-events set to "none".',
)
}
hover(element, init, {skipPointerEventsCheck})
click(element, init, {skipHover: true, clickCount: 0, skipPointerEventsCheck})
click(element, init, {skipHover: true, clickCount: 1, skipPointerEventsCheck})
fireEvent.dblClick(element, getMouseEventOptions('dblclick', init, 2))
}
export {click, dblClick}