Files
heroui-vue/apps/docs/demos/autocomplete-user-selection.vue
T
maoyingjie 3e9be13b19 fix(pickers): repair data-slot selectors and align demos with the baseline
The CSS port prefixed data-slot attribute values with hv-, so the compiled
selectors never matched the DOM and the ComboBox trigger chevron never
rotated. Only class names get the prefix; slot values stay byte-identical.

Also commit typed text as the value when allowsCustomValue finds no record,
restore the baseline search-field shell in all 24 Autocomplete demos, and
correct both doc pages against the pinned upstream sources.
2026-10-25 17:46:32 +08:00

147 lines
3.8 KiB
Vue

<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Avatar,
AvatarFallback,
AvatarImage,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} 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",
},
];
const selectedKey = ref<string | null>(null);
const userOf = (key: unknown) => users.find((user) => user.id === key);
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="选择用户"
selection-mode="single"
>
<Label>用户</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ 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="userOf(selectedItems[0]?.key)?.avatarUrl ?? ''"
/>
<AvatarFallback>
{{ userOf(selectedItems[0]?.key)?.fallback }}
</AvatarFallback>
</Avatar>
<span>{{ userOf(selectedItems[0]?.key)?.name }}</span>
</div>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索用户"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索用户…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="user in users"
:id="user.id"
:key="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>
<template #empty>
<EmptyState>未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>