types(@vant/use): improve useEventListener typing (#10952)

This commit is contained in:
neverland 2022-08-21 17:49:48 +08:00 committed by GitHub
parent cb93bd2da3
commit 6b7ae0f6a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -10,6 +10,16 @@ export type UseEventListenerOptions = {
passive?: boolean; passive?: boolean;
}; };
export function useEventListener<K extends keyof DocumentEventMap>(
type: K,
listener: (event: DocumentEventMap[K]) => void,
options?: UseEventListenerOptions
): void;
export function useEventListener(
type: string,
listener: EventListener,
options?: UseEventListenerOptions
): void;
export function useEventListener( export function useEventListener(
type: string, type: string,
listener: EventListener, listener: EventListener,
@ -27,7 +37,10 @@ export function useEventListener(
const element = unref(target); const element = unref(target);
if (element && !attached) { if (element && !attached) {
element.addEventListener(type, listener, { capture, passive }); element.addEventListener(type, listener, {
capture,
passive,
});
attached = true; attached = true;
} }
}; };

View File

@ -222,11 +222,11 @@ export default defineComponent({
); );
// add passive option to avoid Chrome warning // add passive option to avoid Chrome warning
useEventListener('touchstart', onTouchStart as EventListener, { useEventListener('touchstart', onTouchStart, {
target: track, target: track,
passive: true, passive: true,
}); });
useEventListener('touchmove', onTouchMove as EventListener, { useEventListener('touchmove', onTouchMove, {
target: track, target: track,
}); });