Skip to content

Commit

Permalink
feat(ui): set focus on combobox when loaded
Browse files Browse the repository at this point in the history
When Combobox is loaded in Datacenters and Infrastructures views, the
focus is automatically set on the input so user can directly type or
select an entry with keyboard without requiring an initial click on the
Combobox.
  • Loading branch information
rezib authored and florianLSP committed Feb 5, 2024
1 parent a350376 commit f827059
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion racksdb/web/ui/src/components/ComboBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This file is part of RacksDB.
SPDX-License-Identifier: GPL-3.0-or-later -->

<script setup lang="ts">
import { ref, computed, watchEffect } from 'vue'
import { ref, computed, watchEffect, onMounted } from 'vue'
import type { Ref } from 'vue'
import { useRouter } from 'vue-router'
import type { Datacenter, Infrastructure } from '@/composables/RacksDBAPI'
Expand All @@ -21,6 +21,7 @@ import { ChevronUpDownIcon, CheckIcon } from '@heroicons/vue/24/outline'
const selectedItem: Ref<string | undefined> = ref()
const router = useRouter()
const input = ref('')
const inputComponent: Ref<typeof ComboboxInput | null> = ref(null)
const filteredItem = computed(() =>
input.value === ''
? props.items
Expand Down Expand Up @@ -52,13 +53,25 @@ watchEffect(() => {
goToItem(selectedItem.value)
}
})

onMounted(() => {
/*
* Give focus to ComboboxInputElement when component is loaded.
*
* This is not stated in Headless UI documentation but ComboboxInput
* component exposes an el attribute which is a ref on the child input
* HTML element. This can be use to set focus on this element.
*/
inputComponent.value?.el.focus()
})
</script>

<template>
<div class="flex justify-center items-center pt-10">
<Combobox as="div" v-model="selectedItem" class="p-4 px-20">
<div class="relative mt-2">
<ComboboxInput
ref="inputComponent"
class="w-96 focus:border-purple-700 focus:outline-none border-2 border-solid rounded-md bg-white py-1.5 pl-3 pr-10 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-purple-600 sm:text-sm sm:leading-6"
placeholder="Filter a name"
@change="input = $event.target.value"
Expand Down

0 comments on commit f827059

Please sign in to comment.