mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
43 lines
946 B
Vue
43 lines
946 B
Vue
<script setup lang="ts">
|
|
const value = ref<string>()
|
|
|
|
const { data: users, execute } = await useLazyFetch('https://jsonplaceholder.typicode.com/users', {
|
|
key: 'typicode-users-email',
|
|
transform: (data: { id: number, name: string, email: string }[]) => {
|
|
return data?.map(user => ({
|
|
label: user.name,
|
|
email: user.email,
|
|
value: String(user.id),
|
|
avatar: { src: `https://i.pravatar.cc/120?img=${user.id}`, loading: 'lazy' as const }
|
|
}))
|
|
},
|
|
immediate: false
|
|
})
|
|
|
|
function onOpen() {
|
|
if (!users.value?.length) {
|
|
execute()
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<USelect
|
|
v-model="value"
|
|
:items="users"
|
|
placeholder="Select user"
|
|
value-key="value"
|
|
:ui="{ content: 'min-w-fit' }"
|
|
class="w-48"
|
|
@update:open="onOpen"
|
|
>
|
|
<template #item-label="{ item }">
|
|
{{ item.label }}
|
|
|
|
<span class="text-muted">
|
|
{{ item.email }}
|
|
</span>
|
|
</template>
|
|
</USelect>
|
|
</template>
|