fix(components): add generic constraints and null checks in AutocompleteDropdown

- Add 'extends string' constraint to generic type T
- Add null check for model.value with nullish coalescing
- Add null check for scroller before accessing properties
- Add instanceof check for HTMLElement before accessing offsetTop
This commit is contained in:
kolaente 2025-11-22 16:00:41 +01:00
parent a575159424
commit 81f85a3849
1 changed files with 10 additions and 4 deletions

View File

@ -1,4 +1,4 @@
<script setup lang="ts" generic="T">
<script setup lang="ts" generic="T extends string">
import {type ComponentPublicInstance, nextTick, ref, watch} from 'vue'
const props = withDefaults(defineProps<{
@ -30,15 +30,21 @@ const editorRef = ref<HTMLTextAreaElement | null>(null)
watch(
() => model.value,
newValue => {
val.value = newValue
val.value = newValue ?? ''
},
)
function updateSuggestionScroll() {
nextTick(() => {
const scroller = suggestionScrollerRef.value
const selectedItem = scroller?.querySelector('.selected')
scroller.scrollTop = selectedItem ? selectedItem.offsetTop : 0
if (!scroller) return
const selectedItem = scroller.querySelector('.selected')
if (selectedItem && selectedItem instanceof HTMLElement) {
scroller.scrollTop = selectedItem.offsetTop
} else {
scroller.scrollTop = 0
}
})
}