element-plus/packages/utils/dom/event.ts
LIUCHAO 7bb6b6d99f
refactor(utils): drop off/on/once, use EventListener instead (#8960)
* refactor(utils): drop off/on/once, use EventListener instead

* refactor(utils): use useEventListener

* refactor(utils): use useEventListener

* style(components): [roving-focus-group] useEventListener  pass ref
2022-07-26 23:01:07 +08:00

21 lines
607 B
TypeScript

export const composeEventHandlers = <E>(
theirsHandler?: (event: E) => boolean | void,
oursHandler?: (event: E) => void,
{ checkForDefaultPrevented = true } = {}
) => {
const handleEvent = (event: E) => {
const shouldPrevent = theirsHandler?.(event)
if (checkForDefaultPrevented === false || !shouldPrevent) {
return oursHandler?.(event)
}
}
return handleEvent
}
type WhenMouseHandler = (e: PointerEvent) => any
export const whenMouse = (handler: WhenMouseHandler): WhenMouseHandler => {
return (e: PointerEvent) =>
e.pointerType === 'mouse' ? handler(e) : undefined
}