Files
heroui-vue/apps/docs/demos/select-custom-value.vue
T

108 lines
2.8 KiB
Vue

<script setup lang="ts">
import {
Select,
SelectTrigger,
SelectValue,
SelectIndicator,
SelectPopover,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Label,
Description,
} from "@heroui-vue/components";
import { Avatar, AvatarImage, AvatarFallback } from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
fallback: "B",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
fallback: "F",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
fallback: "M",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
{
id: "4",
name: "John",
email: "john@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
},
{
id: "5",
name: "Jane",
email: "jane@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
},
];
</script>
<template>
<Select class="w-[256px]" placeholder="请选择用户">
<Label>用户</Label>
<SelectTrigger>
<SelectValue v-slot="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || !selectedItems.length">
{{ defaultChildren }}
</template>
<template v-else-if="selectedItems.length > 1">
{{ selectedItems.length }} 位用户已选
</template>
<div v-else class="flex items-center gap-2">
<Avatar class="size-4" size="sm">
<AvatarImage
:src="
users.find((u) => u.id === selectedItems[0]?.key)?.avatarUrl ??
''
"
/>
<AvatarFallback>
{{ users.find((u) => u.id === selectedItems[0]?.key)?.fallback }}
</AvatarFallback>
</Avatar>
<span>{{ defaultChildren }}</span>
</div>
</SelectValue>
<SelectIndicator />
</SelectTrigger>
<SelectPopover>
<ListBox>
<ListBoxItem
v-for="user in users"
:key="user.id"
:id="user.id"
:text-value="user.name"
>
<Avatar size="sm">
<AvatarImage :src="user.avatarUrl" />
<AvatarFallback>{{ user.fallback }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</SelectPopover>
</Select>
</template>