ant-design-vue/components/vc-select/hooks/useSelectTriggerControl.ts

28 lines
740 B
TypeScript
Raw Normal View History

2020-10-07 14:49:01 +00:00
import { onBeforeUnmount, onMounted, Ref } from 'vue';
export default function useSelectTriggerControl(
2020-10-10 05:57:37 +00:00
refs: Ref[],
2020-10-07 14:49:01 +00:00
open: Ref<boolean>,
triggerOpen: (open: boolean) => void,
) {
function onGlobalMouseDown(event: MouseEvent) {
const target = event.target as HTMLElement;
2020-10-10 05:57:37 +00:00
const elements = [refs[0]?.value, refs[1]?.value?.getPopupElement()];
2020-10-07 14:49:01 +00:00
if (
open.value &&
elements.every(element => element && !element.contains(target) && element !== target)
) {
// Should trigger close
triggerOpen(false);
}
}
onMounted(() => {
window.addEventListener('mousedown', onGlobalMouseDown);
});
onBeforeUnmount(() => {
window.removeEventListener('mousedown', onGlobalMouseDown);
});
}