I am delving into Vue 3 and Headless UI, and I have successfully created a ListBox
component in App.vue
to display a list of People
which is functioning as expected. Now, I am aiming to transform it into a reusable component capable of displaying lists of both People
and Countries
.
How can I make my component dynamic to accommodate both types of lists?
ListBox.vue
<template>
<Listbox v-model="selectedPerson">
<ListboxButton class=" bg-white shadow-sm border-gray-500 border-2 shadow-gray-200 font-bold p-4 text-left rounded-md">
{{ selectedPerson.name }}
</ListboxButton>
<ListboxOptions class="mt-2">
<ListboxOption
as="template" v-slot="{ active }"
v-for="person in people" :key="person"
:value="person">
<li :class="{
'bg-blue-500 text-white p-6': active,
'bg-white shadow-lg p-6 text-black ': !active
}">
{{ person.name }}
</li>
</ListboxOption>
</ListboxOptions>
</Listbox>
</template>
<script setup>
import { ref } from 'vue'
import {
Listbox,
ListboxLabel,
ListboxButton,
ListboxOptions,
ListboxOption,
} from '@headlessui/vue'
const people = [
{ id: 1, name: 'Durward Reynolds' },
{ id: 2, name: 'Kenton Towne' },
{ id: 3, name: 'Therese Wunsch' },
{ id: 4, name: 'Benedict Kessler' },
{ id: 5, name: 'Katelyn Rohan' },
]
const selectedPerson = ref(people[0])
</script>
App.vue:
<template>
<ListBox />
</template>