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

138 lines
3.7 KiB
Vue

<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Button,
EmptyState,
FieldError,
Form,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const states = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const countries = [
{ id: "usa", name: "United States" },
{ id: "canada", name: "Canada" },
{ id: "mexico", name: "Mexico" },
{ id: "uk", name: "United Kingdom" },
{ id: "france", name: "France" },
{ id: "germany", name: "Germany" },
];
const message = ref<string | null>(null);
function onSubmit(event: Event) {
event.preventDefault();
const formData = new FormData(event.currentTarget as HTMLFormElement);
message.value = `表单提交成功!州:${formData.get("state")},国家:${formData.get("country")}`;
}
</script>
<template>
<Form class="flex w-[256px] flex-col gap-4" @submit="onSubmit">
<Autocomplete
is-required
class="w-full"
name="state"
placeholder="请选择一项"
selection-mode="single"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="state in states"
:id="state.id"
:key="state.id"
:text-value="state.name"
>
{{ state.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
<FieldError />
</Autocomplete>
<Autocomplete
is-required
class="w-full"
name="country"
placeholder="选择一个国家"
selection-mode="single"
>
<Label>国家</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索国家">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索国家…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="country in countries"
:id="country.id"
:key="country.id"
:text-value="country.name"
>
{{ country.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
<FieldError />
</Autocomplete>
<Button type="submit">提交</Button>
<p v-if="message" role="status" class="text-sm text-success">
{{ message }}
</p>
</Form>
</template>