-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathkeypress.ts
68 lines (62 loc) · 1.68 KB
/
keypress.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
/* eslint-disable @typescript-eslint/no-use-before-define */
import {isContentEditable, isEditable, isElementType} from '../../utils'
import {input} from '../input'
import {behavior} from './registry'
behavior.keypress = (event, target, instance) => {
if (event.key === 'Enter') {
if (
isElementType(target, 'button') ||
(isElementType(target, 'input') &&
ClickInputOnEnter.includes(target.type)) ||
(isElementType(target, 'a') && Boolean(target.href))
) {
return () => {
instance.dispatchUIEvent(target, 'click')
}
} else if (isElementType(target, 'input')) {
const form = target.form
const submit = form?.querySelector(
'input[type="submit"], button:not([type]), button[type="submit"]',
)
if (submit) {
return () => instance.dispatchUIEvent(submit, 'click')
} else if (
form &&
SubmitSingleInputOnEnter.includes(target.type) &&
form.querySelectorAll('input').length === 1
) {
return () => instance.dispatchUIEvent(form, 'submit')
} else {
return
}
}
}
if (isEditable(target)) {
const inputType =
event.key === 'Enter'
? isContentEditable(target) && !instance.system.keyboard.modifiers.Shift
? 'insertParagraph'
: 'insertLineBreak'
: 'insertText'
const inputData = event.key === 'Enter' ? '\n' : event.key
return () => input(instance, target, inputData, inputType)
}
}
const ClickInputOnEnter = [
'button',
'color',
'file',
'image',
'reset',
'submit',
]
const SubmitSingleInputOnEnter = [
'email',
'month',
'password',
'search',
'tel',
'text',
'url',
'week',
]