element-plus/packages/directives/repeat-click/index.ts
Obaydur Rahman e25df5408f
fix(directives): [repeat-click] Interval time is too short for single clicks (#9466)
* fix(directives): [repeat-click] time is too short for single clicks

* fix(test-utils): [repeat-click] increase sleep time

Updated times according to PR #9466

* fix(test-utils): [repeat-click] increase test time

Updated times according to PR #9466

* chore: update

* chore: improve test case

Co-authored-by: holazz <2418184580@qq.com>
2022-08-31 13:47:58 +08:00

40 lines
980 B
TypeScript

import type { DirectiveBinding, ObjectDirective } from 'vue'
export const REPEAT_INTERVAL = 100
export const REPEAT_DELAY = 600
const RepeatClick: ObjectDirective = {
beforeMount(el: HTMLElement, binding: DirectiveBinding) {
let interval: ReturnType<typeof setInterval> | null = null
let delay: ReturnType<typeof setTimeout> | null = null
const handler = () => binding.value && binding.value()
const clear = () => {
if (delay) {
clearTimeout(delay)
delay = null
}
if (interval) {
clearInterval(interval)
interval = null
}
}
el.addEventListener('mousedown', (e: MouseEvent) => {
if (e.button !== 0) return
handler()
document.addEventListener('mouseup', clear, { once: true })
clear()
delay = setTimeout(() => {
interval = setInterval(() => {
handler()
}, REPEAT_INTERVAL)
}, REPEAT_DELAY)
})
},
}
export default RepeatClick