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

115 lines
3.0 KiB
Vue

<script setup lang="ts">
import { onBeforeUnmount, ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
type City = {
name: string;
country: string;
};
const allCities: City[] = [
{ country: "美国", name: "纽约" },
{ country: "美国", name: "洛杉矶" },
{ country: "美国", name: "芝加哥" },
{ country: "英国", name: "伦敦" },
{ country: "法国", name: "巴黎" },
{ country: "日本", name: "东京" },
{ country: "澳大利亚", name: "悉尼" },
{ country: "加拿大", name: "多伦多" },
{ country: "德国", name: "柏林" },
{ country: "西班牙", name: "马德里" },
];
const selectedKey = ref<string | null>(null);
const loading = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;
const contains = (text: string, input: string) =>
text.toLocaleLowerCase().includes(input.toLocaleLowerCase());
// 源示例在过滤回调里模拟异步搜索:先标记加载中,300ms 后结束。
function customFilter(text: string, input: string) {
if (!input) return true;
loading.value = true;
clearTimeout(timer);
timer = setTimeout(() => {
loading.value = false;
}, 300);
return contains(text, input);
}
onBeforeUnmount(() => clearTimeout(timer));
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="搜索城市"
selection-mode="single"
>
<Label>城市</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter :filter="customFilter">
<SearchField
auto-focus
aria-label="搜索城市"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索城市…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="city in allCities"
:id="city.name"
:key="city.name"
:text-value="`${city.name} ${city.country}`"
>
<div class="flex flex-col">
<Label>{{ city.name }}</Label>
<Description>{{ city.country }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState>
{{ loading ? "搜索中…" : "未找到城市" }}
</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>