Files
heroui-vue/apps/docs/demos/autocomplete-location-search.vue
T
maoyingjie 5e688680e9 feat(autocomplete): migrate Autocomplete with popover-owned filtering
Port the fixed-baseline Autocomplete onto the B02a collection protocol. The
trigger is the single Tab entry; opening moves focus to the search box inside the
popover, which owns filtering, aria-activedescendant and keyboard navigation
while the list only tracks an active row.

- Seven parts: root (field, value/open models, form value, owner wiring),
  trigger (anchor + activation), value (scoped slot with selectedItems and
  selectedText instead of copied item subtrees), indicator, clear button,
  popover (measured trigger width), filter (owns the search text and composes the
  inner SearchFieldInput into the combobox control).
- Filter exposes input-value/update:inputValue/inputChange, matching ComboBox.
- Confirming the active row in multiple mode now toggles it instead of replacing
  the accepted set (ComboBox shared the same bug).
- ComboBox shows the accepted label on the first render via a display value that
  falls back to the selected record, so preselected fields are readable before
  the selection watcher runs (including SSR).
- Popovers measure the anchor with a ResizeObserver because Reka does not publish
  the anchor size; the caller's root class is merged again so the trigger fills
  the field width.
- 24 baseline previews, Chinese doc page, navigation, 17 behaviour tests, 5
  demo/document contracts and an independent tarball consumption script for both
  pickers.

Not included: the full bundler/NodeNext type matrix (B02d).
2026-10-25 17:20:17 +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 class="p-1">
{{ loading ? "搜索中…" : "未找到城市" }}
</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>