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

287 lines
8.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const items = [
{ id: "option1", name: "选项 1" },
{ id: "option2", name: "选项 2" },
{ id: "option3", name: "选项 3" },
{ id: "option4", name: "选项 4" },
];
const selectedKey1 = ref<string | null>(null);
const selectedKey2 = ref<string | null>(null);
const selectedKeys1 = ref<TagKey[]>([]);
const selectedKeys2 = ref<TagKey[]>([]);
function onRemoveTags1(keys: TagKey[]) {
selectedKeys1.value = selectedKeys1.value.filter(
(key) => !keys.includes(key),
);
}
function onRemoveTags2(keys: TagKey[]) {
selectedKeys2.value = selectedKeys2.value.filter(
(key) => !keys.includes(key),
);
}
</script>
<template>
<div class="flex flex-col gap-8">
<div class="flex flex-col gap-4">
<h3 class="text-lg font-semibold">单选变体</h3>
<div class="flex flex-col gap-4">
<Autocomplete
v-model="selectedKey1"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
variant="primary"
>
<Label>主色(primary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
name="search"
variant="secondary"
aria-label="搜索选项"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState>未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<Autocomplete
v-model="selectedKey2"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
variant="secondary"
>
<Label>次色(secondary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
name="search"
variant="secondary"
aria-label="搜索选项"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState>未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</div>
</div>
<div class="flex flex-col gap-4">
<h3 class="text-lg font-semibold">多选变体</h3>
<div class="flex flex-col gap-4">
<Autocomplete
v-model="selectedKeys1"
class="w-[256px]"
placeholder="选择多项"
selection-mode="multiple"
variant="primary"
>
<Label>主色(primary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template
#default="{ defaultChildren, isPlaceholder, selectedItems }"
>
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags1">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
name="search"
variant="secondary"
aria-label="搜索选项"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState>未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<Autocomplete
v-model="selectedKeys2"
class="w-[256px]"
placeholder="选择多项"
selection-mode="multiple"
variant="secondary"
>
<Label>次色(secondary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template
#default="{ defaultChildren, isPlaceholder, selectedItems }"
>
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup
v-else
size="sm"
variant="surface"
@remove="onRemoveTags2"
>
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
name="search"
variant="secondary"
aria-label="搜索选项"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState>未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</div>
</div>
</div>
</template>