Skip to content

Commit

Permalink
Drop CmkDropdown up if space below is limited
Browse files Browse the repository at this point in the history
CMK-20367

Change-Id: I9cf94749c1dba4622162e76276aec0c8692f1f22
  • Loading branch information
cellador committed Dec 18, 2024
1 parent 9674510 commit 393e23c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/cmk-frontend-vue/src/components/CmkDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const noChoiceAvailable = computed(
)

const suggestionsShown = ref(false)
const suggestionsRef = ref<HTMLUListElement | null>(null)
const suggestionInputRef = ref<HTMLInputElement | null>(null)
const comboboxButtonRef = ref<HTMLButtonElement | null>(null)
const optionRefs = ref<(HTMLLIElement | null)[]>([])
Expand All @@ -69,11 +70,22 @@ watch(filterString, (newFilterString) => {
function showSuggestions(): void {
if (!disabled && !noChoiceAvailable.value) {
suggestionsShown.value = !suggestionsShown.value
if (!suggestionsShown.value) {
return
}
filterString.value = ''
filteredOptions.value = options.map((_, index) => index)
selectedSuggestionOptionIndex.value = filteredOptions.value[0] ?? null
// eslint-disable-next-line @typescript-eslint/no-floating-promises
nextTick(() => {
if (suggestionsRef.value) {
const suggestionsRect = suggestionsRef.value.getBoundingClientRect()
if (window.innerHeight - suggestionsRect.bottom < suggestionsRect.height) {
suggestionsRef.value.style.bottom = `calc(2 * var(--spacing))`
} else {
suggestionsRef.value.style.removeProperty('bottom')
}
}
suggestionInputRef.value?.focus()
})
}
Expand Down Expand Up @@ -170,6 +182,7 @@ function wrap(index: number, length: number): number {
<span v-else>{{ noElementsText }}</span>
<ul
v-if="!!suggestionsShown"
ref="suggestionsRef"
class="cmk-dropdown__suggestions"
@keydown.escape.prevent="hideSuggestions"
@keydown.tab.prevent="hideSuggestions"
Expand Down

0 comments on commit 393e23c

Please sign in to comment.