mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-14 01:11:25 +08:00
b8c38a9fe5
* chore: enhance eslint rules * chore: enhance eslint rules
30 lines
694 B
TypeScript
30 lines
694 B
TypeScript
/**
|
|
* Trigger event
|
|
* mouseenter, mouseleave, mouseover, keyup, change, click
|
|
*/
|
|
const triggerEvent = (elm: Element, name: string, ...opts: any[]) => {
|
|
let eventName: string
|
|
|
|
if (/^mouse|click/.test(name)) {
|
|
eventName = 'MouseEvents'
|
|
} else if (name.startsWith('key')) {
|
|
eventName = 'KeyboardEvent'
|
|
} else {
|
|
eventName = 'HTMLEvents'
|
|
}
|
|
const evt = document.createEvent(eventName)
|
|
|
|
evt.initEvent(name, ...opts)
|
|
|
|
if (name === 'keydown' && opts[0]) {
|
|
// trigger event with keycode
|
|
// triggerEvent(ele, 'keydown', 'ArrowDown')
|
|
Object.defineProperty(evt, 'code', { value: opts[0] })
|
|
}
|
|
elm.dispatchEvent(evt)
|
|
|
|
return elm
|
|
}
|
|
|
|
export default triggerEvent
|