Skip to content

fix(vc-picker): global mousedown handler in usePickerInput hooks is never registered #5657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions components/vc-picker/hooks/usePickerInput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComputedRef, HTMLAttributes, Ref } from 'vue';
import { onBeforeUnmount, watchEffect, watch, ref, computed } from 'vue';
import { onBeforeUnmount, onMounted, watch, ref, computed } from 'vue';
import type { FocusEventHandler } from '../../_util/EventInterface';
import KeyCode from '../../_util/KeyCode';
import { addGlobalMousedownEvent, getTargetFromEvent } from '../utils/uiUtil';
Expand Down Expand Up @@ -148,30 +148,26 @@ export default function usePickerInput({
});
const globalMousedownEvent = ref();
// Global click handler
watchEffect(
() =>
globalMousedownEvent.value &&
globalMousedownEvent.value()(
(globalMousedownEvent.value = addGlobalMousedownEvent((e: MouseEvent) => {
const target = getTargetFromEvent(e);

if (open) {
const clickedOutside = isClickOutside(target);

if (!clickedOutside) {
preventBlurRef.value = true;

// Always set back in case `onBlur` prevented by user
raf(() => {
preventBlurRef.value = false;
});
} else if (!focused.value || clickedOutside) {
triggerOpen(false);
}
}
})),
),
);
onMounted(() => {
globalMousedownEvent.value = addGlobalMousedownEvent((e: MouseEvent) => {
const target = getTargetFromEvent(e);

if (open.value) {
const clickedOutside = isClickOutside(target);

if (!clickedOutside) {
preventBlurRef.value = true;

// Always set back in case `onBlur` prevented by user
raf(() => {
preventBlurRef.value = false;
});
} else if (!focused.value || clickedOutside) {
triggerOpen(false);
}
}
});
});
onBeforeUnmount(() => {
globalMousedownEvent.value && globalMousedownEvent.value();
});
Expand Down