Skip to content

Commit 18ce00d

Browse files
authored
fix: global mousedown handler in picker input hooks (#5657)
1 parent 3cd7ca3 commit 18ce00d

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

components/vc-picker/hooks/usePickerInput.ts

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ComputedRef, HTMLAttributes, Ref } from 'vue';
2-
import { onBeforeUnmount, watchEffect, watch, ref, computed } from 'vue';
2+
import { onBeforeUnmount, onMounted, watch, ref, computed } from 'vue';
33
import type { FocusEventHandler } from '../../_util/EventInterface';
44
import KeyCode from '../../_util/KeyCode';
55
import { addGlobalMousedownEvent, getTargetFromEvent } from '../utils/uiUtil';
@@ -148,30 +148,26 @@ export default function usePickerInput({
148148
});
149149
const globalMousedownEvent = ref();
150150
// Global click handler
151-
watchEffect(
152-
() =>
153-
globalMousedownEvent.value &&
154-
globalMousedownEvent.value()(
155-
(globalMousedownEvent.value = addGlobalMousedownEvent((e: MouseEvent) => {
156-
const target = getTargetFromEvent(e);
157-
158-
if (open) {
159-
const clickedOutside = isClickOutside(target);
160-
161-
if (!clickedOutside) {
162-
preventBlurRef.value = true;
163-
164-
// Always set back in case `onBlur` prevented by user
165-
raf(() => {
166-
preventBlurRef.value = false;
167-
});
168-
} else if (!focused.value || clickedOutside) {
169-
triggerOpen(false);
170-
}
171-
}
172-
})),
173-
),
174-
);
151+
onMounted(() => {
152+
globalMousedownEvent.value = addGlobalMousedownEvent((e: MouseEvent) => {
153+
const target = getTargetFromEvent(e);
154+
155+
if (open.value) {
156+
const clickedOutside = isClickOutside(target);
157+
158+
if (!clickedOutside) {
159+
preventBlurRef.value = true;
160+
161+
// Always set back in case `onBlur` prevented by user
162+
raf(() => {
163+
preventBlurRef.value = false;
164+
});
165+
} else if (!focused.value || clickedOutside) {
166+
triggerOpen(false);
167+
}
168+
}
169+
});
170+
});
175171
onBeforeUnmount(() => {
176172
globalMousedownEvent.value && globalMousedownEvent.value();
177173
});

0 commit comments

Comments
 (0)