-
Notifications
You must be signed in to change notification settings - Fork 240
/
clear.ts
41 lines (33 loc) 路 1.21 KB
/
clear.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
import {isDisabled, isInstanceOfElement} from './utils'
import {type} from './type'
function clear(element: Element) {
if (
!isInstanceOfElement(element, 'HTMLInputElement') &&
!isInstanceOfElement(element, 'HTMLTextAreaElement')
) {
// TODO: support contenteditable
throw new Error(
'clear currently only supports input and textarea elements.',
)
}
const el = element as HTMLInputElement | HTMLTextAreaElement
if (isDisabled(el)) {
return
}
// TODO: track the selection range ourselves so we don't have to do this input "type" trickery
// just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
const elementType = el.type
if (elementType !== 'textarea') {
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
;(element as HTMLInputElement).type = 'text'
}
type(element, '{selectall}{del}', {
delay: 0,
initialSelectionStart: el.selectionStart ?? undefined,
initialSelectionEnd: el.selectionEnd ?? undefined,
})
if (elementType !== 'textarea') {
;(el as HTMLInputElement).type = elementType
}
}
export {clear}