Merge branch 'dev' into next

This commit is contained in:
chenjiahan
2022-08-24 22:04:34 +08:00
39 changed files with 485 additions and 372 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@vant/use",
"version": "1.4.1",
"version": "1.4.2",
"description": "Vant Composition API",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.mjs",

View File

@@ -7,7 +7,10 @@ export type UseClickAwayOptions = {
};
export function useClickAway(
target: Element | Ref<Element | undefined>,
target:
| Element
| Ref<Element | undefined>
| Array<Element | Ref<Element | undefined>>,
listener: EventListener,
options: UseClickAwayOptions = {}
) {
@@ -18,8 +21,13 @@ export function useClickAway(
const { eventName = 'click' } = options;
const onClick = (event: Event) => {
const element = unref(target);
if (element && !element.contains(event.target as Node)) {
const targets = Array.isArray(target) ? target : [target];
const isClickAway = targets.every((item) => {
const element = unref(item);
return element && !element.contains(event.target as Node);
});
if (isClickAway) {
listener(event);
}
};

View File

@@ -10,6 +10,16 @@ export type UseEventListenerOptions = {
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(
type: string,
listener: EventListener,
@@ -27,7 +37,10 @@ export function useEventListener(
const element = unref(target);
if (element && !attached) {
element.addEventListener(type, listener, { capture, passive });
element.addEventListener(type, listener, {
capture,
passive,
});
attached = true;
}
};