mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-15 18:01:24 +08:00
f15d61ddbc
* fix: fix timepicker keyevent bug fix #904 * chore: update * chore: update * chore: update
34 lines
788 B
TypeScript
34 lines
788 B
TypeScript
/**
|
|
* Trigger event
|
|
* mouseenter, mouseleave, mouseover, keyup, change, click
|
|
* @param {Element} elm
|
|
* @param {String} name
|
|
* @param {*} opts
|
|
*/
|
|
const triggerEvent = (elm, name, ...opts) => {
|
|
let eventName
|
|
|
|
if (/^mouse|click/.test(name)) {
|
|
eventName = 'MouseEvents'
|
|
} else if (/^key/.test(name)) {
|
|
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
|
|
? elm.dispatchEvent(evt)
|
|
: elm.fireEvent('on' + name, evt)
|
|
|
|
return elm
|
|
}
|
|
export default triggerEvent
|