diff --git a/ui/src/formkit/inputs/select/SelectOption.vue b/ui/src/formkit/inputs/select/SelectOption.vue index 41d248193..b1f782d5b 100644 --- a/ui/src/formkit/inputs/select/SelectOption.vue +++ b/ui/src/formkit/inputs/select/SelectOption.vue @@ -27,6 +27,8 @@ const emit = defineEmits<{ const selectedIndex = ref(0); const selectOptionRef = ref(); +const oldEvent = ref(); +const isCursorHidden = ref(false); const selectedValues = computed(() => props.selectedOptions?.map((option) => option.value) @@ -46,6 +48,9 @@ const getSelectedIndex = () => { }; const handleKeydown = (event: KeyboardEvent) => { + if (selectOptionRef.value) { + isCursorHidden.value = true; + } const key = event.key; if (key === "ArrowUp") { selectedIndex.value = @@ -138,7 +143,7 @@ const handleOptionScroll = (state: UseScrollReturn) => { }; watch( - () => props.options, + props.options, () => { selectedIndex.value = getSelectedIndex(); }, @@ -153,6 +158,48 @@ watch( handleScrollIntoView(); } ); + +/** + * check if cursor is changed + * + * @param newEvent + * @param oldEvent + */ +const isCursorChanged = ( + newEvent: MouseEvent, + oldEvent: MouseEvent | undefined +) => { + if (!oldEvent) { + return true; + } + return ( + newEvent.screenX !== oldEvent.screenX || + newEvent.screenY !== oldEvent.screenY + ); +}; + +const handleMouseover = (event: MouseEvent) => { + if (isCursorHidden.value) { + if (!oldEvent.value) { + oldEvent.value = event; + } + + if (isCursorChanged(event, oldEvent.value)) { + isCursorHidden.value = false; + oldEvent.value = undefined; + } + return; + } + const target = event.target as HTMLElement; + if ( + target.classList.contains("select-option-item") && + target instanceof HTMLElement + ) { + const parentElement = target.parentElement as HTMLElement; + const index = Array.from(parentElement.children).indexOf(target); + selectedIndex.value = index; + } +};