Files
heroui-vue/apps/docs/demos/autocomplete-user-selection-multiple.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

159 lines
4.1 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,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} 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 selectedKeys = ref<TagKey[]>(["1", "2"]);
const userOf = (key: unknown) => users.find((user) => user.id === key);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<Autocomplete
v-model="selectedKeys"
class="w-[256px]"
placeholder="选择队友"
selection-mode="multiple"
>
<Label>用户</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
<Avatar class="size-4" size="sm">
<AvatarImage :src="userOf(item.key)?.avatarUrl ?? ''" />
<AvatarFallback>
{{ userOf(item.key)?.fallback }}
</AvatarFallback>
</Avatar>
<span>{{ item.textValue }}</span>
</Tag>
</TagGroupList>
</TagGroup>
</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>