feat(@vant/use): useClickAway support multiple targets (#10948)

This commit is contained in:
neverland
2022-08-21 10:55:18 +08:00
committed by GitHub
parent 1e8187bc37
commit 31ac5faa3a
3 changed files with 17 additions and 9 deletions

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);
}
};