<script setup lang="ts" generic="T extends { id: string | number }">
/*
* One list for anything with an id: the parent decides how each item looks
* through the #item slot, and TypeScript knows what `item` is in there.
*/
const { items } = defineProps<{ items: T[] }>();
const selected = defineModel<T | undefined>();
defineSlots<{
item(props: { item: T; active: boolean }): unknown;
}>();
</script>
<template>
<ul class="pick">
<li v-for="item in items" :key="item.id">
<button :aria-pressed="selected?.id === item.id" @click="selected = item">
<slot name="item" :item="item" :active="selected?.id === item.id" />
</button>
</li>
</ul>
</template><script setup lang="ts">
import { ref } from "vue";
import PickList from "./PickList.vue";
type Record = { id: number; title: string; artist: string; year: number };
const records: Record[] = [
{ id: 1, title: "Kind of Blue", artist: "Miles Davis", year: 1959 },
{ id: 2, title: "Blue Train", artist: "John Coltrane", year: 1958 },
{ id: 3, title: "Mingus Ah Um", artist: "Charles Mingus", year: 1959 },
];
const picked = ref<Record>();
</script>
<template>
<PickList v-model="picked" :items="records">
<template #item="{ item, active }">
<strong>{{ item.title }}</strong> · {{ item.artist }} <span v-if="active">✓</span>
</template>
</PickList>
<p class="muted">{{ picked ? `${picked.title}, ${picked.year}` : "Elige un disco" }}</p>
</template>Elige un disco