wujingjing
2025-04-14 1df71bdd7fc5b35be1447c9cc574bf610666f436
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { computed, watch, type Ref, watchEffect, nextTick } from 'vue';
 
export const useClickOther = (
    excludeEle: Ref<HTMLElement>[] | Ref<HTMLElement>,
    isShow: Ref<boolean>,
    handleClickOther: () => void = () => {
        isShow.value = false;
    }
) => {
    const domList = computed(() => {
        const computedEle = Array.isArray(excludeEle) ? excludeEle : [excludeEle];
        return computedEle.map((item) => item.value?.$el).filter(item=>!!item);
    });
 
 
    
    const listenClickOtherExit = (e) => {
        if (!domList.value.includes(e.target) && domList.value.every((item) => !item.contains(e.target))) {
            handleClickOther();
        }
    };
 
    watch(isShow, (val) => {
        if (val) {
            setTimeout(() => {
                document.addEventListener('click', listenClickOtherExit);
            }, 0);
        } else {
            document.removeEventListener('click', listenClickOtherExit);
        }
    });
};