-
Notifications
You must be signed in to change notification settings - Fork 242
/
dispatchEvent.ts
43 lines (37 loc) 路 1.04 KB
/
dispatchEvent.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
import {Config} from '../setup'
import {EventType} from './types'
import {behavior, BehaviorPlugin} from './behavior'
import {wrapEvent} from './wrapEvent'
export function dispatchEvent(
config: Config,
target: Element,
event: Event,
preventDefault: boolean = false,
) {
const type = event.type as EventType
const behaviorImplementation = preventDefault
? () => {}
: (behavior[type] as BehaviorPlugin<EventType> | undefined)?.(
event,
target,
config,
)
if (behaviorImplementation) {
event.preventDefault()
let defaultPrevented = false
Object.defineProperty(event, 'defaultPrevented', {
get: () => defaultPrevented,
})
Object.defineProperty(event, 'preventDefault', {
value: () => {
defaultPrevented = event.cancelable
},
})
wrapEvent(() => target.dispatchEvent(event), target)
if (!defaultPrevented as boolean) {
behaviorImplementation()
}
return !defaultPrevented
}
return wrapEvent(() => target.dispatchEvent(event), target)
}